固定div上的z-index堆叠似乎不起作用

时间:2016-03-30 12:43:01

标签: html css z-index

我无法理解为什么这个z-index在我的页面上不起作用。

以下是一个工作示例:https://jsfiddle.net/daeyqv31/

#login_modal_wrapper {
  background: #eee;
  background: red;/*rgba(0,0,0,0.7);*/
  width: 100%; height: 100%;
  position: fixed; top: 0; left: 0;
  z-index: 10003;
}

#login_modal {
  z-index: 10002;
  background: #eee;
  border: 2px solid #092834;
  border: 2px solid rgba(9,40,52,0.6);
  border-radius: 20px;
  width: 700px;
  height: 240px;
  position: fixed;
  top: 50%;
  left: 50%;
  margin-left: -350px;
  margin-top: -120px;
}
#login_modal_wrapper.over {
    z-index: 100003; // this needs to go OVER login_modal
}
#ajax_spinner {
  width: 34px;
  height: 34px;
  position: fixed;
  top: 50%;
  left: 50%;
  margin-left: -17px;
  margin-top: -17px;
  z-index: 200000;
}

然而,我还希望能够强制#login_modal_wrapper div去 OVER #login_modal。所以,我有这个(只是一个基本的例子);

https://jsfiddle.net/daeyqv31/1/

所以这个:

<div id="ajax_spinner">
    loading icon
</div>
<div id="login_modal_wrapper">
  <div id="login_modal"></div>
</div>

变为:

<div id="ajax_spinner">
    loading icon
</div>
<div id="login_modal_wrapper" class="over">
  <div id="login_modal"></div>
</div>

...但z-index似乎没有被尊重(它仍然显示在{1}}顶部,而它实际上应该落后于#login_modal

更新如下面所述,无法将更高 z-index应用于父级div。为了解决这个问题,我只是略微摇晃了一下HTML:

#login_modal_wrapper

这现在完全符合预期:)

1 个答案:

答案 0 :(得分:2)

由于#login_modal_wrapper包含其他<div>元素,z-index无效。你可以尝试:

<div id="login_modal_wrapper">
  <div class="div-above"></div>
  <div id="ajax_spinner">
    loading icon
  </div>
  <div id="login_modal"></div>
</div>

CSS:

.div-above {
  width: 100%;
  height: 100%;
  background: blue;
  position:fixed;
  z-index: 10;
}

这是updated fiddle

相关问题