简体   繁体   English

是否可以使用z-index在其父元素后面添加子元素

[英]Is it possible to have a child element behind his parent element with z-index

I would like to know if it possible to have a child element behind his parent element with z-index. 我想知道是否可以在z-index的父元素后面有一个子元素。

I would like to use the parent div as transparent color layer on top of his content. 我想在他的内容之上使用父div作为透明颜色层。

Why not? 为什么不? Sure you can, and it's easy: 当然可以,而且很简单:

  1. give a non-static position to your desired elements; 给你想要的元素一个非静态的位置;
  2. set z-index of child to -1 ; 将子的z-index设置为-1 ;
  3. create a stacking context on the main container (by setting on it a z-index , opacity , transforms or whatelse generates a composite layer). 在主容器上创建堆叠上下文(通过在其上设置z-indexopacitytransforms或whatelse生成复合层)。

 .container { position: absolute; z-index: 0; /* or eg. opacity: 0.99;*/ background-color: blue; color: lightblue; width: 100%; height: 100%; text-align: center; } .parent { position: relative; background-color: rgba(100, 255, 150, 0.75); color: green; width: 50%; height: 30%; top: 30%; left: 20%; } .child { position: absolute; z-index: -1; background-color: orange; color: yellow; width: 100%; height: 100%; top: -50%; left: 20%; } 
 <div class="container"> <span>container</span> <div class="parent"> <span>parent</span> <div class="child"> <span>child</span> </div> </div> </div> 

(if the parent is used as a transparent layer, be sure to use a background-image or rgba background-color : children inherit the opacity of the parent) (如果父级用作透明层,请务必使用background-image或rgba background-color :子级继承父级的opacity

The short answer is Yes ;) There is an excellent article here that describes how you can use the stacking order of elements to allow the z-index to be negative in order to place an element behind it's parent. 简短的回答是Yes;)这里有一篇很好的文章描述了如何使用元素的堆叠顺序来允许z-index为负数,以便将元素放在它的父元素后面。

http://philipwalton.com/articles/what-no-one-told-you-about-z-index/ http://philipwalton.com/articles/what-no-one-told-you-about-z-index/

While this wouldn't necessarily work in all browsers (especially older ones), the following has worked for me in the past: 虽然这不一定适用于所有浏览器(特别是旧版本),但以下对我有用:

#child {
  position: relative;
  z-index: -1;
  ...
}

I'm really only suggesting this as a last resort and would still prefer to use any technique other than this, although it might be ideal in your scenario. 我真的只是建议这作为最后的手段,并且仍然倾向于使用除此之外的任何技术,尽管它可能在您的场景中是理想的。

Not possible, because each positioned element creates a stacking context. 不可能,因为每个定位元素都会创建堆叠上下文。

Explanation 1 , Explanation 2 说明1解释2

You could just do it the other way and use the child as the overlay like this 您可以按照其他方式执行此操作,并将子项用作此类叠加层

HTML HTML

<div id="stuff"><div class="overlay"></div>
    <p>
    Cras venenatis ornare tincidunt. Nam laoreet ante sed nibh pretium nec gravida turpis dapibus. Curabitur lobortis; lacus sit amet rutrum aliquet, est massa feugiat lectus, bibendum eleifend velit metus vitae dolor! Duis vulputate mi vitae quam fermentum pharetra.
    </p>
</div>

CSS CSS

#stuff{
    position:relative;
    }

.overlay{
    width:100%;
    height:100%;
    position:absolute;
    top:0;
    left:0;
    background:#ACA;
    opacity:0.4;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM