带有Z-index问题的固定模态

时间:2018-07-14 09:59:38

标签: css

我有一个z-index: 100的固定模式,但是它不在页面上所有元素的上方。它在position: relative; z-index: 2;下是一个元素,我不知道为什么?!这是fiddle。任何帮助,将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

您需要从纸张标题div中取出模式包装器div。 这是完整的代码。

.paper-header {
  align-items: flex-start;
  display: flex;
  justify-content: space-between;
  position: relative;
  z-index: 2;
  background: #ffffff;
  min-height: 8rem;
}

.paper-body {
  position:relative;
  z-index: 1;
}

.modal-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  z-index: 100;
  overflow-x: hidden;
  overflow-y: auto;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.modal {
  width: 32.5rem;
  background-color: #FFFFFF;
  border-radius: 4px;
  box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 101;
  padding: 1.25rem;
}

.modal-overlay {
  position: absolute;
  z-index: 100;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 1.25rem;
  background-color: rgba(34, 34, 34, 0.5)
}
<div class="wrapper">
  <div class="paper-header">
    <h1>
      Segment 1
    </h1>
    
  </div>
  <div class="modal-wrapper">
      <div class="modal">
      modal content  
      </div>
      <div class="modal-overlay"></div>
   </div>
  <div class="paper-body">
    Segment body
  </div>
  
  <div class="paper-header">
    <h2>
      Segment 2
    </h2>
  </div>
</div>

答案 1 :(得分:0)

实际上,当父元素的z-index差时,子元素就是其父元素的高位。

当您告诉'modal-wrapper'z index = 100 时,由于父元素为'paper-header'且父元素z-index为该元素,因此它不起作用是2。

所以,孩子高是2。

解决方案是

只需将同级元素设置为纸张标题,然后正常工作即可。

.paper-header {
  align-items: flex-start;
  display: flex;
  justify-content: space-between;
  position: relative;
  z-index: 2;
  background: #ffffff;
  min-height: 8rem;
}

.paper-body {
  position:relative;
  z-index: 1;
}

.modal-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  z-index: 100;
  overflow-x: hidden;
  overflow-y: auto;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.modal {
  width: 32.5rem;
  background-color: #FFFFFF;
  border-radius: 4px;
  box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 101;
  padding: 1.25rem;
}

.modal-overlay {
  position: absolute;
  z-index: 100;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 1.25rem;
  background-color: rgba(34, 34, 34, 0.5)
}
<div class="wrapper">
  <div class="paper-header">
    <h1>
      Segment 1
    </h1>
    
  </div>
  <div class="modal-wrapper">
    <div class="modal">
    modal content  
    </div>
    <div class="modal-overlay"></div>
  </div>
  <div class="paper-body">
    Segment body
  </div>
  
  <div class="paper-header">
    <h2>
      Segment 2
    </h2>
  </div>
</div>

----谢谢----