CSS Z-Index
Master element stacking and layering with z-index. Control which elements appear in front and understand stacking contexts for complex layouts.
How does z-index work in CSS?
z-index decides which overlapping elements appear in front: a higher value is closer to the viewer. It only works on positioned elements (relative, absolute, fixed, sticky) and on flex or grid items, and it only competes within the same stacking context.
.modal-backdrop { position: fixed; inset: 0; z-index: 100; }
.modal { position: fixed; z-index: 101; }
/* Common scale */
:root { --z-dropdown: 10; --z-sticky: 20; --z-modal: 100; --z-toast: 200; }Z-Index Stacking Demo
Code Editor
<div class="stage">
<div class="box a">z-index: 1</div>
<div class="box b">z-index: 3</div>
<div class="box c">z-index: 2</div>
</div>
<p>Change the numbers in the CSS tab to reorder the stack.</p>Preview
Understanding Z-Index
Z-index controls the stacking order of positioned elements, determining which elements appear in front of others. It's essential for creating layered interfaces, modals, dropdowns, and any component that needs to appear above other content.
Z-index only works on positioned elements (position: relative, absolute, fixed, or sticky). Elements with position: static ignore z-index completely.
Stacking Contexts
A stacking context is a group of elements that are stacked together as a unit. Elements within a stacking context can only compete with siblings in the same context.
What Creates Stacking Contexts
- • Positioned element with z-index ≠ auto
- • Opacity < 1
- • Transform ≠ none
- • Filter ≠ none
- • Isolation: isolate
- • Will-change with certain values
- • CSS containment
Context Rules
- • Children can't escape parent's context
- • Higher parent context wins
- • Z-index only compares within same context
- • Understanding prevents "z-index wars"
Z-Index Scale Systems
Recommended Z-Index Scale
Base Layers
- -1: Hidden behind
- 0: Base level
- 10: Docked elements
Navigation
- 100: Sticky headers
- 200: Dropdowns
- 300: Mobile menus
Overlays
- 1000: Modals
- 1100: Popovers
- 1200: Tooltips
System
- 9000: Skip links
- 9999: Debug tools
Common Interface Patterns
Modal System
z-index: 1040z-index: 1050Navigation
z-index: 100z-index: 200Best Practices & Debugging
Best Practices
- • Use a consistent z-index scale
- • Document your z-index system
- • Prefer CSS custom properties
- • Avoid very high numbers (999999)
- • Test across different browsers
- • Use isolation: isolate when needed
Common Issues
- • Forgetting position property
- • Not understanding stacking contexts
- • Z-index wars (escalating values)
- • Ignoring parent context limitations
- • Performance impact of many layers
- • Accessibility issues with overlays
Debugging Tips
Visual Debugging
- • Add colored borders to positioned elements
- • Use browser dev tools 3D view
- • Create z-index indicators
- • Log stacking contexts to console
Investigation Steps
- • Check if element is positioned
- • Find parent stacking context
- • Compare z-index within same context
- • Test with isolation: isolate
CSS z-index — step by step
How z-index Works
z-index controls the stacking order of positioned elements. Higher values appear in front. It only works on elements with position other than static.
🎯 Quick Check
Which CSS property creates a stacking context WITHOUT requiring z-index?
CSS z-index: summary
Key points
z-indexhas no effect on aposition: staticelement that isn't a flex or grid item.- A new stacking context is created by
positionwith az-indexother thanauto, byopacitybelow 1, and bytransform,filterorisolation: isolate, among others. - Children can never escape their parent's stacking context: a child with
z-index: 9999still sits below a sibling of its parent that's in front. - Keep a small, named scale (10, 20, 100…) in CSS variables instead of escalating to
99999.
Common mistakes
- Raising
z-indexhigher and higher when the real problem is a parent's stacking context. - Forgetting to add
position: relativeso thez-indexapplies. - A
transformon an ancestor unexpectedly creating a stacking context and trapping a dropdown.
Frequently asked questions
›Why is my z-index not working?
Either the element isn't positioned (add position: relative), or it's inside a stacking context created by a parent with z-index, opacity < 1, transform or filter. In that case its z-index only applies inside that parent.
›What is a stacking context?
It's a group of elements that are layered together as one unit. Elements inside it are ordered against each other, and then the whole group is placed among its siblings using the parent's z-index.
›Can z-index be negative?
Yes. A negative z-index places the element behind its siblings, and even behind its parent's content if the parent doesn't form a stacking context.