Stacking Contexts and z-index: Why Your z-index Doesn't Work
How browsers decide what paints on top: the painting order in a stacking context, which elements z-index applies to, every property that creates a stacking context, why z-index: 9999 still loses, isolation: isolate, and a layering system.
kvi5hnuCode Sprout team member9 min readOn this page
When elements overlap, the browser has to decide which one is drawn on top. Usually it's simple: later elements in the HTML paint over earlier ones. z-index changes the order, but only within limits that confuse almost everyone at first: a dropdown with z-index: 9999 can still end up under a header with z-index: 2. The reason is stacking contexts.
The painting order
Within a stacking context, the browser paints in layers, from bottom to top:
- the stacking context element's own background and borders
- descendants with negative
z-index - backgrounds and borders of in-flow blocks, in source order
- floats
- inline content: text, images, inline boxes
- positioned descendants with
z-index: autoor0, and other stacking contexts such as elements withopacityortransform, in source order - descendants with positive
z-index, lowest first; ties in source order
So without any z-index, a positioned element (step 6) paints over non-positioned siblings (steps 3 to 5), even if it comes earlier in the HTML. That's why adding position: relative alone can change what's on top.

The second and fourth panels are the surprising ones: neither has a z-index, but position: relative and opacity both move a box from the in-flow steps up to step 6.
z-index
| Value | Meaning |
|---|---|
auto (initial) | painted at step 6 with its parent's context; doesn't create a new stacking context |
an integer, such as 1, -1, 10 | the element's level within its stacking context, and it creates a new stacking context |
z-index only applies to:
- positioned elements, whose
positionisn'tstatic; - flex items and grid items, even when they aren't positioned.
On a static block, z-index has no effect. That's the first thing to check when it seems ignored.
Stacking contexts
A stacking context is a group of elements painted together, as one unit, by the order above. The key rule:
z-index values only compete within the same stacking context. A whole stacking context is placed in its parent as a single layer, at its own
z-index.

A .menu with z-index: 999 inside a .card with z-index: 1 is at level 999 inside the card. The card as a whole is at level 1 in the page. A .header sibling at level 2 is above the card, and so above everything inside it, including the menu. No z-index on the menu can change that.

