CSS The z-index Property

CSS z-index Property – Complete Beginner Guide
When elements overlap on a webpage, which one appears on top?
That’s where the z-index property comes in.
The z-index property controls the stacking order of positioned elements. It determines which element appears in front and which appears behind.
In this beginner-friendly, you’ll learn:
What
z-indexdoesHow stacking works in CSS
When
z-indexworks (and when it doesn’t)Stacking context explained
Real-world examples
Common mistakes
Best practices
Let’s dive in
What Is the z-index Property?
The z-index property specifies the stack order of elements.
Basic syntax:
Higher value → appears on top
Lower value → appears behind
Important Rule: z-index Only Works on Positioned Elements
For z-index to work, the element must have:
It does NOT work on:
Example:
Basic Example – Overlapping Boxes
HTML:
CSS:
Blue box appears on top because 2 > 1.
Default Stacking Order
If z-index is not defined:
Later elements in HTML appear on top
Positioned elements stack above non-positioned elements
Understanding default stacking helps debugging.
What Is Stacking Context?
Stacking context is a layered hierarchy system in CSS.
An element creates a new stacking context if:
It has
positionand az-indexvalueIt has
opacityless than 1It uses
transformIt uses
filter
Each stacking context is independent.
Example – Parent Stacking Context
Even if child has 999, it cannot escape parent’s stacking context.
Important concept!
Negative z-index
You can use negative values:
This pushes element behind others.
Be careful — it may disappear behind background.
Real-World Example – Modal Overlay
Used for:
Popups
Login modals
Lightboxes
Real-World Example – Sticky Navbar
Ensures navbar stays above content.
z-index in CSS Frameworks
Frameworks like:
Bootstrap
Tailwind CSS
Provide z-index utilities.
Example (Tailwind):
Example (Bootstrap):
Common Beginner Mistakes
- Forgetting to set position
- Using very large z-index values everywhere
- Not understanding stacking context
- Overlapping elements unnecessarily
- Using negative z-index carelessly
Best Practices
- Keep z-index values small and organized
- Use stacking layers system (e.g., 1–10 scale)
- Always define position
- Avoid using extreme values like 999999
- Debug with browser DevTools
Organizing z-index Scale (Professional Tip)
Example scale:
| Layer | z-index |
|---|---|
| Base content | 1 |
| Dropdown | 10 |
| Navbar | 100 |
| Modal | 1000 |
Helps maintain consistency.
How to Debug z-index Issues
Inspect element in browser
Check position property
Check stacking context
Verify parent z-index
Remove conflicting transforms
Most problems come from stacking context.
Frequently Asked Questions (FAQs)
1. What does z-index do in CSS?
It controls stacking order of positioned elements.
2. Why is z-index not working?
Because position is not set or stacking context is interfering.
3. Can z-index be negative?
Yes, but it may hide element behind others.
4. Does z-index work without position?
No.
5. What is stacking context?
A hierarchical layer system that controls stacking behavior.
Conclusion
The CSS z-index property is essential for controlling overlapping elements.
You learned:
How stacking works
Position requirement
Stacking context
Real-world examples
Best practices
Mastering z-index helps you build professional UI components like modals, dropdowns, and overlays.
