z-Index behaviour on pseudo elements

前端 未结 2 1701
余生分开走
余生分开走 2021-02-07 20:50

see:

http://jsfiddle.net/Kq2PY/

the div is relative with z-index 5, and the :after thing is absolute with z-index 2.

So shouldn\'t :after be behind the div?

相关标签:
2条回答
  • 2021-02-07 21:34

    You would have to give pseudo elements a negative z-index to get it to go behind it's parent, plus remove the z-index on the parent. 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>

    0 讨论(0)
  • 2021-02-07 21:35
    div { z-index: 1; }
    
    div::after { z-index: -1;}
    
    0 讨论(0)
提交回复
热议问题