参考:https://www.cnblogs.com/guolao/p/9048308.html
static 定位
HTML 元素的默认值,即没有定位,遵循正常的文档流对象。
静态定位的元素不会受到 top, bottom, left, right影响。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS-position-static</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
<style>
.container{
background-color: #868686;
width: 100%;
height: 300px;
}
.content{
background-color: yellow;
width: 100px;
height: 100px;
position: static;
left: 10px;/* 这个left没有起作用 */
}
</style>
</head>
<body>
<div class="container">
<div class="content">
</div>
</div>
</body>
</html>
对 content 的 position 设定 static 后,left就失效了,而元素(content)就以正常的 normal flow 形式呈现。
relative定位
个人理解:相对于normal flow中的原位置来定位。
<!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>CSS-position-relative</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
<style>
.container{
background-color: #868686;
width: 100%;
height: 300px;
}
.content_0{
background-color: yellow;
width: 100px;
height: 100px;
}
.content_1{
background-color: red;
width: 100px;
height: 100px;
position: relative;/* 这里使用了relative */
}
.content_2{
background-color: black;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="container">
<div class="content_0">
</div>
<div class="content_1">
</div>
<div class="content_2">
</div>
</div>
</body>
</html>
这是没有设置left、top等属性时,正常出现在normal flow中的位置。
接着添加left、top:
.content_1{
background-color: red;
width: 100px;
height: 100px;
position: relative;/* 这里使用了relative */
left: 20px;/* 这里设置了left和top */
top: 20px;
}
可以看到,元素(content_1)的位置相对于其原位置(normal flow中的正常位置)进行了移动。
absolute定位
个人理解:生成绝对定位的元素,其相对于 static 定位以外的第一个父元素进行定位,会脱离normal flow。注意:是除了static外
<!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>CSS-position-static</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
<style>
.container{
background-color: #868686;
width: 100%;
height: 300px;
margin-top: 50px;
}
.content{
background-color: red;
width: 100px;
height: 100px;
position: absolute;
top: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
</div>
</div>
</body>
</html>
因为 content 的父元素 container 没有设置 position,默认为 static,所以找到的第一个父元素是 body(<body></body>),可以看成是元素(content)相对于 body 向下移动10px。
fixed定位
个人理解:fixed相对于浏览器窗口固定,滚动浏览器窗口并不会使其移动,会脱离normal flow。
<!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>CSS-position-static</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
<style>
.container{
background-color: #868686;
width: 100%;
height: 1000px;
}
.content{
background-color: yellow;
width: 100px;
height: 100px;
position: fixed;/* 这里使用了fixed */
}
</style>
</head>
<body>
<div class="container">
<div class="content">
</div>
</div>
</body>
</html>
sticky定位
(了解)盒位置根据正常流计算(这称为正常流动中的位置),然后相对于该元素在流中的 flow root(BFC)和 containing block(最近的块级祖先元素)定位。在所有情况下(即便被定位元素为 table时
),该元素定位均不对后续元素造成影响。当元素 B 被粘性定位时,后续元素的位置仍按照 B 未定位时的位置来确定。position: sticky对 table元素的效果与 position: relative 相同。
inherit定位
规定应该从父元素继承 position 属性的值。
inherit 继承父元素,这个用得不多,所以也不继续深入了。