网页显示其实是三维的,咱们直观看到的有x轴和y轴,但在网页布局上还有一个z轴。html
对于定位元素,咱们使用top、right、left、bottom来实现元素在x-y平面上的定位,但为了表示布局的三维立体概念,还引入了z-index,z-index属性用来设置元素的堆叠顺序,拥有更高堆叠顺序的元素老是会处于堆叠顺序较低的元素的上方。布局
<div style="width: 100px; height: 100px; background-color: red; position: relative;"></div> <div style="width: 100px; height: 100px; background-color: black; position: relative; top: -50px;"></div>
第二个div向上移动50px,与第一个div产生了重叠部分,显示状况以下:spa
第二个div显示在了第一个div上方,也就是重叠部分第二个div遮住了第一个div,如今给第一个div添加z-index属性:code
<div style="width: 100px; height: 100px; background-color: red; position: relative; z-index: 1;"></div> <div style="width: 100px; height: 100px; background-color: black; position: relative; top: -50px;"></div>
咱们将第一个div的z-index属性设置为1htm
这时咱们发现第一个div遮住了第二个div,下面咱们将逐步介绍定位元素属性z-index。blog
<div style="width: 100px; height: 100px; background-color: red; z-index: 5;"></div> <div style="width: 100px; height: 100px; background-color: black; position: relative; top: -50px; z-index: 1;"></div>
第一个div的z-index设置为5,第二个div的z-index设置为1继承
咱们看到第二个div覆盖了第一个div,虽然第一个div的z-index属性值更大,但它不是定位属性,因此它的z-index不起做用。文档
1. 两个都未设置z-index的定位元素get
<div style="width: 100px; height: 100px; background-color: red; position: relative;"></div> <div style="width: 100px; height: 100px; background-color: black; position: relative; top: -50px;"></div>
后写的这个div(背景为black)覆盖了先写的div(背景为red)博客
2. 两个非定位元素
<div style="width: 100px; height: 100px; background-color: red;"></div> <div style="width: 100px; height: 100px; background-color: black; margin-top: -50px;"></div>
后写的这个div(背景为black)覆盖了先写的div(背景为red)
第一个定位元素div的z-index为正数,第二个为非定位元素div,第一个定位元素div的z-index为负数
<div style="width: 100px; height: 100px; background-color: green; position: relative; top: 50px; z-index: 1"></div> <div style="width: 100px; height: 100px; background-color: red;"></div> <div style="width: 100px; height: 100px; background-color: black; position: relative; top: -50px; z-index: -1;"></div>
z-index为正数的定位元素覆盖了非定位元素,非定位元素覆盖了 z-index为负数的定位元素
<div style="width: 200px; height: 200px; background-color: green; position: relative; top: 50px; z-index: 100"> <div style="width: 100px; height: 100px; background-color: blue; position: relative; z-index: -10;"></div> </div>
虽然子div的z-index值小于其父div,但子div仍显示在了父div的上方
<div style="width: 200px; height: 200px; background-color: green; position: relative; top: 50px;"> <div style="width: 100px; height: 100px; background-color: blue; position: relative; z-index: -1;"></div> </div>
父元素未设置z-index,子元素z-index为-1,能够看到子元素被父元素覆盖了
<div style="width: 200px; height: 200px; background-color: green; position: relative; top: 50px;"> <div style="width: 100px; height: 100px; background-color: red; position: relative; z-index: 2;"></div> <div style="width: 100px; height: 100px; background-color: black; position: relative; z-index: 1; top: -50px;"></div> </div>
第一个子元素的z-index大于第二个子元素的z-index,因此第一个子元素覆盖第二个子元素
<div style="position: relative; z-index: 10;"> <div style="width: 100px; height: 100px; background-color: red; position: relative; z-index: 2;"></div> </div> <div style="position: relative; z-index: 5;"> <div style="width: 100px; height: 100px; background-color: black; position: relative; z-index: 3; top: -50px;"></div> </div>
尽管背景为红色的子div元素z-index小于背景为黑色的子div元素,但红色仍覆盖了黑色,就是由于红色div的父元素z-index大于黑色div的父元素
因此不管黑色子div的z-index多大,它的堆叠顺序始终小于红色div
感谢博主谦行的博客:z-index应用简单总结。
若有错误或不足之处还望指出探讨,十分感谢!