一、static定位
HTML 元素的默認值,即沒有定位,遵循正常的文檔流對象。
靜態定位的元素不會受到 top, bottom, left, right影響。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.c1 {
width: 100px;
height: 100px;
background: red;
position: static;
top: 100px;
left: 100px;
}
</style>
</head>
<body>
<div class="c1"></div>
</body>
</html>

二、relative定位
相對定位元素的定位是相對其左上頂點的正常位置進行定位,定位后元素還會占據原來的空間。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.c1,.c2,.c3 {
width: 100px;
height: 100px;
}
.c1 {
background: red;
position: relative;
left: 100px;
}
.c2 {
background: green;
}
.c3 {
background: blue;
}
</style>
</head>
<body>
<div class="c1"></div>
<div class="c2"></div>
<div class="c3"></div>
</body>
</html>

三、absolute定位
定位后,元素脫離文檔流,不再占據原來的空間。
絕對定位的元素的位置相對於最近的已定位祖先元素,如果元素沒有已定位的祖先元素,那么它的位置相對於<html>
1.父元素設置為relative定位,子元素設置absolute定位,相對其父元素進行定位。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.c1 {
width: 300px;
height: 300px;
background: red;
}
.c2 {
width: 200px;
height: 200px;
background: green;
position: relative;
}
.c3 {
width: 100px;
height: 100px;
background: blue;
position: absolute;
bottom: 0;
right: 0;
}
</style>
</head>
<body>
<div class="c1">
<div class="c2">
<div class="c3"></div>
</div>
</div>
</body>
</html>
2.祖先元素設置為relative定位,后代元素設置absolute定位,相對其祖先元素進行定位。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.c1 {
width: 300px;
height: 300px;
background: red;
position: relative;
}
.c2 {
width: 200px;
height: 200px;
background: green;
}
.c3 {
width: 100px;
height: 100px;
background: blue;
position: absolute;
bottom: 0;
right: 0;
}
</style>
</head>
<body>
<div class="c1">
<div class="c2">
<div class="c3"></div>
</div>
</div>
</body>
</html>

3.元素沒有已定位的祖先元素,它的位置相對於<html>。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
/*這里不清除body的外邊距,是為了能夠區分html和body元素*/
html,body {
width:100%;
height:100%;
}
html {
border: 3px solid yellow;
}
body {
border: 3px solid purple;
}
.c1 {
width: 300px;
height: 300px;
background: red;
}
.c2 {
width: 200px;
height: 200px;
background: green;
}
.c3 {
width: 100px;
height: 100px;
background: blue;
position: absolute;
bottom: 0;
right: 0;
}
</style>
</head>
<body>
<div class="c1">
<div class="c2">
<div class="c3"></div>
</div>
</div>
</body>
</html>

四、fixed定位
元素的位置相對於瀏覽器窗口是固定位置,即使窗口是滾動的它也不會移動。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.c1 {
width: 100px;
height: 100px;
background: red;
position: fixed;
bottom: 0;
right: 0;
}
</style>
</head>
<body>
<div class="c1"></div>
<!--這里用br元素產生滾動條-->
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</body>
</html>

