absolute:生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定。
fixed:生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定。(可用于固定菜单)
relative:生成相对定位的元素,相对于其正常位置进行定位。
static:默认值,无定位。
inherit:承接父元素的属性。
以下为一个相对浏览器的绝对定位demo,代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>绝对/相对定位</title>
<style type="text/css">
body {
background-color: bisque;
}
div#all {
background-color: darkgrey;
color: white;
margin: auto;
width: 1300px;
}
div#all div {
float: left;
}
div#one,div#three {
background-color: dimgray;
width: 20%;
}
**div#fixed {
position: fixed;
background-color: dimgray;
width: 260px;
}**
div#two {
background-color: brown;
width: 60%;
height: 5000px;
}
</style>
<script type="text/javascript"></script>
</head>
<body>
<div id="all">
<div id="one">1
**<div id="fixed">相对浏览器窗口定位的盒子</div>**
</div>
<div id="two">2</div>
<div id="three">3</div>
</div>
</body>
</html>
其中出来的效果如下:
其中这个“相对浏览器窗口定位的盒子”,无论如何滑动滚轮都会被固定在这个位置。