Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Have you ever had two elements overlap on a webpage, and the wrong element ended up on top? 🥞
Maybe a dropdown menu opened behind an image banner, or a popup dialog was covered by a sticky header.
In 2D web design, we control width on the X-axis (left-to-right) and height on the Y-axis (top-to-bottom). But web browsers also have a third dimension: the Z-axis—the depth perpendicular to your screen, pointing directly towards your eyes!
The CSS z-index property controls where an element sits along this 3D depth axis.
In this lesson, you will master:
z-indexauto z-index valuesz-index: 99999 doesn't always work!)Imagine piling your School Notebooks on your desk:
+-------------------------------------------------------------------------+ | THE 3D STACKING ORDER EXPLAINED | +-------------------------------------------------------------------------+ | (Your Eyes Looking Down from Above) | | | | [ Top Book: Math Homework ] z-index: 3 | | [ Middle Book: Science Journal ] z-index: 2 | | [ Bottom Book: History Textbook ] z-index: 1 | | ================================================= | | SURFACE OF WOODEN DESK z-index: 0 | | ================================================= | | [ Under Desk Mat: Secret Note ] z-index: -1 | +-------------------------------------------------------------------------+
A higher z-index value lifts the element closer to your eyes, stacking it right on top of elements with lower numbers!
z-index ⚠️This is the single most common mistake made by CSS beginners:
z-index ONLY works on positioned elements!
An element must have its position set to relative, absolute, fixed, or sticky (or be a flex/grid child).
If an element has default position: static;, adding z-index: 99999; has ZERO effect!z-index)If you never write a single z-index property in your stylesheet, how does the browser decide which element sits on top when they overlap?
The browser follows a strict natural hierarchy:
<html> element.<div>s, <p>s) in the order they appear in your HTML.relative, absolute, fixed, sticky).z-index ValuesThe z-index property accepts integer values:
1, 10, 100, 999)Pulls the element forward towards the user's eyes.
-1, -10)Pushes the element backward behind normal text and parent content. This is frequently used for subtle decorative background patterns, colored blurs, or watermark graphics!
auto (Default)The element has the same stacking level as its parent and does not establish a new stacking context.
Have you ever written z-index: 999999; on a modal or dropdown, and were shocked to see it still hidden behind a header with z-index: 2;?
Welcome to the Stacking Context!
Imagine two competing school teams: Team Blue and Team Gold.
+-------------------------------------------------------------+ | TEAM GOLD (Parent z-index: 2) | | - Player with z-index: 1 <-- WINS! | +-------------------------------------------------------------+ | TEAM BLUE (Parent z-index: 1) | | - Rohan with z-index: 999999 <-- CAN NEVER BEAT TEAM GOLD!| | (Because his parent container is trapped in z-index: 1) | +-------------------------------------------------------------+
If Parent A has z-index: 1 and Parent B has z-index: 2, NO child inside Parent A can ever appear in front of Parent B, no matter how gigantic its child z-index is!
To fix this, adjust the z-index of the parent container instead!
z-index Design SystemIn professional software development, never throw random numbers like 99999 or 9999999 into your CSS. Maintain a clean, organized layering scale:
| Bad Practice ❌ | Why It Fails ⚠️ | Better Approach ✅ |
|---|---|---|
Adding z-index: 10 without setting position | Ignored by browsers on default static elements. | Always declare position: relative; (or absolute/fixed/sticky). |
Writing absurd numbers like z-index: 99999999 | Leads to uncontrollable "z-index wars" across teammates and components. | Use a clean, standardized scale (10, 100, 500, 1000). |
| Wondering why an absolute popup is covered by a sibling section | The popup is trapped in a lower parent stacking context. | Raise the z-index of the parent container itself. |
Using negative z-index without testing clickability | The element can slide behind the body background, making it impossible to click. | Check that interactive links remain accessible. |
z-index ONLY functions on positioned elements (relative, absolute, fixed, sticky) and flex/grid items.z-index: -1; layers elements behind parent content (perfect for background art).z-index property to take effect on an element?z-index defined, which element renders on top?z-index: -1; on a positioned decorative graphic inside a card?z-index: 1 and Card B has z-index: 2. Inside Card A, a tooltip has z-index: 99999. Can this tooltip ever display in front of Card B?z-index scale (e.g. 100 for dropdowns, 500 for fixed headers, 1000 for modals) considered an industry best practice?z-index-layering-lab.html.z-index control:z-index: 10 immediately to the top of the pile! 🎯In the next lesson, we will continue your web styling journey with Float and Clear Basics: Wrapping Text Around Images and Legacy Layouts (Basic Layout Techniques).
👉 Continue to Next Lesson: Float and Clear Basics: Wrapping Text Around Images and Legacy Layouts →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.