层叠上下文可以包含在其他层叠上下文中,并且一起组建了一个有层级的层叠上下文
每个层叠上下文完全独立于它的兄弟元素,当处理层叠时只考虑子元素,这里类似于BFC
每个层叠上下文是自包含的:当元素的内容发生层叠后,整个该元素将会在父级叠上下文中按顺序进行层叠
<span class="red">Red</span>
<span class="green">Green</span>
<span class="blue">Blue</span>
.red, .green, .blue {
position: absolute;
width: 100px;
color: white;
line-height: 100px;
text-align: center;
}
.red {
/* z-index: 1; */
top: 20px;
left: 20px;
background: red;
}
.green {
top: 60px;
left: 60px;
background: green;
}
.blue {
top: 100px;
left: 100px;
background: blue;
}
<div class='first-box'>
<span class="red">Red</span>
<span class="green">Green</span>
</div>
<div class='second-box'>
<span class="blue">Blue</span>
</div>
.first-box{
// z-index:1; /* 父元素的层叠上下文一样 */
}
.second-box{
// z-index:0; /* 父元素的层叠上下文一样 */
}
div{
position: relative;
}
.red,
.green,
.blue {
position: absolute; /* 都是定位元素 */
width: 100px;
color: white;
line-height: 100px;
text-align: center;
}
.red {
top: 20px;
left: 20px;
background: red;
// z-index:3;
}
.green {
top: 60px;
left: 60px;
background: green;
// z-index:2;
}
.blue {
top: 100px;
left: 100px;
background: blue;
// z-index:1;
}
<div class='first-box'>
<span class="red">Red</span>
<span class="green">Green</span>
</div>
<div class='second-box'>
<span class="blue">Blue</span>
<span class="gold">gold</span>
</div>
div{
position: relative;
}
.red, .green, .blue, .gold {
position: absolute;
width: 100px;
color: white;
line-height: 100px;
text-align: center;
}
.red {
top: 20px;
left: 20px;
background: red;
}
.green {
z-index: 1; /* 设置了正的z-index */
top: 60px;
left: 60px;
background: green;
}
.blue {
top: 100px;
left: 100px;
background: blue;
}
.gold{
z-index: -1; /* 设置了负的z-index */
width: 140px;
line-height: 140px;
top: 40px;
left: 40px;
background: gold;
}
<div class='first-box'>
<span class="red">Red</span>
<span class="green">Green</span>
</div>
<div class='second-box'>
<span class="blue">Blue</span>
</div>
.first-box {
position: relative;
z-index: 1; /* 父元素的层叠上下文不一样 */
}
.second-box {
position: relative;
z-index: 0; /* 父元素的层叠上下文不一样 */
}
.red,
.green,
.blue {
position: absolute;
width: 100px;
color: white;
line-height: 100px;
text-align: center;
}
.red {
top: 20px;
left: 20px;
background: red;
z-index: 10;
}
.green {
top: 60px;
left: 60px;
background: green;
z-index: 20;
}
.blue {
top: 100px;
left: 100px;
background: blue;
z-index: 999; /* 设置
<div class='first-box'>
<span class="red">Red</span>
<span class="green">Green</span>
</div>
<div class='second-box'>
<span class="blue">Blue</span>
<span class="gold">gold</span>
</div>
.first-box{
opacity: .99;
}
div{
position: relative;
}
.red, .green, .blue, .gold {
position: absolute;
width: 100px;
color: white;
line-height: 100px;
text-align: center;
}
.red {
top: 20px;
left: 20px;
background: red;
z-index: 999; /* 设置了很大的z-index */
}
.green {
top: 60px;
left: 60px;
background: green;
z-index: 999; /* 设置了很大的z-index */
}
.blue {
top: 100px;
left: 100px;
background: blue;
}
.gold{
z-index: -1; /* 设置了负的z-index */
width: 140px;
line-height: 140px;
top: 40px;
left: 40px;
background: gold;
}