CSS z-index

CSS z-index specifies the stack order of overlapping elements. An element with a higher z-index appears in front of an element with a lower 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;

}
A higher z-index value places an element above lower stacked elements.

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;

}
Elements with higher z-index values appear above elements with lower values.

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
Always apply a position property before using z-index.

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.
Negative z-index values can place an element behind other content.

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;

}
Choose z-index values carefully to avoid overlapping issues in large projects.

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
position:absolute; and z-index are commonly used to create layered layouts.

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
Fixed navigation bars usually use a high z-index value.

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
Sticky navigation bars should always have a suitable z-index.

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;

}
Proper z-index values improve usability and prevent interface conflicts.

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;

}
Use meaningful z-index values instead of extremely large numbers whenever possible.

11. CSS z-index Best Practices

Following best practices makes your webpage easier to manage and prevents unexpected overlapping issues.

  • Use z-index only 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.
A well-planned z-index hierarchy keeps your website organized and easy to maintain.

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;

}
The most common mistake is forgetting to set a position property before using z-index.

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
CSS z-index works consistently across all major browsers.

14. CSS z-index Interview Questions

  1. What is the purpose of the CSS z-index property?
  2. When does z-index work?
  3. Can z-index be used with position: static?
  4. What happens when two elements have different z-index values?
  5. Can z-index have negative values?
  6. What is the default stacking order?
  7. Why is position: relative commonly used with z-index?
  8. How is z-index used in modal windows?
  9. What is stacking order in CSS?
  10. Which element appears on top when overlapping?
These questions are frequently asked in HTML, CSS, and Front-End Developer interviews.

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.
Congratulations! You have successfully completed the CSS z-index lesson. You can now control the stacking order of elements and build professional layered layouts.

Quick Quiz

Which element appears on top when two positioned elements overlap?