I am reviewing the stack context concept and am confused about the following example
In the parent div, we create a new stack context and I do not understand why the child stack above the parent div, I am also using this image as a reference.
.parent {
width: 200px;
height: 100px;
background: #168bf5;
position: relative;
/* Generate new stacking context */
z-index: 1;
}
.child {
width: 100px;
height: 200px;
background: #32d19c;
z-index: -1;
}
<div class="box">
<div class="parent">
parent
<div class="child">child</div>
</div>
</div>
Since you've created a new stacking context for
.parent
, settingz-index
on its children only changes their stacking order relative to other children of the same block. An element can't be layered behind its own stacking context. To put the child behind its parent, let itsz-index
work relative to the document (or some other common ancestor that's further up the DOM).In the example you linked, the
.box
element is set todisplay:flex
. A stacking context is also created when an element "is a child of a flex container, with z-index value other than auto" (stacking context). We can remove thez-index
value from.parent
to avoid giving it a new stacking context.