z-index和嵌套DOM元素(堆叠上下文)overlay

时间:2018-03-01 22:26:52

标签: html css css3 layout z-index

如何制作 div3 & div4 高于div1和div2而不使用" ID" CSS级别的选择器?

enter image description here



.container {
    margin: 50px;
    padding: 20px;
    text-align: center;
    border: 1px dashed #999966;
}

.position {
  height: 120px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  margin-bottom: 10px;
  opacity: 0.8;
  border: 1px dashed #999966;
  background-color: #ffffcc;
}

.position .position {
  margin-top: 60px;
  margin-left: 100px;
  width: 50%;
}

.relative {
  position: relative;
  border: 1px dashed #669966;
  background-color: #ccffcc;
 }
 
.fixed {
  position: fixed;
  border: 1px dashed #669966;
  background-color: #e3e3ff;
  align-items: flex-start;
 }
 
.fixed.target-block {
  width: 200px;
  height: 300px;
  top: 130px;
  right: 140px;
  z-index: 999;
 }
 

<div class="container">
  <p>
    DIV: CONTAINER<br>
    position: relative;
  </p>

  <div class="position">
    <p>
      DIV #1<br>
      no position;
    </p>
    <div class="position relative">
      <p>
        DIV #3<br>
        position: relative;<br>
        parent
      </p>
      <div class="position fixed target-block">
        <p>
          DIV #4<br>
          position: fixed;<br>
          child
        </p>
      </div>
    </div>
  </div>
  
  <div class="position">
    <p>
      DIV #2<br>
      no position;
    </p>
  </div>
  
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

如果我们参考specification,我们可以阅读:

  
      
  1. 所有定位,不透明度或变换后代,按树顺序   分为以下几类:   ...

         
        
    1. 所有不透明度后代的不透明度小于1,按树顺序,   创建以原子方式生成的堆叠上下文。
    2.   
  2.   

在你的情况下,Div#2会创建它自己的堆叠上下文,比如Div#1,因为它们都不是用z-index定位的,所以只需控制它们的子元素就无法达到你想要的效果。

所以我在这里看到的唯一方法是让其中一个div定位并将z-index应用于它,或者两者都使用不同的z-index定位。目的是避免像上面指定的遵循树顺序

.container {
  margin: 50px;
  padding: 20px;
  text-align: center;
  border: 1px dashed #999966;
}

.position {
  height: 120px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  margin-bottom: 10px;
  opacity: 0.8;
  border: 1px dashed #999966;
  background-color: #ffffcc;
}

.position .position {
  margin-top: 60px;
  margin-left: 100px;
  width: 50%;
}

.relative {
  position: relative;
  border: 1px dashed #669966;
  background-color: #ccffcc;
}

.fixed {
  position: fixed;
  border: 1px dashed #669966;
  background-color: #e3e3ff;
  align-items: flex-start;
}

.fixed.target-block {
  width: 200px;
  height: 300px;
  top: 130px;
  right: 140px;
  z-index: 999;
}
<div class="container">
  <p>
    DIV: CONTAINER<br> position: relative;
  </p>

  <div class="position" style="position:relative;z-index:1;">
    <p>
      DIV #1<br> no position;
    </p>
    <div class="position relative">
      <p>
        DIV #3<br> position: relative;<br> parent
      </p>
      <div class="position fixed target-block">
        <p>
          DIV #4<br> position: fixed;<br> child
        </p>
      </div>
    </div>
  </div>

  <div class="position">
    <p>
      DIV #2<br> no position;
    </p>
  </div>

</div>

一些相关问题:

Stacking order of elements affected by opacity

What has bigger priority: opacity or z-index in browsers?