博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
css绝对定位中的初始包含块问题
阅读量:6863 次
发布时间:2019-06-26

本文共 1459 字,大约阅读时间需要 4 分钟。

在css的绝对定位问题中如果绝对定位的元素没有最近已定位的父元素,那么它相对与初始包含块进行定位。其中初始包含块是是呢?body? html? viewport?

如图所示:

 

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
html
{
border:2px groove #F0F;
padding:50px;
margin:50px;
}
body
{
border:2px dotted red;
margin:20px;
padding:20px;
}
div
{
border:1px solid blue;
background-color:lightblue;
height:300px;
}
img
{
position:absolute;
left:0px;
bottom:0px;
}
</style>
</head>
<body>
i am okok i am okoki am okok i am okok i am okok i am okok i am okok i am okok i am okoki am okok i am okok i am okok i am okok i am okok okok i am okok i am okokokok i am okok i am okokokok i am okok i am okokokok i am okok i am okok
<img src="12.jpg" width="300px" height="300px">
<div>everything will be ok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okok this is okokthis is okok this is okok this is okok this is okok</div>
</body>
</html>

 

可以看出此时top:0px left:0px 的图像既不相对与body也不想对于html,而是相对与视窗屏幕的左上角。

如果把top:0px改成bottom:0px, 这是相对与第一屏的左下角(即使有下拉列表)。

  

 如果要想让定位得img相对于html或body,有两种方法:

1.把html,body{margin:0px; padding:0px} 

2.body设置为position:relative显示指定已body元素作为初始包含块。 

 

 

转载于:https://www.cnblogs.com/feng12345/p/5768857.html

你可能感兴趣的文章
linux用户和文件目录管理
查看>>
表空间数据结构研究_01
查看>>
mysql 编译安装
查看>>
使用KiWi Syslog Daemon构建日志服务器
查看>>
坐观 “爱情”
查看>>
linux apt-get proxy
查看>>
Spring学习总结1——bean的生命周期
查看>>
我的友情链接
查看>>
Apache Tomcat7+MySQL5.6配置
查看>>
char varchar nvarchar区别
查看>>
iOS原生定位和反编码
查看>>
华为交换机配置命令 华为QuidWay交换机配置命令手册
查看>>
如何解决JSP页面的乱码问题
查看>>
SQL中的Null值
查看>>
细看晶圆代工之争,纳米制程是什么
查看>>
东软java实训第一个项目人力资源管理
查看>>
我的友情链接
查看>>
ESXI GLusterFS ISCSI 构建低端虚拟化解决方案
查看>>
mysql使用
查看>>
[Ubuntu 12.10] Openstack 多节点安装--前期准备&&网络拓扑
查看>>