None of these declarations mentions stacking, yet each one makes the card a stacking context and traps the menu's 999 inside it; contain: paint also clips the menu to the card.
What creates a stacking context
The root element creates the first one. Tested in Chrome, these also create one:
| Creates a stacking context | Doesn't |
|---|---|
position: relative or absolute with a z-index other than auto | position: relative without z-index |
position: fixed or sticky, always | overflow: hidden or auto |
a flex or grid item with a z-index other than auto | display: flex or grid on the container |
opacity below 1 | contain: style, container-type |
transform, translate, rotate, scale, perspective, other than none | will-change for properties that don't create one, such as top |
filter, backdrop-filter, other than none | |
clip-path, mask, mask-image and the other mask properties, other than none | |
mix-blend-mode other than normal | |
isolation: isolate | |
contain: layout, paint, strict or content | |
will-change naming any property above | |
view-transition-name other than none | |
| elements in the top layer: modal dialogs, popovers, fullscreen |
Note translate: 0: any value other than none counts, even one that doesn't move anything, and so does opacity: 0.99. Animations are a frequent surprise: while a transform or opacity animation runs, the element is a stacking context, and its children's z-index stop competing with the rest of the page.
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
.header {
position: relative; z-index: 2;
background: #2f7d4f; color: white; padding: 0.75rem; border-radius: 8px;
}
.card {
position: relative; z-index: 1;
margin-top: -0.25rem;
background: #f4efe6; border: 2px solid #8a3b12; border-radius: 8px;
padding: 2.5rem 0.75rem 0.75rem;
}
.menu {
position: absolute; z-index: 999;
top: -2.25rem; left: 4rem;
inline-size: 9rem; padding: 2.4rem 0.5rem 0.4rem;
background: #fde2e1; border: 2px solid #b3261e; border-radius: 8px;
}
label { display: block; margin-top: 1rem; }
body:has(#fix:checked) .card { z-index: 3; } /* the card's whole context moves above the header */
The menu stays under the header whatever its z-index, because it's compared inside the card. Raising the card itself is what moves the menu up.
isolation: isolate
isolation: isolate creates a stacking context and does nothing else: no positioning, no transparency, no transform. It's the cleanest way to contain a component's z-index values, so they can't leak out and fight with the rest of the page:
.card { position: relative; isolation: isolate; } /* the containing block, and layers stay inside it */
.card .badge { position: absolute; z-index: 2; }
.card .overlay { position: absolute; inset: 0; z-index: 1; }
.card .bg { position: absolute; inset: 0; z-index: -1; } /* behind the content, but in front of the page */
Without the isolation, the card isn't a stacking context, so z-index: -1 puts .bg at the negative layer of the nearest ancestor that is one, often the page itself. That layer is painted before in-flow blocks, so .bg ends up behind the card's own background, where you can't see it. With isolation: isolate on the card, -1 means "behind the card's content", which is what you meant.

In the first panel the child isn't missing: it's painted at the root's negative layer, under every block background above it.
It also limits mix-blend-mode (article 99), which blends only with layers inside the same stacking context.
Debugging z-index
When something won't come to the front:
- Is it positioned, or a flex or grid item? If not,
z-indexdoes nothing. - Which stacking context is it in? Walk up its ancestors and look for any property in the table above. The element's
z-indexonly competes with siblings in that context. - Raise the right element: the ancestor that is the stacking context, so the whole group moves.
- Remove accidental contexts: a leftover
transform: translateZ(0),will-change: transform, oropacity: 0.99.
In DevTools, check each ancestor's computed styles for the properties in the table above; that's usually the fastest way to find the context that traps an element.
A layering system
Random numbers like z-index: 9999 escalate: someone else adds 99999. Define a small scale with custom properties, and keep components isolated:
:root {
--z-dropdown: 10;
--z-sticky: 20;
--z-overlay: 30;
--z-toast: 40;
}
.site-header { position: sticky; top: 0; z-index: var(--z-sticky); }
.dropdown { position: absolute; z-index: var(--z-dropdown); }
For modal dialogs, popovers, menus and toasts, the top layer (article 60) is better than any z-index: elements shown with showModal() or the popover attribute are painted above everything else in the page, whatever the stacking contexts around them, and aren't clipped by overflow on their ancestors.
What's current
isolation: isolateworks in all browsers.- The individual transform properties (
translate,rotate,scale) create stacking contexts liketransform. view-transition-namecreates a stacking context.- The top layer, through
<dialog>and thepopoverattribute, solves most "always on top" problems withoutz-index.
Common questions
Why doesn't z-index work on my element?
It isn't positioned and isn't a flex or grid item, or it's inside a stacking context that is itself below the element you're trying to cover.
Why is my z-index: 9999 dropdown under a z-index: 2 header?
An ancestor of the dropdown creates a stacking context with a lower level than the header. Raise that ancestor, or move the dropdown out, for example with a popover.
Why does adding opacity or transform change what's on top?
Both create a stacking context, which groups the element and its descendants into one layer.
What does isolation: isolate do?
It creates a stacking context and has no other effect. Use it to keep a component's z-index values inside it.
Why is my element with z-index: -1 invisible?
It's painted at the negative layer of the nearest stacking context, below in-flow content, so an ancestor's opaque background can cover it. Put isolation: isolate on the element you want it to sit behind.
Summary
- Inside a stacking context, the paint order is: background, negative z-index, blocks, floats, inline content, positioned
auto/0, then positive z-index. z-indexapplies to positioned elements and to flex and grid items; an integer value creates a stacking context.- Many properties create stacking contexts:
opacitybelow 1, transforms, filters, masks,clip-path, fixed and sticky positioning,isolation: isolate, containment andwill-change. z-indexonly competes within the same stacking context; raise the context, not the child.- Use
isolation: isolateto contain components, a smallz-indexscale, and the top layer for overlays.
Practice questions
A tooltip with position: absolute; z-index: 50 inside .card { transform: translateY(-2px); } appears under the next card. Why, and what are two fixes?
Answer
The transform makes each card a stacking context, so the tooltip's z-index only counts inside its card, and the next card, later in the source, paints over the whole first card. Raise the hovered card (.card:hover { position: relative; z-index: 1; }), or show the tooltip as a popover in the top layer.
Which of these create a stacking context: position: relative, opacity: 1, opacity: 0.5, isolation: isolate, overflow: hidden?
Answer
opacity: 0.5 and isolation: isolate. position: relative needs a z-index too, opacity: 1 is the default, and overflow: hidden doesn't create one.
A decorative ::before with position: absolute; z-index: -1 disappears behind the card's background instead of sitting behind the card's text. Fix it.
Answer
.card { position: relative; isolation: isolate; }
The card becomes the stacking context, so -1 puts the pseudo-element behind the card's content but above the page.
Two siblings both have position: relative; z-index: 1. Which is on top?
Answer
The one later in the source. Equal z-index values in the same stacking context paint in document order.
Previous: Fixed and Sticky Positioning: Headers, Modals and Sticky Sections
Next: Anchor Positioning: Tethering Tooltips and Menus With anchor() and position-area

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.