CSS Stacking Context
Stacking contexts determine element layering order. Understanding them solves most “element hiding behind another” problems.
Core Principle: z-index
only works within the same stacking context. Elements in different contexts can’t directly compete for layering.
Stacking Context Triggers:
- Root element (
html
) position: absolute/relative/fixed/sticky
withz-index
≠ autoopacity
< 1transform
≠ nonefilter
≠ noneisolation: isolate
will-change
with certain values
Stacking Order (back to front):
- Root element background
- Positioned elements with negative z-index
- Block-level elements in normal flow
- Floated elements
- Inline elements
- Positioned elements with z-index: auto or 0
- Positioned elements with positive z-index
Mental Model: Think of nested picture frames - each frame creates its own layering system independent of other frames.
Debugging: Use browser dev tools 3D view or “Layers” panel to visualize stacking contexts.
Related: CSS Layout Debugging Strategy, CSS Positioning