DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
← From Beginner to Expert in Fundamentals of Frontend
  • The Box Model
  • Specificity & The Cascade
  • CSS Units Deep Dive
  • Stacking Contexts & Z-Index
  • Flexbox Mastery
  • CSS Grid Mastery
  • Centering, Subgrid & Container Queries
  • CSS Custom Properties & Variables
  • Selectors: :has(), Pseudo-classes & Pseudo-elements
  • Responsive Functions: calc(), clamp(), min(), max()
  • Media Queries & Mobile-First Design
  • Transitions & Animations
  • Transforms, Filters & Clipping
  • CSS Methodologies: BEM, CSS-in-JS & Tailwind
  • Typography, Color Spaces & Dark Mode
  • CSS Performance & Optimization
  • CSS Interview Questions — Part 1
  • CSS Interview Questions — Part 2
← From Beginner to Expert in Fundamentals of Frontend

Stacking Contexts & Z-Index

15 min read
Z-IndexStacking Contextpositionopacitytransformisolation

Why z-index: 9999 Sometimes Doesn't Work

Z-index is one of the most misunderstood properties in CSS. The problem is that z-index doesn't create a single global layering system — it creates stacking contexts.

What Creates a Stacking Context?

PropertyValueCreates Context?
position + z-indexrelative/absolute/fixed + any z-index value✅ Yes
opacityAny value less than 1✅ Yes
transformAny value (even translateZ(0))✅ Yes
filterAny value✅ Yes
isolationisolate✅ Yes
will-changetransform, opacity, etc.✅ Yes
position: fixed/stickyAlways✅ Yes
mix-blend-modeAny value except normal✅ Yes
containlayout, paint, or strict✅ Yes

The Key Rule

A child element can never appear above or below its parent's stacking context siblings. Think of stacking contexts as sealed envelopes — you can reorder cards inside an envelope, but the envelope's position is fixed relative to other envelopes.

Z-Index Strategy

Instead of random numbers, use a token system:

:root {
  --z-dropdown: 100;
  --z-sticky: 200;
  --z-modal-backdrop: 300;
  --z-modal: 400;
  --z-toast: 500;
  --z-tooltip: 600;
}

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.