Stacking Contexts and z-index
Why z-index 9999 sometimes loses to z-index 1, what creates a stacking context, and how isolation and the top layer keep overlapping UI predictable.
Stacking Contexts and z-index
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `z-index` does not rank an element against the whole page. It ranks it only against siblings inside the same stacking context.
- A stacking context is a self-contained painting group. A child's `z-index` cannot escape it, however large, so the whole group sits at the parent's level.
- Contexts are created by more than `position` plus `z-index`: also `opacity` below 1, `transform`, `filter`, `isolation: isolate`, `position: fixed` and `sticky`, and `contain: paint`, among others.
- `isolation: isolate` creates a stacking context on purpose, with no visual side effect. It is the clean way to fence a component's internal `z-index` values.
- Modal dialogs and popovers rendered in the top layer sit above every stacking context, without any `z-index`.
The number that does nothing
You have a dropdown menu hidden behind the page banner. The banner has z-index: 1, so you give the menu z-index: 100. Still hidden. You try 9999. Still hidden. A bigger number must work, so the model in your head is wrong.
The wrong model is that every element has a rank on one page-wide ladder. The right one is a set of nested groups.
Stacking contexts are sealed groups
A stacking context is a self-contained group of elements that are painted together, in a fixed order, as a unit. Inside the group, z-index values decide the order. From the outside, the whole group is one item, sitting at one position in its parent’s order. A child’s z-index can only compete with siblings in the same group; it can never escape to compete with elements outside.
Think of a stack of folders on a desk. Inside folder A there are pages numbered 1 to 9999. Folder B holds pages 1 to 5. If folder B is on top of folder A, then no page in A, even page 9999, can poke out above a page in B. The folder ordering decides everything about cross-folder overlap.
I reproduced the dropdown case in Chromium. An ancestor with opacity: 0.99 wrapped a positioned child with z-index: 9999, and a sibling box with z-index: 1 painted over it. Take away the opacity, and the child at 9999 won.
What makes a new context
The root <html> element is one. After that, an element creates one when it has, among others:
position: relativeorabsolutewith az-indexother thanautoposition: fixedorsticky(always)opacityless than 1transform,filter,perspective,clip-path,maskset to something other thannoneisolation: isolatewill-changenaming a property that would create onecontain: paint(or layout)- a flex or grid item with a
z-indexother thanauto
Many of those are unrelated to z-index at all. People add transform: translateZ(0) for performance or opacity for a fade and create a context by accident, which is why “it was working yesterday” bugs happen. Plain position: relative is not on the list: with z-index: auto the element joins its parent’s stacking order without a sealed group.
Painting order inside a context
Within one context, the order from back to front is roughly: the context root’s background, children with negative z-index, in-flow blocks, floats, inline content, positioned children with z-index: auto or 0 in source order, then positive z-index in increasing order. Ties go to whichever appears later in the HTML. z-index only applies to positioned elements and to flex and grid items, so setting it on a plain static block does nothing.
Fencing things on purpose with isolation
Because contexts are the cause of the problem, they are also the cure. A component library with a header at z-index: 10 and a popup at z-index: 20 should not leak those numbers to the whole page.
.card { isolation: isolate; }
That creates a context on .card with no visual change. Its inner values now rank only against each other. I verified a child at z-index: 9999 inside isolation: isolate being outranked by a later sibling at z-index: 1, which is exactly the containment you want. Combined with a handful of named levels in custom properties (--z-nav, --z-modal) for the page level, most z-index chaos goes away; see custom properties.
The top layer
Some UI needs to be above everything, so the browser provides a top layer. A <dialog> opened with showModal(), an element in the Popover API’s popover state, and fullscreen elements are rendered there, over all stacking contexts and with no z-index involved. For tooltips and menus, that is better than a very large number. Placement of those is covered in anchor positioning and popovers.
Finding the culprit
When an overlay misbehaves, walk up from the stuck element through its ancestors and check each one for the properties above. Browser DevTools list computed styles, so you can look for opacity, transform, filter and z-index on each ancestor. Kevin’s video uses Polypane, a paid browser the video is sponsored by, which has tools that show stacking contexts directly. The idea is the same without it, only slower.
What the video covers
Kevin Powell’s video with Kilian from Polypane, “The unknown fundamentals of CSS: Offset parents and Stacking Context explained”, about 21 minutes, from July 2023. By its chapter list it explores Polypane’s tools (from the start), explains offset parent (the containing block, from 6:08), and explains stacking context (from 12:53). The description says it is sponsored by Polypane through an affiliate link. The numbers and the isolation and top-layer material in this lesson are my own, since I could not read a transcript; the video uses Polypane’s tools, which this lesson does not require. The runner-up is a short 2024 video on a z-index fix.
Where this fits
Stacking contexts explain the strangest-looking bug in layout, and the same list of properties shows up again in transforms, where a transform quietly changes both stacking and the containing block. Next, overflow and scroll containers covers what happens when content is bigger than its box.
Check yourself
A dropdown with `z-index: 9999` appears behind a banner with `z-index: 1`. What is the most likely reason?
The dropdown lives inside an ancestor that creates its own stacking context (for example via `opacity`, `transform` or a positioned element with `z-index`), and that ancestor's level is below the banner's. The dropdown's 9999 only competes inside its context. I reproduced this in Chromium with `opacity: .99` on the ancestor.
Which of these creates a stacking context: `opacity: 0.99`, `position: relative` alone, `isolation: isolate`, `transform: translateX(0)`?
`opacity: 0.99`, `isolation: isolate` and `transform: translateX(0)` do. `position: relative` on its own does not, because without a `z-index` other than `auto` it just takes part in its parent's stacking order.
You want a component's internal `z-index: 10` header and `z-index: 20` menu to never compete with the rest of the page. What one-line change does that?
Put `isolation: isolate` on the component's root. That makes it a stacking context, so its internal values only rank against each other, and the whole component takes one slot in the page's stack.
Why does a `<dialog>` opened with `showModal()` appear above a `z-index: 99999` element?
It is promoted to the browser's top layer, a layer above all normal stacking contexts. Elements in it are painted over regular content regardless of any `z-index` in the page.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.