CSS z-index
1. Introduction to CSS z-index
The z-index property controls which element appears on top when two or more positioned elements overlap.
It works only with elements that have a position value such as relative, absolute, fixed, or sticky.
.box1{
position:absolute;
z-index:1;
}
.box2{
position:absolute;
z-index:2;
}
2. Understanding the Stacking Order
Every webpage has a stacking order. When elements overlap, the browser decides which one appears in front.
| z-index | Result |
|---|---|
| 1 | Behind elements with higher values. |
| 5 | Appears above z-index:1. |
| 10 | Appears above both. |
.red{
position:relative;
z-index:1;
}
.blue{
position:relative;
z-index:5;
}
3. Using z-index with Positioned Elements
The z-index property has no effect on statically positioned elements. It works only when the element has a position value other than static.
.box{
position:relative;
z-index:100;
}
| Position | Supports z-index |
|---|---|
| static | ❌ No |
| relative | ✔ Yes |
| absolute | ✔ Yes |
| fixed | ✔ Yes |
| sticky | ✔ Yes |
4. Positive and Negative z-index
The z-index value can be positive, zero, or negative.
.front{
position:absolute;
z-index:10;
}
.back{
position:absolute;
z-index:-1;
}
| Value | Effect |
|---|---|
| -1 | Moves element behind others. |
| 0 | Default stack level. |
| 10 | Displays above lower values. |
5. Practical z-index Example
A common use of z-index is displaying navigation bars, popups, modals, tooltips, and floating buttons above other page content.
.header{
position:fixed;
top:0;
z-index:999;
}
| Component | Typical z-index |
|---|---|
| Header | 100 |
| Dropdown Menu | 500 |
| Modal Window | 1000 |
| Tooltip | 2000 |
.modal{
position:fixed;
z-index:1000;
}
6. z-index with position: absolute
Elements with position: absolute; can overlap other elements. The z-index property determines which element appears on top.
.box1{
position:absolute;
left:50px;
top:50px;
z-index:1;
}
.box2{
position:absolute;
left:80px;
top:80px;
z-index:5;
}
| Element | z-index | Result |
|---|---|---|
| Box 1 | 1 | Behind |
| Box 2 | 5 | In Front |
7. z-index with position: fixed
Fixed elements remain visible while scrolling. A high z-index keeps them above the page content.
.header{
position:fixed;
top:0;
width:100%;
z-index:999;
}
| Component | Recommended z-index |
|---|---|
| Header | 100-999 |
| Floating Button | 999 |
| Notification Bar | 1000 |
8. z-index with position: sticky
Sticky elements behave like relative elements until they reach a scroll position, after which they become fixed. A proper z-index prevents other elements from covering them.
.menu{
position:sticky;
top:0;
z-index:100;
}
| Property | Purpose |
|---|---|
| position:sticky | Sticks while scrolling |
| z-index | Keeps menu above content |
9. Real-World Uses of z-index
The z-index property is widely used in modern websites to control the display order of overlapping elements.
| Application | Purpose |
|---|---|
| Navigation Bar | Always visible on top |
| Modal Popup | Appears above webpage |
| Tooltip | Displays above other elements |
| Dropdown Menu | Shows above content |
| Floating Chat Button | Remains accessible |
.chat-button{
position:fixed;
bottom:20px;
right:20px;
z-index:9999;
}
10. z-index Comparison Table
| z-index Value | Layer Position |
|---|---|
| -1 | Behind other elements |
| 0 | Default layer |
| 1 - 99 | Normal overlapping elements |
| 100 - 999 | Headers and sticky menus |
| 1000+ | Modals, popups, tooltips |
.popup{
position:fixed;
z-index:1000;
}
11. CSS z-index Best Practices
Following best practices makes your webpage easier to manage and prevents unexpected overlapping issues.
- Use
z-indexonly when necessary. - Always apply a position property before using
z-index. - Use small, meaningful values instead of extremely large numbers.
- Create a consistent z-index scale for your project.
- Use higher values only for important UI components.
- Test overlapping elements on different screen sizes.
- Avoid unnecessary stacking conflicts.
- Document z-index values in large projects.
12. Common z-index Mistakes
| Mistake | Correct Practice |
|---|---|
| Using z-index without position. | Apply relative, absolute, fixed, or sticky. |
| Using very large values everywhere. | Maintain a logical stacking order. |
| Ignoring negative z-index. | Use carefully for background elements only. |
| Overlapping menus and modals. | Assign appropriate z-index values. |
| Not testing overlapping layouts. | Check on desktop and mobile devices. |
❌ Incorrect
.box{
z-index:100;
}
✔ Correct
.box{
position:relative;
z-index:100;
}
13. Browser Support
The z-index property is supported by all modern web browsers.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
14. CSS z-index Interview Questions
- What is the purpose of the CSS
z-indexproperty? - When does
z-indexwork? - Can
z-indexbe used withposition: static? - What happens when two elements have different z-index values?
- Can z-index have negative values?
- What is the default stacking order?
- Why is
position: relativecommonly used with z-index? - How is z-index used in modal windows?
- What is stacking order in CSS?
- Which element appears on top when overlapping?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS z-index.
- ✔ Stacking order.
- ✔ Using z-index with positioned elements.
- ✔ Positive and negative z-index values.
- ✔ Practical examples.
- ✔ z-index with absolute, fixed, and sticky positioning.
- ✔ Real-world applications.
- ✔ Best practices and common mistakes.
- ✔ Browser support.
- ✔ Interview questions.
Quick Quiz
Which element appears on top when two positioned elements overlap?