![627dbf94fd004e449a61fd00bb1ac851.gif](https://i-blog.csdnimg.cn/blog_migrate/77bf6f18fdeb52d5b3bd6d5293945b5b.gif)
![3cfc4bb3d309a978d282cd6af72535d6.png](https://i-blog.csdnimg.cn/blog_migrate/8bc7cd2c01156ab9da0e672fadee8ec0.png)
CSS定位是基于盒子模型的,可分为静态static定位,绝对absolute定位,相对relative 定位,固定fixed定位。
盒子模型:
static:对象遵循正常文档流,top、right、bottom、left等属性无效。absolute:对象脱离正常文档流,使用left、right、top、bottom等属性进行定位,其层叠可通过css z-index属性定义,此时对象不具有margin边距,但仍有补白和边框border。absoulte定位虽然号称“绝对”,但是它的功能却更接近于“相对”一词,因为使用absoulte定位的元素脱离文档流后,就只能根据祖先类元素(父类及以上)进行定位,而这个祖先类还必须是以postion非static方式定位的, 举个例子,a元素使用absoulte定位,它会从父类开始找起,寻找以position非static方式定位的直系祖先类元素,直到标签为止,这里还需要注意的是,relative和static方式在最外层时是以标签为定位原点的,而absoulte方式在无父级是position非static定位时是以作为原点定位,和元素相差9px左右。
relative:对象遵循正常文档流,相对于对象左上角位置(移动前)进行定位,移动后仍占据原空间,对象不可层叠,但将依据left、right、top、bottom等属性在正常文档流中偏移位置。
span style="box-sizing: border-box;outline: 0px;overflow-wrap: break-word;">html>
<style>
html{border:1px dashed green;}
body{border:1px dashed yellow;}
#first{
width:200px;
height:200px;
border:1px solid red;
position: relative;
color:red;
}
#second{
width:200px;
height:200px;
color:blue;
border:1px solid blue;
position: absolute;
top:0;
left:0;
}
style>
<body>
<div id="first">relativediv>
<div id="second">absolutediv>
body>
html>
fixed:对象脱离正常文档流,使用top、right、bottom、left等属性以窗口左上角为参考点进行定位,当出现滚动条时,对象不会随着滚动,其层叠通过z-index属性定义。overflow:如果一个元素的大小固定,但其内容放不下,就会导致溢出,overflow控制溢出部分的可见visible、不可见hidden、滚动可见scroll、自动auto、 继承inherit、默认值为visible。
文档流:将浏览器窗体内部自上而下分成一行行, 并在每行中按从左至右的顺序排放元素,即为文档流。
![ebac9d19dc5cc5ad208121a1dcdbdd32.gif](https://i-blog.csdnimg.cn/blog_migrate/2d23b6cdac9066f2d429b38f72b4e99a.gif)
更多精彩请继续关注小编了解哦!
计算机毕业设计(源程序+论文+开题报告+文献综述+翻译+答辩稿)
联系QQ:2932963541进行咨询
![5e02999c17ba3b479c47841137997ba2.png](https://i-blog.csdnimg.cn/blog_migrate/48f49864d38af9d1236eefa208056a84.png)
![9bcc6785fb9e058e7d84e55665b6becc.png](https://i-blog.csdnimg.cn/blog_migrate/24e37c3403a295259385481d095a7ba6.jpeg)
![dc40958be4d8aeaca6ebc8fdc09d0ff7.png](https://i-blog.csdnimg.cn/blog_migrate/16a2e0e51d8adb1a68cb7d2739a3fc9c.png)