If we use z-index
combined with position: absolute;
its possible to place the ::before
of a element under itself. There is a good example on another question (jsfiddle.net/Ldtfpvxy).
Basically
<div id="element"></div>
#element {
position: relative;
width: 100px;
height: 100px;
background-color: blue;
}
#element::after {
content: "";
width: 150px;
height: 150px;
background-color: red;
/* create a new stacking context */
position: absolute;
z-index: -1; /* to be below the parent element */
}
renders:
So the stacking context/order is defined by z-index
. But when I apply z-index: 1;
to the element and z-index: -1;
to its ::before
I cannot achieve the same thing.
Only if I omit z-index
from the element.
Any ideas why this is? Is the element rendered after its ::before
& ::after
pseudos so they get the same z-index
?
Working: https://jsfiddle.net/Ldtfpvxy/
Not working: https://jsfiddle.net/Ldtfpvxy/1/ (only added z-index: 1;
to element)
Specifying
z-index
you are creating a new stacking content;if this is done only on the child
::after
pseudo-elem the parent won't establish a new stacking content and everything will work as expected.But adding
z-index
on the parent element will start a new stack (which will also wrap the child-stack..).And if you look at the first 2 points on stack rendering specification you'll see background will be rendered before other child-stacks:
here's an example, to clarify the different rendering behavior for nested stacking background.
BTW
position: relative
is not optional, with defaultposition:static
z-index
has no effect.Your div and its pseudo-child are members of the same stacking context, in this case the root stacking context. The new stacking context you give the pseudo-element would be used as a reference to its children (which are non-existant), but the
z-index
value applies to the current stacking context. And the CSS spec dictates the following paint order for each stacking context:Look, child stacking contexts with negative stack levels, such as your
div::after
are painted before the positioned descendants with stack level 0, such as thediv
itself. This explains the behavior you noticed.