Z-Index
z-index
property sets the z-ordering of elements (i.e., which element comes in front of another).
Basic Concepts
Stacking Context
z-index
will only work as expected if the elements which you try to reorder belong to one stacking context.
If two elements belong to different stacking contexts, their z-index
values won't be compared to each other.
A stacking context can be contained within other stacking context (creating tree-like hierarchy), but sibling stacking contexts are independent of each other. Resolve z-axis placement from the children stacking context, then the parent, and so on.
You can see what creates a stacking context here.
With Nesting
Note that nesting plays a big role. If an element B sits on top of element A, a child element of element A can never be on top of element B (CSS-Tricks).
(This is the implication of "resolve z-axis placement from the children stacking context, then the parent, and so on" — once stacking and rendering within A is completed, the whole element A is passed for stacking in the parent element with respect to its sibling B.)

CSS Properties
z-index
Specify auto
or an integer.
auto
— Does not create a new stacking context. This element's stacking context is the same as its parent.- integer — (E.g.
1
,0
, or-2
). Places the element in this stack level in the current stacking context. Also creates a new local stacking context with stack level of 0. (The creation of a new stacking context implies that thez-index
's of this element's descendants are not compared to thez-index
's of elements outside this element.)
Obscured CSS Properties
isolation
Forces an element to create a new stacking context.
auto
— Only creates a stacking context if required (depending of the element's CSS properties)isolate
— Forces the creation of a new stacking context.
References
- Adding z-index (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/Adding_z-index
- z-index (MDN) — https://css-tricks.com/almanac/properties/z/z-index/
- z-index (CSS-Tricks) — https://css-tricks.com/almanac/properties/z/z-index/
- The stacking context (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context
- isolation (MDN) — https://developer.mozilla.org/en-US/docs/Web/CSS/isolation