How it works
CSS 2's Appendix E defines how a stacking context paints its own subtree, bottom to top:
- The context element's own background and border (the root's background covers the canvas).
- Child stacking contexts with negative
z-index, most negative first, then tree order.
- In-flow, non-positioned block descendants: backgrounds and borders.
- Non-positioned floats.
- In-flow inline content: text, inline boxes, replaced elements.
- Positioned descendants with
z-index: auto or 0, and other stacking contexts at level 0 (an opacity or transform context counts as 0), in tree order.
- Child stacking contexts with positive
z-index, lowest first, then tree order.
Each child stacking context is painted atomically at its slot by running the same seven steps on its own subtree. That recursion is the whole story: a descendant's z-index can only reorder it among steps of its nearest ancestor context.
Step 6 also explains auto vs 0. A positioned box with z-index: auto is painted as if it were a context, but its positioned descendants and child contexts are hoisted into the parent context and compete there. With z-index: 0 it's a real context and keeps them.
Every creator in the Answer comes from the spec that defines the property: CSS Color for opacity, Transforms for the transform family, Filter Effects for filter and backdrop-filter, Masking for clip-path and mask, Compositing for mix-blend-mode and isolation, Containment for contain, Flexbox and Grid for item z-index, and Will Change for the hint. They create a context because the effect has to be applied to the subtree as one flattened image: you can't blur or fade half of an interleaved stack.
Edge cases
A negative z-index child goes behind its parent's background only if the parent is not a stacking context:
<style>
.parent { position: relative; width: 100px; height: 100px; background: white; }
.child { position: absolute; inset: 0; z-index: -1; }
</style>
<div class="parent" id="p1"><div class="child" id="c1"></div></div>
<div class="parent" id="p2" style="z-index: 0"><div class="child" id="c2"></div></div>
<div class="parent" id="p3" style="isolation: isolate"><div class="child" id="c3"></div></div>
<script>
for (const parent of document.querySelectorAll(".parent")) {
const { left, top } = parent.getBoundingClientRect();
console.log(parent.id, document.elementFromPoint(left + 10, top + 10).id);
}
// → p1 p1
// → p2 c2
// → p3 c3
</script>
In p1, the child joins the root context at step 2, below p1's step-3 background. In p2 and p3, the parent's background is step 1 of its own context, and the child's step 2 lands above it.
will-change creates the context even before the property changes: will-change: opacity at opacity: 1 does it, by design, so the start of an animation doesn't reorder the page.
Hit testing follows paint order, so a transparent stacking context on top swallows clicks. pointer-events: none on it lets them through without changing what's painted.
Real-world scenario
A site header is position: sticky; z-index: 10, and its account menu has z-index: 1000. A chat widget added later sits at the body level with z-index: 50. The menu now opens under the chat widget. Inside the header, 1000 is meaningless outside the header's context; the real comparison is header 10 vs widget 50.
The fixes, best first:
- Render the menu as a
popover so it enters the top layer and stops depending on any ancestor's context.
- Define page-level layers once (
--z-header, --z-overlay, --z-toast) and apply them only to the elements that form contexts at the root.
- Put
isolation: isolate on self-contained components (cards, carousels) so their internal z-index values can't collide with page-level layers.
Trade-offs
A global z-index scale works only if each value sits on an element that's a child of the root context; once a value lands inside a nested context it's silently local. isolation: isolate keeps contexts intentional but also makes the element a group for blending, so a mix-blend-mode child inside it no longer blends with the page behind.
Creating contexts through transform or will-change to promote GPU layers changes stacking and containing blocks as a side effect; compositing layers are a separate browser concern from stacking contexts.