The z-index property controls which overlapping element appears on top — essential for modals, dropdowns, and layered UI.
01
Stacking
Layer order.
02
Syntax
auto + integers.
03
auto
Default value.
04
position
Required pairing.
05
Contexts
Isolated layers.
06
Modals
Overlays & menus.
Fundamentals
Introduction
The z-index property in CSS controls the vertical stacking order of elements that overlap. When elements overlap, the z-index value determines which one appears on top.
This property only works on positioned elements (those with a position value other than static), or on flex and grid items when z-index is set.
Definition and Usage
Use z-index when building modals, sticky headers, dropdown menus, tooltips, and any UI where elements share the same screen space. Pair it with position so the browser can layer elements correctly.
💡
Beginner Tip
Higher numbers do not always win globally. A child with z-index: 9999 can still sit behind a sibling parent if that parent creates a lower stacking context. Always check parent elements too.
Foundation
📝 Syntax
The syntax for the z-index property is simple and can be applied to any positioned element.
syntax.css
element{z-index:auto | <integer>;}
Basic Example
z-index.css
.modal{position:fixed;z-index:1000;}
Syntax Rules
auto uses default stacking based on document order and stacking contexts.
Integers can be positive or negative; higher values stack in front within the same context.
z-index has no effect on position: static elements unless they are flex or grid items.
Creating a new stacking context (e.g., with position + z-index) isolates child layering.
The property is not inherited, but stacking contexts affect descendants.
Related Properties
position — required for z-index on most elements
opacity — values less than 1 can create a stacking context
isolation: isolate — explicitly creates a new stacking context
Defaults
🎯 Default Value
The default value of the z-index property is auto, which means the element will use the default stacking order based on its order in the HTML and its parent stacking context.
Cheat Sheet
⚡ Quick Reference
Question
Answer
Default value
auto
Bring element to front
Higher integer, e.g. z-index: 10;
Send element behind
Negative integer, e.g. z-index: -1;
Works with
position: relative | absolute | fixed | sticky
Modal pattern
Backdrop 100, dialog 101
Inherited
No
Reference
💎 Property Values
The z-index property accepts auto or an integer.
Value
Example
Description
auto
z-index: auto;
The element uses the default stacking order (default).
integer
z-index: 3;
Any integer, positive or negative. Higher values appear in front of lower values in the same stacking context.
auto110100-1
Context
When to Use z-index
z-index is helpful whenever overlapping elements need a clear front-to-back order:
Modals and dialogs — Layer backdrop and content above the page.
Dropdown menus — Keep menus above neighboring buttons and cards.
Sticky headers and toolbars — Ensure navigation stays above scrolling content when needed.
Tooltips and popovers — Display hints on top of interactive UI.
Decorative backgrounds — Use negative values to push shapes behind content.
Preview
👀 Live Preview
Three overlapping boxes with z-index: 1, 2, and 3:
z: 1
z: 2
z: 3
Hands-On
Examples Gallery
In this example, we will stack three colored boxes using the z-index property.
📜 Core Patterns
Control basic layering with positioned elements and integer values.
Example 1 — Stacked colored boxes
In this example, we will stack three colored boxes using the z-index property.
The card creates a stacking context. The pseudo-element’s negative z-index places the glow behind the card’s background but still inside the card box.
A11y
♿ Accessibility
Focus order vs visual order — Changing z-index does not change tab order. Keep keyboard focus logical.
Modals — Trap focus inside dialogs and restore focus when closed; z-index alone does not make a modal accessible.
Hidden content — Elements behind others may still receive pointer events unless you also manage visibility or pointer-events.
Contrast — Overlapping layers should maintain readable text contrast.
Companion
z-index + position
z-index depends on positioning. Set position first, then choose a z-index that fits your UI layer scale.
layer-scale.css
/* Example layer scale */.dropdown{position:absolute;z-index:50;}.sticky-header{position:sticky;z-index:40;}.modal{position:fixed;z-index:100;}
🧠 How z-index Works
1
Elements overlap
Positioned elements share the same screen area.
Input
2
Stacking context applies
Parent contexts group children into layers.
Context
3
z-index compares values
Higher integers render in front within the same context.
Order
=
✅
Correct visual layering
The intended element appears on top.
Compatibility
Browser Compatibility
The z-index property is fully supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is also supported in older browsers, making it a reliable property to use for controlling the stacking order of elements.
✓ Modern browsers · Universal support
Reliable layering control
auto and integer z-index values work consistently across all major browsers.
99%Browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll versions
Full support
Testing tip
Test modals and dropdowns in Chrome, Safari, and Firefox. Verify stacking when parent elements use transform or opacity.
z-index property99% supported
Bottom line:z-index is safe to use everywhere for layering positioned elements.
Wrap Up
Conclusion
The z-index property is an essential tool for web developers when designing layouts with overlapping elements.
By controlling the stacking order, you can create complex and visually appealing designs. Experiment with different z-index values to see how they affect the stacking order of elements on your web pages.