CSS Z-Index: A Complete Guide
The z-index property in CSS manages the stacking order of elements, deciding which ones appear in front or behind when overlapping.
1. What is z-index in CSS?
z-indexdefines the stack order of elements.- Elements with a higher
z-indexappear in front of those with a lowerz-index. - Only positioned elements (
relative,absolute,fixed, orsticky) respond toz-index.
2. Why Use z-index?
- Overlapping Control – Manage which elements overlap others.
- Layering – Create multi-layer designs (modals, dropdowns, tooltips).
- Visibility – Ensure important elements (popups, sticky headers) stay on top.
z-index Works (Example)<div class="box box1">Box 1</div>
<div class="box box2">Box 2</div>
.box {
position: absolute;
width: 100px;
height: 100px;
}
.box1 {
background: red;
z-index: 1;
}
.box2 {
background: blue;
top: 30px;
left: 30px;
z-index: 2;}
- Result: Box 2 appears on top of Box 1 because it has a higher
z-index.
4. Key Points to Remember
- Higher Value = Higher Stack Order
- Negative
z-index– Places elements behind their parent or background. - Default Value –
z-index: auto;(inherits or follows normal stacking).
<div class="background">Background</div><div class="content">Content</div><div class="popup">Popup</div>.background {position: relative;z-index: 1;}.content {position: relative;z-index: 2;}.popup {position: absolute;z-index: 100;}
- Effect:
popupstays on top ofcontentandbackground.
.modal {position: fixed;top: 0;left: 0;width: 100%;height: 100%;background: rgba(0, 0, 0, 0.7);z-index: 999;}
- Use Case: Modals and overlays use high
z-indexto stay in front of the entire page.
.behind {position: relative;z-index: -1;}
- Effect: The element moves behind its parent but in front of the background.
8. Stacking Context (Important Concept)
- A new stacking context forms when:
position: absolute | relative | fixed | sticky;withz-indexother thanauto.opacityless than 1 (opacity: 0.9).transformapplied (transform: scale(1)).
- Elements inside a stacking context are layered within it, but not with elements outside.
.parent {position: relative;z-index: 10;}.child {position: absolute;z-index: -1;}
- Result:
childcannot go behindparenteven withz-index: -1.
9. Z-Index with Flex and Grid
- Flex/grid items respect
z-indexbut needpositionset (relative,absolute, etc.).
.flex-item {
z-index: 10; /* No effect without position */
}
z-index: 10; /* No effect without position */}
10. Example: Creating Layered Cards
<div class="card card1">Card 1</div><div class="card card2">Card 2</div>.card {position: absolute;width: 200px;height: 300px;}.card1 {z-index: 5;background: lightblue;}.card2 {z-index: 10;top: 50px;left: 50px;background: coral;
}
- Result:
card2overlapscard1due to higherz-index.
11. Best Practices for Using z-index
- Avoid High
z-indexValues – Large values (99999) make debugging hard. - Create Stacking Contexts Wisely – Use
relativeorabsoluteto limit scope. - Plan Layering Early – Organize elements logically from the start.
- Keep Values Manageable – Use increments like
1, 10, 100for flexibility. - Use
z-indexfor Critical Elements – Modals, popups, and tooltips should have clear stack priorities.
By mastering z-index, you can effectively layer and organize web elements, ensuring dynamic, user-friendly interfaces with precise control over visual hierarchy.