← Back to HTML & CSS, page 1

html-css-0002 · Hard senior

How do z-index and stacking contexts work, and why does a huge z-index sometimes not work?

z-index orders overlapping boxes, but only inside the same stacking context. A context is created by a positioned element with a z-index value, by opacity below 1, by transform, filter, isolation and several other properties. A child cannot rise above something outside its parent's context, so a huge z-index often fails because an ancestor created a lower-ranked context.

Full answer

z-index decides which overlapping box is painted on top. It applies to positioned elements (anything except position: static) and to flex and grid items. A higher number paints above a lower one, but only among boxes in the same stacking context.

A stacking context is a self-contained group. Everything inside it is painted together as one unit, and the group as a whole is compared with its siblings. So a child with z-index: 9999 cannot rise above an element outside its group if the group itself is ranked lower.

A new stacking context is created by the root element, by a positioned element with a z-index other than auto, by position: fixed or sticky, by opacity below 1, by transform, filter, perspective, clip-path or mask, by will-change on such properties, by isolation: isolate, and by contain: layout or paint. A flex or grid item with a z-index other than auto also creates one.

In the first stage the red parent has z-index: 1, so it forms a stacking context ranked below the blue box with z-index: 2. Its child with 9999 stays trapped inside it, and the blue box covers it. In the second stage the parent has z-index: auto, so it creates no context. The child joins the stage's context and its 9999 beats the 2, so the green box shows on top.

To debug, check each ancestor of the hidden element for a property from the list above, especially an innocent opacity, transform or filter. Then either move the element (modals are often rendered at the end of body for this reason), or remove the property, or put the competing parts in an explicit context with isolation: isolate so z-index values stay local.

Inside a context, painting order is: its own background, negative z-index children, in-flow blocks, floats, inline content, positioned children with z-index: auto or 0, then positive z-index in ascending order.

html
<style>
  body { margin: 0; font: 14px sans-serif; }
  .stage { position: relative; isolation: isolate; width: 120px; height: 100px; margin: 10px; border: 1px solid #333; }
  .layer { position: absolute; top: 0; left: 0; width: 100px; height: 100px; }
  .parent { background: #fbb; z-index: 1; }
  .parent.auto { z-index: auto; }
  .other { background: #bbf; z-index: 2; }
  .deep { position: absolute; inset: 0; background: #bfb; z-index: 9999; }
</style>

<div class="stage" id="s1">
  <div class="layer parent"><div class="deep" id="d1">deep</div></div>
  <div class="layer other" id="o1">other</div>
</div>

<div class="stage" id="s2">
  <div class="layer parent auto"><div class="deep" id="d2">deep</div></div>
  <div class="layer other" id="o2">other</div>
</div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • z-index works on positioned elements and on flex and grid items.
  • A stacking context is a group painted as one unit and compared with its siblings.
  • Opacity below 1, transform, filter, position: fixed or sticky, and isolation: isolate all create a stacking context.
  • A child's z-index is only compared with elements in the same context.
  • Debug by checking every ancestor for a property that creates a context, then move the element or remove the property.

Common mistakes

  • raising z-index again and again on a child, when the real problem is an ancestor that created a stacking context.
  • setting z-index on a static element and seeing no effect. Add position: relative, or make the parent flex or grid.

Follow-ups

  • how can you use isolation: isolate on purpose?
  • Why do many teams render modals at the end of the body?
  • In what order does the browser paint the layers inside one stacking context?

Tips

  • Keep z-index values small and named, for example --z-dropdown: 10 and --z-modal: 100. Huge numbers hide a stacking-context problem rather than fix it.
  • When you describe the bug, name the two stacking contexts involved. That shows you understand the cause and not just the symptom.
  • Mention that the native dialog element and the popover top layer avoid most of these fights.