是否可以在其父元素后面有一个具有 z-index 的子元素

发布于 2024-09-17 01:07:31 字数 74 浏览 8 评论 0原文

我想知道是否可以在其父元素后面有一个具有 z-index 的子元素。

我想使用父 div 作为其内容之上的透明颜色层。

I would like to know if it possible to have a child element behind his parent element with z-index.

I would like to use the parent div as transparent color layer on top of his content.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

高冷爸爸 2024-09-24 01:07:31

为什么不呢?当然可以,而且很简单:

  1. 为您想要的元素提供非静态位置;
  2. 将子级的 z-index 设置为 -1
  3. 在主容器上创建一个堆叠上下文(通过在其上设置
    z-indexopacitytransforms 或其他生成合成的内容
    层)。
.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>

(如果父级用作透明层,请务必使用 background-image 或 rgba background-color:子级继承 opacity父母的)

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;
  3. create a stacking context on the main container (by setting on it a
    z-index, opacity, transforms or whatelse generates a composite
    layer).

.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)

迟到的我 2024-09-24 01:07:31

简短的回答是肯定的;)
这里有一篇很棒的文章,描述了如何使用元素的堆叠顺序来允许 z-index 为负数,以便将元素放置在其父元素后面。

http://philipwalton.com/articles/what-没有人告诉你关于 z-index/

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.

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

不即不离 2024-09-24 01:07:31

不可能,因为每个定位元素都会创建一个堆叠上下文。

说明 1说明 2

Not possible, because each positioned element creates a stacking context.

Explanation 1, Explanation 2

薄暮涼年 2024-09-24 01:07:31

你可以用另一种方式来做,并使用子元素作为覆盖层,就像这样的

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

#stuff{
    position:relative;
    }

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

You could just do it the other way and use the child as the overlay like this

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

#stuff{
    position:relative;
    }

.overlay{
    width:100%;
    height:100%;
    position:absolute;
    top:0;
    left:0;
    background:#ACA;
    opacity:0.4;
    }
末蓝 2024-09-24 01:07:31

虽然这不一定适用于所有浏览器(尤其是较旧的浏览器),但以下内容过去对我有用:

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

我实际上只是建议将此作为最后的手段,并且仍然更愿意使用除此之外的任何技术,尽管它可能是您的情况的理想选择。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文