css - 伪元素上的 z-Index 行为

标签 css z-index pseudo-element

<分区>

见:

http://jsfiddle.net/Kq2PY/

div 与 z-index 5 相关,而 :after 是绝对的 z-index 2。

那么 :after 不应该在 div 后面吗?

div{
    position:relative;
    z-index: 5;
    background: #000;
    padding: 10px;
}    

div:after{
    content: '';
    position: absolute;
    z-index: 2;            /*  <= not working:( */
    background: #3d3;
    left: 20px;
    top: 20px; 
    width: 30px;
    height: 30px;
}    
<div>erferf</div>

最佳答案

您必须给伪元素一个负的 z-index 才能使其位于其父级之后,并删除父级的 z-index。 http://jsfiddle.net/jklm313/Kq2PY/4/

div{
    position:relative;
    background: #000;
    padding: 10px;
}    

div:after{
    content: '';
    position: absolute;
    z-index: -1;            /*  <= not working:( */
    background: #3d3;
    left: 20px;
    top: 20px; 
    width: 30px;
    height: 30px;
}    
<div>erferf</div>

关于css - 伪元素上的 z-Index 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12463061/

相关文章:

html - 如何处理 CSS 中的文本换行?

html - 将 li 直线向下对齐

html - 文本框,将文本带到文本框中的下一行

css - 具有绝对位置的 z 索引与 IE

html - Z-Index 不能通过动画/过渡正常工作?

css - 无法旋转 :after pseudo-element using CSS in IE8

javascript - "for"在 NodeList 上循环以更改类名

css - 悬停时子菜单位于主菜单后面

html - 如何选择样式的单选/复选框按钮?

javascript - 更改 :before or :after CSS 中的 SVG 填充颜色