一·、用position定位
position:static | 默认值,无效果 |
position:relative | 相对,相对于自己的位置进行定位 |
position:absolute | 绝对定位,脱离文档流,相对于浏览器进行定位 |
position:fixed | 固定定位 |
position:stricky | 粘性定位 |
posit:z-index | 元素的堆叠顺序,对定位起作用 |
1.相对定位;
一个div,最初在页面的左上角,用position进行定位,用left,right,top,bottom以div原来的位置为标准,进行移动
缺点:用position移动位置后,原来的位置还在,不能被其他元素占用
实例:
<style>
.box1{
width:200px;
height:200px;
background-color: blue;
position:relative;
top:200px;
left:200px;
}
.box2{
width:300px;
height:300px;
background-color: red;
}
.box3{
width:200px;
height:200px;
background-color: aqua;
}
</style>
</head>
<body>
<div class="box1">
</div>
<div class="box2">
</div>
<div class="box3">
</div>
</body>
2.绝对定位:
以浏览器为标准进行移动,脱离了一般的文档流,一般与相对定位配合使用。
如果外面套了相对定位的父元素,那么只能在父元素里进行定位
3.粘性定位:
基于浏览器为标准进行移动,不管有没有父元素,都只在浏览器里进行定位。
在网页中有滑轮的部分,固定定位让元素不跟随着滑轮进行滑动。
4. 固定定位:
也是脱离文档流, 基于浏览器定位,不管有没有父元素,都只基于浏览器