Skip to content

CSS Stacking Context Isolates Z-Index

Quick summary

You set z-index: 9999 on a child but it stays below a modal — a parent created a new stacking context that isolates the child's z-index.

Why this happens

  • An ancestor has transform, opacity < 1, filter, or will-change creating a stacking context

  • z-index on the child is compared only within its stacking context, not globally

  • A modal in a different context with a lower z-index still wins

Minimal example

✗ Broken code
.parent { transform: translateZ(0); /* creates stacking context */ }
.child { z-index: 9999; /* cannot escape parent's context */ }
.modal { z-index: 100; /* in root context, wins over .child */ }
✓ Fixed code
.parent { /* no transform — no new stacking context */ }
.child { z-index: 9999; /* compared in root context */ }
.modal { z-index: 100; }

transform on .parent creates a stacking context, so .child's z-index is local and cannot beat .modal in the root context. Removing the transform lets .child compete globally.

How to diagnose

  • Inspect the ancestor chain for properties that create stacking contexts

  • Compare the z-index of the contexts' roots, not just the leaf elements

  • Use DevTools to see the stacking context hierarchy

How to fix

  • Move the element out of the isolating stacking context (e.g., portal to body)

  • Raise the z-index of the ancestor that roots the context

  • Remove the property (transform, opacity) that creates the context if unneeded

How to prevent

  • Be aware that transform, opacity, filter create stacking contexts

  • Port modals and tooltips to the document body to escape nested contexts

Related Resources

Related Glossary

Related Lessons

  • CSS

    Learn about stacking contexts.

← Back to language