【发布时间】:2017-01-21 00:45:23
【问题描述】:
我的理解是z-index元素是only stacked with their siblings。我也读过 z-index "doesn't work" with position: fixed。但是,我将两者一起使用,并且恰好按照我的意愿进行;我想知道为什么。
我有一个阻塞 div,它需要覆盖页面中除一个元素之外的所有内容。该元素不是阻止程序的兄弟,我认为这会使它变得不可能,但它起作用了。这里利用了什么规则,我如何预测它是否可以在其他浏览器中工作?
.top {
background-color: yellow;
}
.yes {
position: relative;
z-index: 10;
background-color: blue;
}
.no {
position: relative;
z-index: 1;
background-color: red;
}
.blocker {
position: fixed;
z-index: 5;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .7);
}
<div class="top">
<button class="yes">
Yes
</button>
<button class="no">
No
</button>
</div>
<div class="blocker">
</div>
【问题讨论】:
-
你的理解有误。带有
position: fixed的元素确实适用于z-index。这可能对您有用:Basics of thez-indexproperty