我试图用:后伪元素CSS选择器样式一个元素
#element {
position: relative;
z-index: 1;
}
#element::after {
position:relative;
z-index: 0;
content: " ";
position: absolute;
width: 100px;
height: 100px;
}
似乎::后面的元素不能比元素本身更低。
有没有办法让伪元素低于元素本身?
这里有两个问题:
The CSS 2.1 specification states that "The :beforeand :after pseudo-elements elements interact with other boxes, such as run-in boxes, as if they were real elements inserted just inside their associated element." Given the way z-indexes are implemented in most browsers, it's pretty difficult (read, I don't know of a way) to move content lower than the z-index of their parent element in the DOM that works in all browsers.
Number 1 above does not necessarily mean it's impossible, but the second impediment to it is actually worse: Ultimately it's a matter of browser support. Firefox didn't support positioning of generated content at all until FF3.6. Who knows about browsers like IE. So even if you can find a hack to make it work in one browser, it's very likely it will only work in that browser.
我能想到的唯一一件事是跨浏览器工作是使用javascript插入元素,而不是CSS。我知道这不是一个很好的解决方案,但是:before和:after伪选择器看起来并不能解决这个问题。
我知道这是一个旧的帖子,但我觉得有必要张贴适当的答案。这个问题的实际答案是,您需要在带有伪元素的元素的父元素上创建一个新的堆叠上下文(并且您实际上必须给它一个z-index,而不仅仅是一个位置)。
是这样的:
#parent {
position: relative;
z-index: 1;
}
#pseudo-parent {
position: absolute;
/* no z-index allowed */
}
#pseudo-parent:after {
position: absolute;
top:0;
z-index: -1;
}
#父{位置:相对;z - index: 1;}
#伪父{位置:绝对;} /*不需要z-index */
#伪父:后{位置:绝对;z - index: 1;}
/*示例样式来说明*/
#伪父{背景:#d1d1d1;}
#伪parent:after {margin-left: -3px;内容:M}
< div id = "父" >
< div id = " pseudo-parent " >
,
< / div >
< / div >
这里有两个问题:
The CSS 2.1 specification states that "The :beforeand :after pseudo-elements elements interact with other boxes, such as run-in boxes, as if they were real elements inserted just inside their associated element." Given the way z-indexes are implemented in most browsers, it's pretty difficult (read, I don't know of a way) to move content lower than the z-index of their parent element in the DOM that works in all browsers.
Number 1 above does not necessarily mean it's impossible, but the second impediment to it is actually worse: Ultimately it's a matter of browser support. Firefox didn't support positioning of generated content at all until FF3.6. Who knows about browsers like IE. So even if you can find a hack to make it work in one browser, it's very likely it will only work in that browser.
我能想到的唯一一件事是跨浏览器工作是使用javascript插入元素,而不是CSS。我知道这不是一个很好的解决方案,但是:before和:after伪选择器看起来并不能解决这个问题。