Here is a behavior I don't quite understand regarding z-index and css pseudo element ::before / ::after.
It is illustrated on this jsfiddle : http://jsfiddle.net/jgoyon/T6QCf/
I created a positioned box and added content with ::after pseudo element (positioned as well).
#no-z-index {
background:lightblue;
width:100px;
height:100px;
position:relative;
}
#no-z-index:after {
content: 'z-index -1';
width:50px;
height:50px;
background:yellow;
position:absolute;
z-index:-1; /* z-index in question */
top:70px;
left:70px;
}
#z-index {
background:lightblue;
left:200px;
width:100px;
height:100px;
position:relative;
z-index:0; /* parent z-index */
}
#z-index:after {
content: 'z-index -1';
width:50px;
height:50px;
background:yellow;
position:absolute;
z-index:-1; /* z-index in question */
top:70px;
left:70px;
}
Is it an expected behavior ?
This is expected behavior, documented in the spec.
When you don't specify z-index
on the generating element (defaulting to auto
), the generating element and the pseudo-element will appear in the same stacking context. This allows the pseudo-element to appear below the element if its z-index
is lower.
When you do specify z-index
on the generating element, that element creates a new stacking context for the pseudo-element (and in fact all of its descendants), preventing the pseudo-element from ever appearing below it even if you give it a negative z-index
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With