Skip to content
Tutorials Center
My learning

Topics

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.

CSS
.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.

Context traps a big z-index

What creates a stacking context

  • The root element (<html>).
  • A positioned element (relative, absolute) with a z-index other than auto.
  • position: fixed or sticky, always.
  • A flex or grid item with a z-index other than auto, even if it is not positioned.
  • opacity less than 1.
  • transform, translate, rotate, scale, filter, backdrop-filter, perspective, clip-path, mask or mix-blend-mode with a value other than none.
  • isolation: isolate.
  • will-change naming any property that would itself create one.
  • contain: layout, paint or strict, 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:

  1. The context root's own background and borders.
  2. Child contexts with negative z-index.
  3. In-flow, non-positioned block descendants.
  4. Floats.
  5. In-flow inline content (text).
  6. Positioned descendants with z-index: auto or 0, in document order.
  7. 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.99 or filter on a wrapper can trap dropdowns inside it.
  • Contain on purpose. Use isolation: isolate on a component so its internal z-index values 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-index needs 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.