What Creates a New Stacking Context in CSS?
A stacking context is a three-dimensional conceptualization of HTML
elements along the imaginary z-axis relative to the user. Understanding
what triggers a new stacking context is essential for mastering CSS
layering, as child elements within a stacking context cannot escape
their parent’s stacking order, regardless of their own
z-index values. This article covers the primary CSS
properties and values that generate new stacking contexts, from classic
positioning rules to modern CSS layout and compositing features.
The Root Stacking Context
The base stacking context is automatically established by the root
element of the document, which is the <html> element
in standard web pages. Every other element on the page exists within
this initial context unless a distinct property creates a localized
sub-context.
Position and z-index
The most traditional method for instantiating a new stacking context
involves combining CSS positioning with the z-index
property.
- Positioned elements with an explicit
z-index: Any element with apositionvalue ofrelative,absolute, orfixed(in older browser specs) that also has az-indexvalue other thanautoforms a stacking context. - Sticky and fixed positioning: An element with
position: fixedorposition: stickyalways creates a stacking context in modern browsers, even ifz-indexis set toauto.
Flexbox and Grid Child Items
Direct children of flex containers (display: flex or
display: inline-flex) and grid containers
(display: grid or display: inline-grid)
generate a stacking context when their z-index property is
set to any value other than auto. Unlike standard
block-level elements, flex and grid items do not require explicit
position declarations to utilize z-index for
stacking context creation.
Visual and Transform Properties
Several visual rendering and compositing properties force the browser to composite an element separately, resulting in the creation of a new stacking context:
- Opacity: An element with an
opacityvalue strictly less than1. - Transform: An element with a
transformproperty value other thannone(such asscale(),translate(), orrotate()). - Filter: An element with a
filterproperty value other thannone(such asblur(),drop-shadow(), orgrayscale()). - Perspective: An element with a
perspectivevalue other thannone. - Clip-path and Masking: An element with
clip-path,mask,mask-image, ormask-borderapplied with a value other thannone.
Isolation and Containment
Modern CSS provides explicit mechanisms for isolating components and managing rendering boundaries:
- The
isolationproperty: Settingisolation: isolateis the most explicit and semantic way to create a new stacking context without altering visual styles or layout behavior. - Containment: Elements with
containvalues oflayout,paint,strict, orcontentform a new stacking context as part of browser rendering optimization. - Container queries: Setting
container-typetoinline-sizeorsizecan establish formatting contexts that encapsulate rendering layers.
Compositing and Performance Properties
- Mix-blend-mode: An element with
mix-blend-modeset to any value other thannormal. - Will-change: An element with a
will-changeproperty specifying any property that would create a stacking context on its own (for instance,will-change: transformorwill-change: opacity).
Stacking Context Containment
Once a stacking context is formed, all descendant elements are
grouped and rendered within that single layer relative to elements
outside of it. A child element with z-index: 9999 inside a
parent element with z-index: 1 will never appear in front
of an external sibling element with z-index: 2. Managing
these boundary conditions ensures predictable layer stacking and
prevents common layout conflicts.