定位 = 定位模式 + 边偏移
1.模式
position : static (静态) | relative (相对) | absolute (绝对) | fixed (固定)
2.边偏移
top | bottom | left | right
1.静态定位static
默认定位方式,无定位的意思
2.相对定位relative
相对于它原来的位置进行偏移
特点:原来在标准流中的位置继续占有(不脱标)
未加定位:
<style>
div {
width: 200px;
height: 100px;
}
.one {
background-color: pink;
}
.two {
background-color: aquamarine;
}
.three {
background-color: blue;
}
</style>
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
给第二个元素添加定位
.two {
position: relative;
left: 100px;
top: 20px;
background-color: aquamarine;
}
可以看到,第三个蓝色元素并没有移动到上方,说明原来第二个元素的位置依然占有,且看绿色盒子的移动,top和left值都是相对于它原来在的位置。
3.绝对定位absolute
相对于它祖先元素来说
特点:
- 如果没有祖先元素或者祖先元素没有定位,则以浏览器为准(document文档)
- 如果祖先元素有定位(相对,绝对,固定),则以最近一级的有定位的祖先元素为参考点移动位置
- 绝对定位不再占有原先的位置(脱标)
将上述例子中第二个元素的定位改成绝对定位:
.two {
position: absolute;
left: 100px;
top: 20px;
background-color: aquamarine;
}
可以看到,蓝色方块上移,说明绝对定位不占有原来的位置,而根据绿色方块的移动,他是根据浏览器进行移动的。
如果父元素有定位:
<style>
.big {
position: absolute;
width: 200px;
height: 200px;
background-color: cornflowerblue;
}
.small {
position: absolute;
top: 30px;
left: 20px;
width: 50px;
height: 50px;
background-color: crimson;
}
</style>
<div class="big">
<div class="small"></div>
</div>
则相对于父元素移动
☆☆子绝父相的由来
因为父级元素需要占有位置,因此是相对定位,子盒子不需要占有位置,则是绝对定位
<style>
div {
width: 200px;
height: 100px;
}
.one {
background-color: pink;
}
.two {
position: relative;
background-color: aquamarine;
}
.three {
background-color: blue;
}
.small {
position: absolute;
top: 20px;
left: 20px;
width: 20px;
height: 20px;
background-color: crimson;
}
</style>
<div class="one"></div>
<div class="two">
<div class="small"></div>
</div>
<div class="three"></div>
像这样,父级元素要按照正常次序摆放,保留自己的位置,因此只能用相对定位,以保证不会影响后面的元素,而子元素需要在父元素中放到一定的位置,且不需要占有原来的位置,因此可以选择绝对定位。
4.固定定位fixed
页面滚动时元素的位置不会改变,是相对于浏览器的可视窗口
特点:
- 跟父元素没有任何关系,不随滚动条滚动
- 固定定位不占有原先的位置(脱标)
.right {
position: fixed;
top: 100px;
right: 10px;
width: 50px;
height: 100px;
background-color: darkmagenta;
}
<div class="right"></div>
可以看到,右边滚动条移动,紫色盒子距离上方横线的距离并不会改变
5.粘性定位sticky
特点:
- 以浏览器可视窗口为参照点
- 占有原先的位置
- 必须添加top|bottom|left|right中的一个才生效
<!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>Document</title>
<style>
div {
width: 200px;
height: 400px;
}
.one {
background-color: pink;
}
.two {
/* position: relative; */
background-color: aquamarine;
}
.three {
background-color: blue;
}
.right {
position: sticky;
top: 100px;
width: 50px;
height: 100px;
background-color: darkmagenta;
}
</style>
</head>
<body>
<div class="one"></div>
<div class="right"></div>
<div class="two"></div>
<div class="three"></div>
</body>
</html>
设置的top为100px,在紫色盒子的顶部距离可视页面顶部距离大于100px时,按照正常位置的顺序排列,而当滚动页面到距离小于100px时,紫色盒子会始终和顶部保持100px的距离,类似绝对定位,但是他原来的位置会保留,可以看到青色盒子并没有上移。
6.定位叠放次序 z-index
- 数值越大,越靠上
- 如果值相同,则按照书写顺序
- 只有定位的盒子才有该属性
<style>
div {
position: absolute;
width: 200px;
height: 400px;
}
.one {
background-color: pink;
}
.two {
background-color: aquamarine;
}
.three {
background-color: blue;
}
</style>
最开始的例子,给三个盒子都添加绝对定位,因为绝对定位不占有位置,所以三个盒子会层叠在一起,并只能看到第三个:
现在给第一个元素添加z-index
.one {
z-index: 1;
background-color: pink;
}
可以看到第一个元素会覆盖其他两个:
当然,若是第二个设置z-index更大,则第二个元素就会覆盖其他两个