当页面上发生了重叠时,使用定位。
1.固定定位:position:fiexd
相对于浏览器不占据原来的位置(脱离文档流)
<style>
#d1{
width: 200px;
height: 200px;
background-color: red;
position: fixed;
top: 300px;
left: 300px;
}
</style>
<body>
<div id="d1">我是第一个div</div>
</body>
效果图:
2.相对定位:position: relative;
相对定位不会脱离文档流 ,相对于它自己本身的位置
<style>
#d1{
width: 200px;
height: 200px;
background-color: red;
position:relative;
top: 300px;
left: 300px;
}
</style>
<body>
<div id="d1">我是第一个div</div>
</body>
3.绝对定位:position: absolute;
不占据原有位置(脱离文档流),相对于谁是会发生改变的 ,如果最近的祖先元素有定位属性就相对于该元素,若无就相对body