1.什么是z-index属性?
默认情况下所有的元素都有一个默认的z-index属性, 取值是0.
z-index属性的作用是专门用于控制定位流元素的覆盖关系的
1.默认情况下定位流的元素会盖住标准流的元素(定位流元素脱标,他后面的往上顶,然后根据定位属性排版)

2.默认情况下定位流的元素后面编写的会盖住前面编写的(如果全是定位流)

3.如果定位流的元素设置了z-index属性, 那么谁的z-index属性比较大, 谁就会显示在上面(在网页中,有些元素往上滚动时会覆盖导航条,只要设置导航条的z-index大于这个元素)


注意点:
1.从父现象
1.1如果两个元素的父元素都没有设置z-index属性, 那么谁的z-index属性比较大谁就显示在上面
1.2如果两个元素的父元素设置了z-index属性, 那么子元素的z-index属性就会失效, 也就是说谁的父元素的z-index属性比较大谁就会显示在上面
*{
margin: 0;
padding: 0;
}
div{
width: 100px;
height: 100px;
}
.father1{
width: 200px;
height: 200px;
background-color: red;
position: relative;
z-index: 2;
}
.father2{
width: 200px;
height: 200px;
background-color: green;
position: relative;
z-index: 3;
}
.son1{
width: 100px;
height: 100px;
background-color: blue;
position: absolute;
left: 200px;
top: 200px;
z-index: 1;
}
.son2{
width: 100px;
height: 100px;
background-color: yellow;
position: absolute;
left: 250px;
top: 50px;
z-index: 2;
}
<div class="father1">
<div class="son1"></div>
</div>
<div class="father2">
<div class="son2"></div>
</div>
网友评论