1、相對定位概述
如果對一個元素進行相對定位,它將出現(xiàn)在它所在的位置上。然后,可以通過設置垂直或水平位置,讓這個元素“相對于”它的起點進行移動。
(1)相對定位 元素不會脫離文檔流
(2)相對定位 是相對元素,在正常文檔流中的原始位置
2、效果演示
源代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS-定位-相對定位</title>
<style type="text/css">
*{
margin: 0px;
padding: 0px;
}
.wrap{
width : 800px;
background: gray;
margin: 0px auto;
}
.div1{
border: 1px;
background: blue;
height: 100px;
}
.div2{
width: 700px;
background: red;
height: 100px;
position: relative;
top : -20px;
}
.div3{
background: green;
height: 100px;
}
</style>
</head>
<body>
<div class="wrap">
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
</div>
</body>
</html>
效果如下,紅色的內(nèi)容是相對布局,因為浮動不會脫離文檔流,仍然占據(jù)原來的位置,所以綠色的塊不會向上移動。

image.png