简体   繁体   English

:after中的z-index如何工作

[英]How does the z-index in :after work

I have this nice css3 post-it ( JSFiddle ). 我有这个漂亮的css3 post-it( JSFiddle )。 However, in my application they can sometimes be on top of each other, like this: JSFiddle1 但是,在我的应用程序中,它们有时可以彼此重叠 ,如下所示: JSFiddle1

As you can see, the shadow of the post-it is gone (it's behind the other one). 如您所见,便利贴的阴影消失了(它在另一个的后面)。 So, for the post-it on top, I added the following styles (I increased the z-index ): 因此,对于顶部贴子,我添加了以下样式(增加了z-index ):

.on-top {
    z-index: 11;
}
.on-top:after {
    z-index: 10;
}

Checkout this JSFiddle2 for the result. 检出此JSFiddle2以获取结果。 For some reason the shadow :after part is now on top of the post-it. 由于某种原因,阴影:after部分现在位于便利贴的顶部。 It seems that the z-index: 11 doesn't work. 似乎z-index: 11不起作用。 Any suggestions? 有什么建议么?

When using z-index , you create a layering context. 使用z-index ,您将创建一个分层上下文。 All other z-index es inside that context are resolved with respect to each other, and then everything in that container is treated as a single "layer" with the container's z-index . 该上下文内的所有其他z-index相对于彼此解析,然后将该容器中的所有内容与该容器的z-index视为单个“层”。

Try applying z-index:-1 to the shadow. 尝试将z-index:-1应用于阴影。 It seems counter-intuitive, but it will now appear behind the container element, but the whole "container with its shadow" will be applied with the z-index:10 of that parent. 似乎违反直觉,但它现在将出现在容器元素的后面,但是整个“带有阴影的容器”将与该父对象的z-index:10一起应用。

In order for the z-index property to work, you must specify one of the following additional properties within the same set of curly brackets {}: 为了使z-index属性起作用, 必须在同一组大括号{}中指定以下其他属性之一:

position: fixed;
position: absolute;
position: relative;

See the Spec Wiki 参见规格Wiki

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

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