1.相对定位
1.1.相对定位,就是微调元素位置的。让元素相对自己原来的位置,进行位置调整。也就是说,如果一个盒子想进行位置调整,那么就要使用相对定位
a position:relative; → 必须先声明,自己要相对定位了
b left:100px; → 然后进行调整。
c top:150px; → 然后进行调整。
Documentdiv{
width: 200px;
height: 200px;
}
.box1{
background-color: yellowgreen;
}
.box2{
background-color: skyblue;
position: relative;
top: 150px;
left: 100px;
}
.box3{
background-color: orange;
}
图片.png
1.2.不脱标,老家留坑,形影分离,相对定位不脱标,真实位置是在老家,只不过影子出去了,可以到处飘。
图片.png
1.3.相对定位的用途:相对定位有坑,所以一般不用于做“压盖”效果。页面中,效果极小。就两个作用:
a:微调元素
b:做绝对定位的参考,子绝父相
1.4.相对定位的定位值
a:可以用left、right来描述盒子右、左的移动,相当于marginLeft ,marginRight
b:可以用top、bottom来描述盒子的下、上的移动,marginTop,marginBottom
图片.png
图片.png
图片.png
2.绝对定位
2.1.绝对定位的盒子,是脱离标准文档流的。所以,所有的标准文档流的性质,绝对定位之后都不遵守了。绝对定位之后,标签就不区分所谓的行内元素、块级元素了,不需要display:block;就可以设置宽、高了:
Document*{
margin: 0;
padding: 0;
}
div{
width: 200px;
height: 200px;
}
.box1{
background-color: yellowgreen;
}
.box2{
background-color: skyblue;
position: absolute;
top: