A stacking context in CSS is a three-dimensional grouping of elements along the imaginary z-axis, inside which descendants are painted in a defined order and compared with each other only. The group as a whole is then placed into its parent stacking context as a single unit, so no descendant can rise above or sink below content outside the group, whatever its z-index value.
Understanding stacking contexts explains most of the puzzling layering bugs in front-end work, such as a dropdown that stays hidden behind a header despite z-index: 9999. The root element always forms the first stacking context, and many other properties create new ones, often as an unintended side effect.
How a stacking context orders painting
Inside each stacking context, the browser paints content in a fixed sequence of layers defined by the CSS 2.1 specification, Appendix E, and refined by later modules. Elements later in this sequence appear in front of earlier ones, and elements within the same layer are painted in document source order.
The following table lists that painting order from back to front.
| Order | Layer | What it contains |
|---|---|---|
| 1 | Root background | Background and borders of the element forming the context |
| 2 | Negative z-index | Positioned descendants with z-index below zero, lowest first |
| 3 | Block-level flow | Non-positioned block descendants in source order |
| 4 | Floats | Non-positioned floating descendants |
| 5 | Inline content | Inline and inline-block descendants, including text |
| 6 | Z-index zero or auto | Positioned descendants with z-index 0 or auto, in source order |
| 7 | Positive z-index | Positioned descendants with z-index above zero, lowest first |
The crucial rule is atomicity: once an element forms a stacking context, its entire subtree is resolved first and then treated as one painted layer at the position of that element in its parent context. A child with z-index: 1000 inside a parent with z-index: 1 is still behind a sibling of the parent that has z-index: 2.
Properties that create a stacking context
A new stacking context is created by a long list of property values, not only by z-index. Many of them are visual or performance properties whose layering effect is easy to overlook, which is why stacking bugs often appear after an unrelated change such as adding a fade animation.
The table below groups the most common triggers and their typical origin in real code.
| Trigger | Condition | Typical origin |
|---|---|---|
| Positioned with z-index | position relative or absolute and z-index not auto | Deliberate layering |
| Fixed or sticky position | position fixed or sticky, always | Headers and toolbars |
| Flex or grid child with z-index | z-index not auto on a flex or grid item | Card layouts |
| Opacity below 1 | Any value under 1 | Fade effects and disabled states |
| Transform, filter, perspective | Any value other than none | Animations and hover effects |
| Mix-blend-mode | Any value other than normal | Decorative blending |
| Isolation isolate | isolation set to isolate | Explicit grouping without side effects |
| Will-change | Naming a property that creates a context | Performance hints |
| Contain | layout, paint, strict or content | Rendering optimization |
| Top layer | Modal dialogs, popovers, fullscreen | Native overlays |
Because a stacking context is also a natural compositing boundary, browsers often promote such elements to separate GPU layers. Creating many of them, for example through blanket will-change rules, can raise memory use without any visible benefit.
Stacking context compared with related concepts
A stacking context is frequently confused with neighboring ideas in CSS layout and rendering. Distinguishing them helps locate the real cause of a layering problem instead of escalating z-index values.
| Concept | What it controls | Relationship to stacking context |
|---|---|---|
| Z-index | Order of an element within its current context | Meaningful only inside a stacking context |
| Containing block | Reference box for sizing and positioning | Independent; a transform affects both |
| Block formatting context | Float containment and margin collapsing | Separate mechanism with different triggers |
| Cascade layers | Priority of style rules | Unrelated despite the similar word |
| Top layer | Rendering above the whole document | Escapes all page stacking contexts |
The top layer deserves special attention: content placed there through showModal(), the popover API or the Fullscreen API is rendered above every stacking context in the document, which makes it the reliable modern solution for overlays that must never be clipped or covered.
Debugging and controlling stacking contexts
Effective debugging starts by finding the nearest ancestor that forms a context for each element involved, then comparing those ancestors rather than the elements themselves. Browser developer tools help: Chromium offers a Layers panel and a 3D view, and Firefox marks elements that create a stacking context in its layout inspection.
- Walk up the DOM tree and note every ancestor with a context-creating property.
- Compare elements at the level where their ancestor chains diverge.
- Adjust z-index on those sibling ancestors, not on the deeply nested child.
- Use
isolation: isolateto create a context without visual side effects. - Move overlays to the top layer or render them near the end of the body.
A disciplined team also defines a small z-index scale, for example five named levels stored in CSS variables, instead of arbitrary values. Combined with deliberate isolation of components, this keeps layering predictable as the code base grows.
Common mistakes with stacking contexts
Most failures follow a few recognizable patterns. The table below links each symptom to its usual cause and a durable fix.
| Symptom | Likely cause | Durable fix |
|---|---|---|
| Dropdown hidden behind a header | Dropdown parent forms a lower context | Raise the parent or move the menu to the top layer |
| Overlay jumps behind content during animation | Transform or opacity creates a context mid-transition | Set the context permanently with isolation |
| Negative z-index child disappears | Child slides behind an ancestor background | Give the intended parent its own context |
| Z-index has no effect | Element is static and not a flex or grid item | Add position relative or adjust layout |
| Ever-growing z-index values | Teams compete without a shared scale | Introduce named layering tokens |
The deepest misconception is that z-index is a global ranking across the page. It is a local ranking within one context, and recognizing that distinction resolves most layering disputes.
Put simply
Think of a web page as a stack of transparent sheets. A stacking context is like stapling several sheets into one booklet. Inside the booklet, the sheets can be rearranged freely. The whole booklet, however, moves as one piece within the larger pile.
So a sheet inside one booklet can never slip between the sheets of another booklet. That is why a menu with a huge layer number can still hide behind a header. The real fix is to move the whole booklet, not to shout a bigger number.
In practice
A front-end developer at an online retailer receives a report that the account menu opens behind the sticky promotion banner. The menu already has z-index: 999, while the banner has only z-index: 10. Inspection shows that the menu sits inside the site header, which uses transform: translateZ(0) added years earlier as a performance hint, so the header forms its own context with an implicit z-index of zero.
Raising the menu value changes nothing, because it competes only inside the header. The developer removes the obsolete transform, gives the header position: sticky with z-index: 30 from the shared layering scale, and sets the banner to the lower token of 20. The menu now appears above the banner on every page, and the fix requires two declarations instead of escalating numbers.