SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Welcome to CSS
The Box Model and SizingWriting Modes and Logical PropertiesSizing ElementsBorders and ShadowsDisplay and Document FlowNormal FlowFloats and ColumnsPositioning and StackingStacking and Z-Index Measuring boxes and predicting their positions
Box Model and Flow

Stacking and Z-Index

Control overlapping elements and understand stacking contexts.

When boxes overlap, the browser must decide which one to paint in front. z-index helps control that order, but its numbers are compared within stacking contexts, not across the whole page.

Set an order for positioned boxes

.card {
  position: relative;
  padding: 1rem 1rem 5rem;
  border: 1px solid green;
}

.badge {
  position: absolute;
  top: 1rem;
  right: 1rem;
  z-index: 2;
  background-color: honeydew;
}

.caption {
  position: absolute;
  top: 1rem;
  right: 1rem;
  margin: 0;
  z-index: 1;
  background-color: white;
}

Where these boxes overlap, the badge paints in front because 2 is greater than 1 within their shared stacking context. z-index changes painting order; it does not move boxes or reserve space.

For these ordinary boxes, positioning makes z-index applicable. Flex and grid items can also use z-index without positioning. Adding a large number to a static paragraph in normal block flow does not bring it forward.

Understand the context boundary

A stacking context groups elements into one layer. Children stack within that group, and the whole group stacks among the surrounding elements.

The document root creates a stacking context. Other common causes include:

  • A relative or absolute box with a z-index other than auto.
  • A fixed or sticky box.
  • An element with opacity less than 1.
  • An element with transform other than none.
  • An element with isolation: isolate.

This is a selection, not the complete list. In particular, position: relative by itself, with z-index: auto, does not create a stacking context.

Compare ancestors before descendants

For a separate example, put a badge inside one section and a notice after it:

.panel {
  position: relative;
  z-index: 1;
  height: 6rem;
  background-color: honeydew;
}

.panel .badge {
  position: absolute;
  top: 4rem;
  left: 0;
  z-index: 999;
}

.notice {
  position: relative;
  z-index: 2;
  margin-top: -3rem;
  padding: 1rem;
  background-color: white;
}

The notice overlaps the panel. Wherever it overlaps the badge, the notice paints in front:

  • The panel establishes a context at level 1.
  • The badge's 999 is meaningful inside that panel.
  • The notice's context is at level 2 in the shared parent context, above the panel as a whole.

Increasing the badge's number cannot move it outside its context. Adjust the ancestor's stacking level or move the overlay to a suitable place in the document when that matches the intended design.

A badge at z-index 999 belongs to a panel at level 1. The separate notice at level 2 covers the panel and hides the badge where they overlap.

Keep a component's stacking local

Give a component its own stacking context with isolation: isolate:

.card {
  isolation: isolate;
}

Its descendants now stack within the card's context. This can keep local decorative layers from competing directly with unrelated page layers, but it also constrains overlays that need to appear outside the component's stacking order.

If an element appears behind the wrong thing, check its parents for stacking contexts. Then check its position and z-index.

Key Takeaways

  • z-index: Changes which eligible elements appear in front without moving them.
  • Contexts: Stacking contexts group descendants into units in a parent context.
  • Local numbers: A child's large value cannot escape an ancestor's stacking context.
  • Context creators: Positioning, opacity, transforms, and isolation can establish boundaries.
  • Debugging: Compare the relevant ancestor contexts before adjusting descendant values.

Try it yourself

Try it yourself

Bring the badge above the notice

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

No comparison yetChecks after 3s without edits
Loading your draft…

Take the quiz · Measuring boxes and predicting their positions

How is this lesson?

Previous lessonPositioning and StackingNext lesson Measuring boxes and predicting their positions
Edit on GitHubReport on GitHub
On this page
%

On this page

Set an order for positioned boxesUnderstand the context boundaryCompare ancestors before descendantsKeep a component's stacking localKey TakeawaysTry it yourself