CSS Stacking Order
Stacking Order is the order in which elements overlay each other along the Z-axis (depth). This determines which element will be "on top" if they overlap.
How Does Stacking Order Work?
Basic Rules
- By default, elements overlay in the order they appear in the DOM.
- Elements with
position(relative,absolute,fixed,sticky) andz-indexcan overlay each other. - Creating stacking context affects display priority.
Stacking Order Algorithm
- Root element context (
html) - Block background and borders with
z-index: auto - Block elements without positioning (
position: static) - Positioned elements (
relative,absolute,fixed,sticky) withz-index: auto - Positioned elements with z-index > 0
- Created stacking contexts (inside
transform,opacity < 1,filter,will-change, etc.)
What is Stacking Context?
Stacking Context is an isolated layering area where layer order works independently of parents.
How Stacking Context is Created
z-index(with positioned elements)opacity< 1transform,filter,perspectivewill-changeposition: fixedorposition: sticky
Tip:
If z-index doesn't work, check if the element creates its own stacking context.