Rendering PipelineBaseline · Widely

CSS Stacking Context

Mental Model

A stacking context is an atomic boundary on the Z-axis. Descendants are painted relative to their parent context and can never interleave in front of an external element stacked above that parent.

Interactive Simulator

Stacking Context Trap on z-index: 9999

Toggle opacity or transform on the parent container to observe child isolation.

Z-Order Paint StackRoot Stacking Context
z-index: 10Sticky Header (sibling)Behind modal
Parent (z: 1)No isolation boundary
z-index: 9999Child ModalElevated
New Stacking Context
NO (root)
Effective child z-index
9999 (global)
GPU Compositor Layer
None
No intermediate stacking context: the child z-index: 9999 participates in the root context.
Parent (z: 1)
Child modal (z-index: 9999)
Sibling Header (z: 10)

Under the Hood

  1. During the Paint phase of the Browser Rendering Pipeline, the engine sorts boxes across 7 stacking levels (context background, z-index < 0, in-flow blocks, floats, inline, z-index: 0, z-index > 0).
  2. Many non-positioning properties create a stacking context: opacity < 1, transform !== none, filter !== none, isolation: isolate, contain: layout|paint.
  3. A child stacking context is painted as a single atomic unit inside its parent before subsequent sibling contexts are drawn.
CSS
/* Header sits at root stacking order z-index: 10 */
.site-header { position: sticky; z-index: 10; }

/* Parent opacity < 1 creates a stacking context at z-index: 1 */
.card-wrapper { position: relative; z-index: 1; opacity: 0.99; }

/* Trapped inside .card-wrapper: still paints BEHIND .site-header */
.card-tooltip { position: absolute; z-index: 999999; }

Problem Genealogy

  • 1998 (CSS 2.0): z-index only applies to positioned elements (relative, absolute, fixed).
  • 2011 (CSS3 Transforms/Opacity): Effects requiring an offscreen buffer (opacity < 1, transform) implicitly create a stacking context.
  • 2015+ (isolation: isolate): Dedicated property added to create a stacking context without visual side effects or forced GPU promotion.

Production Pitfalls

Production Pitfall
  • Modal stuck under a header: An intermediate ancestor receives transform: translateY(0) for an animation, trapping the modal's z-index: 9999 under the sticky header.
  • z-index: 999999 escalation: Adding extra nines to z-index is the CSS equivalent of shouting louder in a soundproof room, it never crosses a parent stacking context boundary.
When Not to Use

Connections & Logical Next Steps

Seen in Architecture Autopsies

Mechanical Comparisons