【发布时间】:2017-12-16 09:06:52
【问题描述】:
今天我有点意外,我有下面的 HTML 标记。
在我的 CSS 中,我将容器设置为 position: fixed; z-index: 3,然后我为 div#1 和 div#3 提供了一个 1 的 z-index 给 div#2 z-index: 2。我的期望是,当我将div#3 向上移动时,它将落后于div#2,但令我最惊讶的是,无论它的z-index 值或div#2 的值如何,它总是保持在它之上,我很困惑为什么会这样是,也许我并不像我想的那样理解堆叠上下文。有什么帮助吗?下面我有css。
* {
box-sizing: border-box;
}
body, html, .container {
height: 100%;
width: 100%;
}
.container {
position: fixed;
z-index: 300;
}
#div1, #div2, #div3 {
opacity: 0.7;
padding: 10px;
}
#div1 {
border: 1px dashed #996;
background-color: #ffc;
height: 33.333%;
z-index: 1;
}
#div2 {
border: 1px dashed #900;
background-color: #fdd;
height: 33.333%;
z-index: 2;
}
#div3 {
border: 1px dashed #696;
background-color: #cfc;
height: 33.333%;
z-index: 1;
transform: translateY(-40px)
}
<div class='container'>
<div id="div1">DIV#1 </div>
<div id="div2">DIV#2</div>
<div id="div3">DIV#3</div>
</div>
【问题讨论】: