CSS

CSS

🎯 CSS z-index Property

The z-index CSS property controls the stacking order of positioned HTML elements on the web page.

It defines which elements appear in front of or behind others in the 3D layer system of a webpage.

🔹 Why Use z-index?

The z-index property is essential for managing overlapping elements like modals, dropdowns, tooltips, and sticky components.

  • Controls element layering on the page
  • Prevents unwanted overlap issues
  • Useful for modals, menus, tooltips
  • Improves UI structure and usability

🧩 z-index Values

1️⃣ auto

The browser automatically determines the stacking order based on the parent element.

CSS

div {
    z-index: auto;
}
                

2️⃣ number

A numeric value defines the stacking order. Higher values appear above lower ones.

CSS

.box1 {
    z-index: 10;
    position: relative;
}

.box2 {
    z-index: 100;
    position: relative;
}
                

Elements with higher z-index values appear on top of others.

3️⃣ initial

Resets the property to its default value (auto).

⚠️ Important Notes

  • z-index only works on positioned elements (relative, absolute, fixed, sticky).
  • Higher values appear in front of lower values.
  • If two elements have the same z-index, the one written later in HTML appears on top.

📌 Quick Summary

  • 🧠 Overview

  • z-index → controls stacking order of elements
  • Higher numbers appear above lower ones
  • Works only with positioned elements
  • Commonly used in overlays, modals, and menus