Coding/Learn/css/z index stacking
CSS Beginner Course
01.Introduction to CSS
02.Selectors (Element, Class & ID)
03.3 Ways to Add CSS to HTML
04.Colors & Backgrounds (HEX, RGB, HSL)
05.Typography & Fonts (font-size, font-weight, line-height)
06.The Cascade & Specificity Wars
07.The Sacred CSS Box Model
08.Padding vs Margin & Shorthand Notation
09.Borders & Border Radius (Rounded Corners & Circles)
10.The Magic of box-sizing: border-box
11.Sizing & Units (px, rem, em, %, vh, vw)
12.The Display Property (block, inline, inline-block, none)
13.Flexbox Part 1: Container Powers (justify, align, gap)
14.Flexbox Part 2: Direction, Wrapping & Item Powers
15.Positioning (relative, absolute, fixed, sticky)
16.Z-Index & The 3D Stacking Context
17.Pseudo-Classes (:hover, :active, :focus, :first-child)
18.Smooth Transitions & Box Shadows
19.Responsive Design & Media Queries (@media)
20.Capstone: Crafting a Modern UI Profile Card
Course Certificate0%

Complete all lessons to earn

LearnZ-Index & The 3D Stacking Context
16 / 20 0%
Lesson 16 of 20
5 mins read

Z-Index & The 3D Stacking Context

PreviousNext Lesson
In Plain English

The web isn't just 2D (X and Y); it has a Z-axis extending toward your eyeballs! The z-index property controls which element renders on top when elements overlap. Higher numbers sit on top of lower numbers.

The Mental Model

"Think of a deck of playing cards or Photoshop layers. Card with z-index: 10 sits on top of Card with z-index: 1. A modal popup at z-index: 100 sits on top of everything on the page."

Deep Dive: How It Works

The Positioning Prerequisite: z-index ONLY works on elements that have a position property (relative, absolute, fixed, or sticky). Setting z-index on position: static does nothing!

Standard Stacking Scale: Dropdowns (z-index: 10-20), Sticky Navbars (z-index: 50), Modal Dialogs (z-index: 100), Toast Notifications (z-index: 1000).

Stacking Context Trap: An element with z-index: 9999 cannot escape its parent if the parent has a lower stacking context.

Syntax Blueprint

/* Modal Backdrop */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.8);
  z-index: 100;
}

/* Modal Window */
.modal-content {
  position: fixed;
  z-index: 110; /* Sits above backdrop */
}

Higher z-index numbers stack in front of lower numbers along the Z-axis.

PointCore Rules to Remember

Point star
Requires Position: Always pair z-index with position: relative, absolute, fixed, or sticky.
Point star
Sensible Layer Scales: Use clean tiers (10, 50, 100, 1000) rather than typing z-index: 9999999.
Point star
Negative Z-Index: z-index: -1 places decorative background art behind standard text.

Common Beginner Traps & How to Fix Them

Mistake:Setting z-index on a static element and wondering why it doesn't work

Why it happens: Default position: static elements ignore the z-index property completely.

How to fix: Add position: relative; to the element.

Pro Tip: Create a z-index design system variable scale so your team never has z-index fighting bugs!

Live Interactive Example

Hit Run Code to see it live
Modal Layering Stack
CSS3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Live Browser Preview
Click "Preview / Apply" to view the rendered output.
How it works: z-index: 50 ensures the navbar always remains on top during scrolling.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Elevate an element with z-index

  • PointWrite a CSS rule for .modal-window with position: fixed; and z-index: 100;
1
2
Browser Preview & Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates