Fixing z-index Bugs: Stacking Contexts and a Named Layer Scale
Why z-index 9999 still loses, which CSS properties create stacking contexts, and how to replace magic numbers with a named z-index scale of tokens.

A dropdown hides behind a sticky header. Someone raises its z-index to 999. It still hides. Someone else tries 9999, then 99999. The menu stays behind the header, and the stylesheet now contains three magic numbers that nobody understands. The number was never the problem. This guide explains the real cause, stacking contexts, and shows how to clean up with the CSS z-index Manager and Stacking Context Map.
Stacking contexts in one paragraph
z-index values are only compared between elements in the same stacking context. A stacking context is a group that the browser paints as one unit. The root element forms one, and so does any positioned element with a numeric z-index. Once an element is inside a context, it can rise above its siblings in that context, but never above the context's own siblings. A dropdown with z-index 9999 inside a header with z-index 10 is, from the outside, a 10.
The properties that create contexts quietly
Many rules create a context without mentioning z-index at all:
opacitybelow 1- any
transform,filter,backdrop-filter,clip-pathormask isolation: isolatemix-blend-modeother than normalwill-changenaming transform, opacity or filtercontainwith paint, layout, strict or contentposition: fixedorstickycontainer-typeof size or inline-size
A hero section with transform: translateZ(0) added for smoother scrolling is a common hidden trap.
Step 1: find every z-index
Paste your CSS into the conflict finder. Each rule with z-index is listed with its line, selector and value, plus two kinds of notes. The first is what else in that rule creates a context. The second is a list of problems: no position in the rule (z-index only works on positioned elements and flex or grid items), position static, values of 9999 and above, values that are not in your scale, CSS variables that are not defined, and negative values. A separate box groups different selectors that share the same value. When two such components overlap, whichever comes later in the HTML is painted on top, which is usually an accident.
Step 2: define named layers
Open the named scale tab. The starting scale has below, base, raised, dropdown, sticky, fixed, overlay, modal, popover, toast and tooltip, with values from -1 to 1700 and gaps of 100 above 1000. Rename them for your product, adjust values, write a note about what belongs in each layer, and delete what you do not need. A warning appears if two layers share a number. Re-space by 100 keeps -1, 0 and 1 and evenly spaces the rest.
Then export. The tool writes CSS custom properties, SCSS variables with a map, a design-tokens JSON file, a Tailwind theme extension or a TypeScript constant. Components should use only these names from now on.
Step 3: rewrite the stylesheet
Back on the conflict tab, the rewritten CSS replaces each raw number with var(--z-name) for the nearest layer. It is a starting point, not a final answer: two components that shared 1050 might both become modal, when one of them is really a toast. Review each change in the pull request.
Step 4: model the tree when the bug is structural
When a high value still loses, open the stacking map and model the part of the page involved. Add components with their parent, z-index (blank means auto), DOM order, and tick other context creator if a transform, opacity or similar property applies. The map paints them in the order the CSS 2.1 rules define: inside each context, negative values first, then auto and zero in document order, then positive values, with nested contexts painted as one block. Each row shows the full path, such as header (100) › dropdown (9999), which makes it obvious why the dropdown loses to a modal at 1050 on the root.
Typical fixes
- Move the dropdown or modal out of the constrained parent, for example by rendering it in a portal at the end of the body.
- Remove the accidental context: drop the unnecessary transform or will-change.
- Raise the parent context instead of the child.
- Use
isolation: isolateon components on purpose, so their inner z-index values never leak out. - For dialogs and popovers, use the native dialog element or the popover attribute; they live in the top layer above all z-index values.
What the tool cannot see
It reads CSS text and the model you build. It does not know which elements overlap on screen or what JavaScript changes at run time, so always confirm a fix in the browser. Used together, though, the finder, the scale and the map turn z-index from guesswork into a small, documented system.



