When working with overlapping elements in web design — like dropdowns, modals, tooltips, or sticky headers — the CSS z-index
property is your go-to tool for managing which element appears “on top.”
In this article, we’ll break down:
- ✅ What the
z-index
property is - 📐 How stacking context works
- 🧠 When to use
z-index
- 🚫 Common pitfalls and how to avoid them
- 💡 Real-world examples
✅ What is z-index
?
The z-index
CSS property controls the vertical stacking order of elements that overlap.
.element {
position: relative;
z-index: 10;
}
- Higher
z-index
values are placed on top of lower values. - It only works on elements with a
position
value other thanstatic
(relative
,absolute
,fixed
, orsticky
).
📊 Syntax
z-index: auto | <integer>;
Value | Description |
---|---|
auto | Default stacking based on order |
Integer | Defines explicit stack position |
🧠 How Does z-index
Work?
To understand z-index
, you need to know about stacking context — a 3D model where elements are placed along an imaginary z-axis (front-to-back).
A stacking context is formed by:
- Root element (
<html>
) - Elements with a
position
other thanstatic
and az-index
value - Elements with
opacity
less than 1,transform
,filter
,perspective
,mix-blend-mode
, and a few others
🔁 Example:
<div class="box box1"></div>
<div class="box box2"></div>
.box {
width: 100px;
height: 100px;
position: absolute;
}
.box1 {
background: red;
z-index: 1;
}
.box2 {
background: blue;
left: 50px;
top: 50px;
z-index: 2;
}
📌 Result: .box2
(blue) will appear on top of .box1
(red), due to the higher z-index
.
🚨 Common Pitfalls with z-index
❌ 1. Using z-index
on a statically positioned element
/* This won't work */
.element {
z-index: 1000;
}
✔️ Fix: Add position: relative
, absolute
, or fixed
.
❌ 2. Expecting z-index
to override stacking context
Each stacking context is isolated — a child cannot appear above a parent’s stacking context, even with a higher z-index
.
🎯 Practical Use Cases
Use Case | Recommended Z-index Strategy |
---|---|
Modals/Overlays | z-index: 9999 or higher |
Dropdown Menus | z-index: 1000+ above content |
Sticky Headers | z-index: 10–100 depending on layout |
Tooltips | z-index: 10000 above everything |
📦 Real-World Example: Modal
<div class="overlay"></div>
<div class="modal"></div>
.overlay {
position: fixed;
top: 0; left: 0;
width: 100vw;
height: 100vh;
background: rgba(0,0,0,0.5);
z-index: 1000;
}
.modal {
position: fixed;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
background: #fff;
padding: 30px;
z-index: 1010;
}
Here, the modal will appear on top of the dark overlay because it has a higher z-index
.
✅ Summary
Property | Description |
---|---|
z-index | Controls stack order |
Only works | On non-static positioned elements |
Higher value | Appears on top |
🧠 Final Tips
- Always pair
z-index
withposition
- Use consistent z-index scales across components
- Consider using utility classes or design tokens for managing layers in large projects
🚀 Conclusion
The z-index
property might seem simple, but mastering it requires understanding stacking contexts and positioning. With a strong grasp of how elements are layered, you can confidently build modals, tooltips, dropdowns, and other interactive UI components that render exactly as intended.