1. 静态定位 static
静态定位是元素的默认定位方式,无定位的意思。
语法:
选择器 { position: static;}
- 静态定位按照标准特性摆放位置,它没有边偏移
- 静态定位在布局时很少用到
2. 相对定位 relative
相对定位是元素在移动位置的时候,是相对于它原来的位置来说的。
语法:
选择器 { position: relative;}
- 它是相对于自己原来的位置来移动 (移动位置的时候参照点是原来的位置)
- 原来在标准流的位置继续占用,后面的盒子仍然以标准流的方式对待它 (不脱标,继续保留原来位置)
3. 绝对定位 absolute
绝对定位是元素在移动位置的时候,是相对于它祖先元素来说的。
语法:
选择器 { position: absolute;}
- 如果没有祖先元素或者祖先元素没有定位,则以浏览器为准定位 (Document文档)。
- 如果祖先元素有定位(相对、绝对、固定定位),则以最近一级的有定位祖先元素为参考点移动位置。
- 绝对定位不再占有原先的位置。(脱标)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>定位</title>
<style>
.father {
width: 500px;
height: 500px;
background-color: skyblue;
}
.son {
position: absolute;
left: 0;
bottom: 0;
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
在上述父级盒子中加入定位position: relative;
再加入一个父级盒子
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>定位</title>
<style>
.yeye {
position: relative;
width: 800px;
height: 800px;
background-color: yellowgreen;
}
.father {
width: 500px;
height: 500px;
background-color: skyblue;
}
.son {
position: absolute;
left: 0;
bottom: 0;
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div class="yeye">
<div class="father">
<div class="son"></div>
</div>
</div>
</body>
</html>
子级是绝对定位的话,父级要用相对定位
1.子级绝对定位,不会占有位置,可以放到父盒子里面的任何一个地方,不会影响其他的兄弟盒子。
2.父盒子需要加定位限制子盒子再父盒子内显示。
3.父盒子布局时,需要占有位置,因此父亲只能是相对定位。
4.固定定位 fixed
固定定位是元素固定于浏览器可视区的位置。主要应用场景:可以在浏览器页面滚动时元素的位置不会改变。
语法:
选择器 { position: fixed;}
- 以浏览器的可视窗口为参照点移动元素
- 跟父元素没有任何关系
- 不随滚动条滚动
- 固定定位不在占有原先的位置
- 固定定位也是脱标的,其实固定定位也可以看做是一种特殊的绝对定位
固定在版心右侧位置
1.让固定定位的盒子left:50%,走到浏览器可视区(也可以看做版心)的一般位置。
2.让固定定位的盒子margin-left:版心宽度的一半距离。多走版心宽度的一半位置就可以让固定定位的盒子贴着版心右侧对齐了。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>定位</title>
<style>
.w {
width: 800px;
height: 1400px;
background-color: pink;
margin: 0 auto;
}
.fixed {
position: fixed;
/*走浏览器宽度的一半*/
left: 50%;
/*利用margin 走版心盒子宽度的一半距离*/
margin-left: 400px;
width: 50px;
height: 150px;
background-color: skyblue;
}
</style>
</head>
<body>
<div class="fixed"></div>
<div class="w">版心盒子</div>
</body>
</html>
5. 粘性定位 sticky
粘性定位可以被认为是相对定位和固定定位的混合。
语法:
选择器 { position: sticky; top: 10px;}
- 以浏览器的可视窗口为参照点移动元素(固定定位特点)
- 粘性定位占有原先的位置(相对定位特点)
- 必须添加top、left、right、bottom其中一个才有效
跟页面滚动搭配使用,兼容性差,IE不支持。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>定位</title>
<style>
body {
height: 3000px;
}
.w {
position: sticky;
top: 0;
width: 800px;
height: 50px;
background-color: pink;
margin: 100px auto;
}
</style>
</head>
<body>
<div class="w">2022</div>
</body>
</html>