static 静态定位/常规定位/自然定位(默认的定位)
作用
使元素定位于常规/自然流中(块、行垂直排列下去、行内水平从左到右)
特点
- 忽略top,bottom,left,right,或者z-index声明
- 两个相邻的元素如果都设置了外边距,那么最终外边距=两者中外边距最大的
- 具有固定width和height值的元素,如果把左右外边距设置为auto,则左右外边距会自动扩大占满剩余宽度。造成的效果就是这个块水平居中
代码示例
忽略top、bottom等声明
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>自然定位</title>
<style>
* {
padding: 0;
margin: 0;
}
.static{
position: static;
width: 200px;
height: 200px;
text-align: center;
border: red 10px solid;
}
.static:ntn-child(2){
top:20px;
}
</style>
</head>
<body>
<div class="static" >1</div>
<div class="static">2</div>
<div class="static">3</div>
<div class="static">4</div>
</body>
</html>
效果,第二个div标签top声明无效
两个相邻元素同时设置外边距
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>自然定位</title>
<style>
* {
padding: 0;
margin: 0;
}
.static{
position: static;
width: 200px;
height: 200px;
text-align: center;
border: red 10px solid;
line-height: 50px;
box-sizing: border-box;
}
.static:nth-child(1){
margin: 30px;
}
.static:nth-child(2){
margin: 60px;
}
</style>
</head>
<body>
<div class="static">1</div>
<div class="static">2</div>
<div class="static">3</div>
<div class="static">4</div>
</body>
</html>
效果,第一块和第二块的间距为第二块的外边距值60px
元素水平居中
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>自然定位</title>
<style>
* {
padding: 0;
margin: 0;
}
.static{
position: static;
width: 200px;
height: 200px;
text-align: center;
border: red 10px solid;
line-height: 50px;
box-sizing: border-box;
}
.static:nth-child(1){
margin: auto;
}
</style>
</head>
<body>
<div class="static">1</div>
<div class="static">2</div>
<div class="static">3</div>
<div class="static">4</div>
</body>
</html>
效果,第一个div元素外边距设置auto,得到水平居中
relative 相对定位
作用
使元素成为containing-block-,即可定位的祖先元素(其他元素用作定位的参照物)
特点
- 可以使用top、right、bottom、left、z-index进行相对定位
- 相对定位的元素不会离开常规流
- 任何元素都可以设置为relative,它的绝对定位的后代都可以相对于它进行绝对定位
- 可以使浮动元素发生偏移,并控制它们的堆叠顺序
代码示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>相对定位</title>
<style>
* {
margin: 0;
padding: 0;
}
.block{
width: 200px;
height: 200px;
position: static;
border: red 10px solid;
}
.block:nth-child(2){
position: relative;
top: 50px;
border: blue 10px solid;
z-index: -1;
}
</style>
</head>
<body>
<div class="block">
1
</div>
<div class="block">
2
</div>
<div class="block">
3
</div>
<div class="block">
4
</div>
</body>
</html>
效果,第二个元素根据top发生偏移,且根据z-index改变了堆叠顺序,但原来常规流中的空间不会被填补,即元素不会脱离常规流
absolute 绝对定位
作用
使元素脱离常规流
特点
- 脱离常规流
- 设置尺寸要注意:百分比比的是谁?——最近定位祖先元素
- left/right/top/bottom若设置为0,它将对齐到最近定位祖先元素的各边
- left/right/top/bottom若设置为auto,它将恢复到常规流
- 如果没有最近定位祖先元素,会认< body >作为祖先元素
- z-index可以控制堆叠顺序
代码示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>绝对定位</title>
<style>
* {
margin: 0;
padding: 0;
}
.block{
width: 200px;
height: 200px;
border: #FF0000 10px solid;
}
.block:nth-child(2){
position: absolute;
right: 0;
bottom: 0;
width: 20%;
}
</style>
</head>
<body>
<div class="block">
1
</div>
<div class="block">
2
</div>
<div class="block">
3
</div>
<div class="block">
4
</div>
</body>
</html>
效果,第二个元素right与bottom设置为0,则与祖先元素右下两边对齐,此时祖先元素是body,所以在右下角,宽度为20%,即body的20%,绝对定位脱离了常规流,原空间被清除
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>绝对定位</title>
<style>
* {
margin: 0;
padding: 0;
}
.block{
width: 200px;
height: 200px;
border: #FF0000 10px solid;
box-sizing: border-box;
position: relative;
}
.child{
position: absolute;
width: 50%;
height: 50%;
border: #0000FF 5px solid;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto auto;
}
/* .block:nth-child(2){
position: absolute;
right: 0;
bottom: 0;
width: 20%;
} */
</style>
</head>
<body>
<div class="block">
1
</div>
<div class="block">
<div class="child">
2
</div>
</div>
<div class="block">
3
</div>
<div class="block">
4
</div>
</body>
</html>
效果,此时红边框div元素定位为relative,则它会变成附近的绝对定位的祖先元素,将left/right/top/bottom均设置为0且margin设置为auto时,外边距填充满空白区域从而实现内容居中
fixed 固定定位
作用
使元素脱离常规流
特点
- 具有绝对定位的特点
- 与绝对定位区别在于参照物不同,绝对定位以祖先元素作为参照物,固定定位以视口作为参照物
- 固定定位元素不会随着视口的滚动而滚动
代码示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>固定定位</title>
<style>
* {
margin: 0;
padding: 0;
}
body{
height: 1800px;
}
.block{
width: 200px;
height: 200px;
}
.block:nth-child(1){
position: absolute;
top: 20px;
left: 20px;
background-color: #0000FF;
}
.block:nth-child(2){
position: fixed;
top: 20px;
left: 20px;
background-color: red;
}
</style>
</head>
<body>
<div class="block">
固定
</div>
<div class="block">
绝对
</div>
</body>
</html>
效果,滚动条下拉但元素不会改变在视口中的位置
sticky 磁贴定位/粘性定位/吸附定位(CSS3新加)
作用
relative和fixed的完美结合,制造吸附效果
特点
- 如果产生偏移,原位置还是会在常规流中空出
- 如果它的最近祖先元素有滚动条,那么他的偏移标尺就是最近的祖先元素
- 如果最近祖先元素没有滚动,那么他的偏移标尺就是视口
- 上下左右的偏移规则
代码示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>磁贴定位</title>
<style>
.parent{
position: relative;
width: auto;
height: 500px;
background-color: antiquewhite;
overflow: scroll;
}
.child{
position: sticky;
width: auto;
height: 100px;
margin-top: 20px;
top: 0;
background-color: #0000FF;
}
</style>
</head>
<body>
<div class="parent">
<div class="child">
吸附
</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 />
</div>
</body>
</html>
效果,当元素根据top值捕捉到祖先元素时贴住祖先元素
inherit 继承定位
作用
继承父元素的定位属性
补充知识点
box-sizing 的使用
作用
允许以特定方式定义匹配某个区域的特定元素
属性值
- content-box:在宽度和高度之外绘制内边距和边框
- border-box:通过从已经设定的宽度和高度分别减去边框和内边距之后得到的值,才是内容的宽度和高度
- inherit:从父元素继承box-sizing的值
代码示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>box-sizing</title>
<style>
.border-box{
box-sizing: border-box;
background-color: antiquewhite;
width: 80px;
height: 80px;
border: red 1px solid;
padding: 20px;
}
.content-box{
box-sizing: content-box;
background-color: antiquewhite;
width: 80px;
height: 80px;
border: red 1px solid;
padding: 20px;
}
</style>
</head>
<body>
<div class="border-box">
border-box
</div>
<br>
<div class="content-box">
content-box
</div>
</body>
</html>
效果,border-box设置的宽高包含了内边距,增加内边距时会先占据盒子内容的空间,content-box设置的宽高仅为盒子内容的宽高,不包含内边距,增加内边距时会直接以盒子内容为界向外扩张
nth-child(n) 选择器
作用
匹配属于其父元素的第n个子元素,不论元素类型,其中n可以是数字、关键字、公式
z-index 属性
作用
设置元素的堆叠顺序,拥有更高堆叠顺序的总是位于拥有较低堆叠顺序的元素的前面
z-index可以有负值
z-index只能在具有position定位的元素上使用
属性值
- auto:默认,顺序与父元素相等
- inherit:继承父元素的z-index值
- number:数值越大离用户越近