层叠上下文
层叠上下文 (Stacking Context)

如何形成层叠上下文
层叠顺序

demo
普通情况

在相同层叠上下文的父元素内的情况

给子元素增加 z-index

在不同层叠上下文的父元素内的情况

给子元素设置 opacity

Last updated







Last updated
<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;
}