الانتقال إلى المحتوى
FrontendHTML و CSS· 6 دقائق للقراءة

z-index: 9999 Won't Save You: Stacking Contexts Explained

z-index only ranks boxes inside one stacking context, and many properties quietly create a new one.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

The account dropdown in the sticky header opens behind the product carousel. The menu has z-index: 9999, the carousel has z-index: 1, and someone bumps the menu to 99999. Nothing changes. Rendered in headless Chromium, a hit test at a point where the two overlap returns the carousel both times, and once the page scrolls, the carousel slides over the sticky header itself.

The common misconception

The belief: a higher z-index always puts an element on top of every element with a lower one.

On simple pages that's exactly what you see. When every positioned element lives in the same stacking context, the bigger number does win: in our test, removing position: sticky from the header made the menu's 9999 beat the carousel's 1 at once. The name also suggests one z-axis shared by the whole page, so when a number loses, the natural reaction is a bigger number.

What actually happens

z-index ranks siblings inside one stacking context

A is a group of boxes that the browser layers together. MDN describes each one as self-contained: after its contents are stacked, the whole element is treated as a single unit in the stacking order of its parent context. The z-index values of its children only mean something inside it.

A useful way to read it is as version numbers. MDN's own example writes the rendering order as pairs, such as 4-6 for a child with z-index: 6 inside a parent with z-index: 4, which loses to a box at 5-0. In the hook, the sticky header forms a context at level 0 in the root, and the carousel sits at level 1. The menu's real position is 0-9999, which loses to 1 every time. Raising 9999 changes the second number, and the comparison is decided by the first.

Many properties create a stacking context

z-index is only one of the triggers, and that's why these bugs feel random. MDN's list includes:

  • the root element, and position: absolute or relative with a z-index other than auto
  • position: fixed and position: sticky, with or without z-index
  • flex and grid items with a z-index other than auto
  • opacity below 1, mix-blend-mode, and transform, filter, backdrop-filter, clip-path, mask or perspective other than none
  • isolation: isolate, will-change naming one of those properties, contain: layout or paint, and size containers

We checked two of them in Chromium. Replacing the sticky header with a plain one that had opacity: 0.99 reproduced the bug, and so did backdrop-filter: blur(8px), the usual "frosted glass" header.

z-index needs a positioned box or a flex item

On a plain, non-positioned block, z-index does nothing. MDN says it applies to positioned elements, and to flex and grid items. In Chromium, a static box with z-index: 10 stayed under a later, overlapping position: relative sibling, while the same z-index on a flex item lifted it above its neighbor.

The painting order inside one context

CSS 2.2's Appendix E spells out the order the browser paints a stacking context in, from back to front. Simplified:

  1. The background and borders of the element that forms the context.
  2. Child stacking contexts with a negative z-index.
  3. Normal-flow blocks, then floats, then inline content and text.
  4. Positioned boxes with z-index: auto or 0, in source order.
  5. Child stacking contexts with a positive z-index, lowest first.

Contexts created without a z-index, such as by opacity, take their place in step 4, in source order. That's why the faded header lost to the carousel's z-index: 1.

Here is the dropdown, before and after:

CSS header.css
.site-header {  position: sticky; /* forms a stacking context */  top: 0;}.menu {  position: absolute;  z-index: 9999; /* ranks only inside .site-header */}.carousel {  position: relative;  z-index: 1; /* ranks in the root context */}

Rendered in headless Chromium, the fixed version puts the menu above the carousel, and the header stays above it while the page scrolls. The fix is at the level where the two actually compete: the header against the carousel, both in the root context.

MythReality
z-index is a global rankingIt ranks boxes only inside their own stacking context
A bigger number always wins9999 inside a context at level 0 still loses to 1 outside it
Only z-index creates layersSticky, fixed, opacity below 1, transform, filter and more create contexts
z-index works on any elementOnly on positioned boxes and flex or grid items
Modals need z-index: 99999A dialog opened with showModal() goes to the top layer, above all of it

How to apply it in your project

  1. Find the context that traps youStart at the element that loses and walk up its ancestors in DevTools until you reach one that forms a stacking context. Compare that ancestor with the winner, inside the context they share.
  2. Fix it at that levelGive the ancestor a z-index just above its competitor, or remove the property that created the context if nobody needs it. Small numbers are enough once you compare the right boxes.
  3. Keep a z-index scaleDefine the layers your app has once, as such as --z-header and --z-toast, and use only those. A bare 9999 in a component is a sign that someone fought the wrong fight.
  4. Contain components on purposeAdd isolation: isolate to a component's root when its internal z-index values shouldn't leak out, and render modals and popovers in the top layer instead of racing numbers.

Key takeaways

  • z-index orders boxes only within one stacking context, and the whole context is painted as one unit in its parent.
  • Sticky and fixed positioning, opacity below 1, transform, filter, backdrop-filter, isolation and others create contexts, often by accident.
  • z-index only works on positioned boxes and on flex or grid items.
  • Fix stacking bugs on the ancestor that forms the context, with small numbers from a shared scale.
  • For modals and popovers, use the top layer through showModal() or the Popover API instead of huge numbers.

📚 المصادر

  1. Stacking context — MDN
  2. z-index — MDN
  3. Top layer — MDN
  4. CSS 2.2, Appendix E: Elaborate description of Stacking Contexts — W3C

التعليقات (0)