CSS Reference ยท Practical Pages
Stacking contexts
How CSS stacking contexts form, which properties create them, the paint order inside one, and why a huge z-index sometimes loses to a smaller one.
A stacking context is a self-contained group of layers. Inside it, children are ordered against each other by z-index and paint order. From the outside, the whole group counts as one layer. That is the entire reason a z-index: 9999 can lose to a z-index: 2: the 9999 lives inside a group whose own position in the larger stack is low.
Think of folders in a stack of papers. Pages inside a folder never interleave with pages of another folder; only the folders are compared with each other.
.card { position: relative; z-index: 1; } /* creates a context */ .card .badge { position: absolute; z-index: 9999; } /* only wins inside .card */
Try it
Both stages have the same shapes. The blue box has a red child with z-index: 999; the green box has z-index: 2. In the first stage the blue box has z-index: 1, so it forms a context and the red child stays beneath green. In the second stage the blue box has no z-index, so no context forms and red wins.
What creates a stacking context
- The root element (
<html>). - A positioned element (
relative,absolute) with az-indexother thanauto. position: fixedorsticky, always.- A flex or grid item with a
z-indexother thanauto, even if it is not positioned. opacityless than1.transform,translate,rotate,scale,filter,backdrop-filter,perspective,clip-path,maskormix-blend-modewith a value other thannone.isolation: isolate.will-changenaming any property that would itself create one.contain: layout,paintorstrict, and container query containers with size containment.- Elements in the top layer (dialogs shown modally, popovers).
Paint order inside a context
From back to front:
- The context root's own background and borders.
- Child contexts with negative
z-index. - In-flow, non-positioned block descendants.
- Floats.
- In-flow inline content (text).
- Positioned descendants with
z-index: autoor0, in document order. - Child contexts with positive
z-index, lowest first.
Elements at the same level are painted in document order, so a later sibling covers an earlier one.
Usage notes
- Debug upward, not by number. When something will not come to the front, walk up its ancestors and find the nearest one that creates a context; compare that element with the thing you want to beat.
- Surprise contexts. A harmless-looking
transform: translateZ(0),opacity: 0.99orfilteron a wrapper can trap dropdowns inside it. - Contain on purpose. Use
isolation: isolateon a component so its internalz-indexvalues cannot leak out, then use small numbers freely. - Keep a scale. Use a few named custom properties (
--z-dropdown,--z-modal) instead of arbitrary huge values. - Modals and popovers that use the top layer escape ancestor contexts entirely, which is the cleanest fix for overlays.
z-indexneeds a participant. On a plain static block it has no effect.
Browser support
Stacking contexts are defined by CSS 2.1 and supported by every browser, with the same set of triggers in all current engines.