Here is a bug almost every frontend developer has shipped at least once.
You build a modal. You give it position: fixed and z-index: 9999. It works on the test page. Then someone drops it inside a product card, and suddenly the sticky header, which has a humble z-index: 10, is painted right over your modal.
So you try z-index: 99999. Then 2147483647, the largest value browsers accept. Nothing changes.
The number was never the problem. The browser never compared 9999 to 10 at all. It compared 10 to 0, and to understand where that zero came from, you need the one CSS concept that explains every "z-index is broken" bug: the stacking context.
The experiment
I reproduced the bug in a headless Chromium page with Playwright and asked the browser which element sits on top at a given point, using document.elementFromPoint(). This is the minimal version:
<header class="header">Header</header>
<div class="card">
<div class="modal">Modal</div>
</div>
.header {
position: sticky;
top: 0;
z-index: 10;
height: 150px;
}
.card {
transform: translateY(0); /* left over from a hover animation */
}
.modal {
position: fixed;
top: 0;
left: 0;
z-index: 9999;
}
Results at a point where header and modal overlap:
| Setup | Element on top |
|---|---|
Modal inside .card (with transform) |
header |
| Same modal, outside any transformed parent | modal |
Same modal, same z-index, opposite outcome. The only difference is one innocent-looking transform on an ancestor.
There was a second surprise. The "fixed" modal was not at the top of the viewport. Its getBoundingClientRect().top was 150, the top of the card. A transform on an ancestor also makes that ancestor the containing block for position: fixed descendants, so the modal was positioned relative to the card instead of the screen. Two bugs from one declaration.
What a stacking context actually is
Think of a stacking context as a sealed box of transparent sheets. Everything inside the box gets sorted by z-index, but only against other things in the same box. Once the box is sorted, the browser treats it as a single flat sheet and slides it into its parent box at the box's own level.
The CSS 2.1 specification describes it this way: z-index values of descendants are only meaningful relative to their own stacking context. A child can never climb above something that its parent context is already below.
Walk through the example step by step:
-
<html>forms the root stacking context. Everything starts here. -
.headerisposition: sticky, which always creates a stacking context. It joins the root at level 10. -
.cardhas atransform, so it creates its own stacking context. It has no z-index, so it is painted at level 0 in the root. -
.modalcreates a context too, but it lives inside.card. Its 9999 is compared only with the card's other children. - In the root, the browser compares
.header(10) with.card(0). The header wins, and it paints over the entire card, modal included.
This is why raising the modal's z-index never helps. You are making it the tallest person inside a room whose ceiling is already lower than the header.
The declarations that quietly create a box
If stacking contexts only came from z-index, this would be easy to spot. The problem is that many declarations written for visual reasons create one as a side effect. MDN's stacking context reference keeps the full list. These are the ones you will meet most often:
A few deserve special attention, because they show up in real codebases all the time:
-
opacity: 0.99from a fade-in animation that never resets to1. -
transformon hover effects or entrance animations, eventranslateY(0), which moves nothing. -
backdrop-filteron glassy navigation bars. -
will-change: transform, added "for performance," which creates the context before any animation even runs. -
container-type: inline-size, which is newer and easy to forget. Turning a component into a size container for container queries also makes it a stacking context.
Note the condition on the second row: position: relative or absolute creates a stacking context only when z-index is something other than auto. A relative element with no z-index is positioned but does not trap its children.
The order inside every box
Once you know where the boxes are, one more rule finishes the picture. Inside each stacking context, the browser paints in a fixed order, defined in Appendix E of CSS 2.1:
This order explains several behaviors that look like bugs:
"z-index does nothing on my element." A static (non-positioned) element does not take part in z-index layering. z-index only applies to positioned elements and to flex and grid items. Add position: relative or make the parent a flex container, and the same z-index suddenly works.
"My relative box covers the next section, and I never set a z-index." Positioned elements with z-index: auto paint in layer 6, above normal blocks in layer 3, regardless of DOM order.
"z-index: -1 vanished behind the page." A negative z-index places the element behind its siblings. It can never go below the background of its own stacking context, but if no ancestor forms a context, that context is the root, and the element can disappear behind an ancestor's background color.
When two elements share the same layer and z-index, the one that appears later in the DOM wins.
Debugging it in under a minute
When an element refuses to come forward, stop editing numbers and walk up the tree instead:
- Select the element in DevTools.
- Check each ancestor's computed styles for anything from the trigger list above.
- The first ancestor that creates a context is your ceiling. That ancestor's z-index, not your element's, is what competes with the element covering yours.
Chromium-based browsers also have a Layers panel (More tools, then Layers) that shows composited layers in 3D. It is not an exact map of stacking contexts, since compositing is an implementation detail, but it often makes the culprit obvious. Firefox's Inspector shows a small hint when z-index has no effect on an element.
Once you find the ancestor, you have three options:
-
Remove the trigger if it was accidental, like that leftover
translateY(0). - Raise the ancestor, not the child, if the whole component should sit higher.
-
Move the overlay out of the box, which is what portals in React, Vue, and other frameworks do by rendering the modal at the end of
<body>.
Making stacking contexts work for you
Stacking contexts are not the enemy. They are scope for z-index, and you can create them on purpose.
.card {
isolation: isolate;
}
isolation: isolate creates a stacking context with no visual side effect. Every z-index inside the card now competes only inside the card. A badge with z-index: 2 in one card can never fight a dropdown in another component. Pair this with a small, documented z-index scale for the top-level pieces (header, sidebar, overlays) and the arms race of ever-larger numbers ends.
The real fix for modals: the top layer
For modals, popovers, and menus, there is now a cleaner answer than portals. Browsers have a top layer, a rendering layer that sits above the entire document. Elements promoted to it ignore every stacking context they are nested in, and ignore z-index too.
Two built-in APIs put elements there:
<!-- Modal dialog -->
<dialog id="confirm">
<p>Delete this item?</p>
<form method="dialog"><button>Close</button></form>
</dialog>
<script>
document.getElementById("confirm").showModal();
</script>
<!-- Popover, no JavaScript needed -->
<button popovertarget="menu">Options</button>
<div id="menu" popover>...</div>
In my Playwright test, I put a <dialog> in the same page and called showModal(). At the point where the trapped modal and the header overlapped, the browser reported the dialog on top, even though it had no z-index at all.
The <dialog> element also gives you a styleable ::backdrop, focus handling, and closing with the Escape key. The Popover API became Baseline in 2024 and handles light dismiss for menus and tooltips.
A few limits worth knowing:
- Only
showModal()uses the top layer.dialog.show()or theopenattribute renders a normal, non-modal dialog that obeys stacking contexts. - Top layer elements stack in the order they were opened, so a popover opened from inside a modal dialog correctly appears above it.
- The element is still styled by its normal CSS, so inherited styles and selectors keep working. Only its painting position changes.
The mental model to keep
Next time something will not come forward, ask a different question. Instead of "how high is my z-index?", ask "which box am I in, and how high is that box?"
z-index is not a global ranking. It is a local ranking inside nested boxes, and many ordinary declarations create those boxes without telling you. Once you see the boxes, the bug stops being mysterious, and for overlays the top layer removes the problem entirely.
I am curious which trigger has bitten you hardest. For me, will-change added "for performance" is the sneakiest, because it does nothing visible until a modal disappears behind a header. If you have a stacking bug story, or a z-index scale that has survived a large codebase, I would love to read it in the comments.
Further reading
- MDN: Stacking context
- CSS 2.1, Appendix E: Elaborate description of stacking contexts
- MDN: The dialog element
- MDN: Popover API
- MDN: Containing block (why fixed elements move inside transformed parents)
If you want the bigger picture of how modern CSS handles the cascade itself, my earlier post on Cascade Layers, container queries and modern CSS architecture covers the other half of the "CSS is unpredictable" problem.




Top comments (2)
Hey, this article appears to have been generated with the assistance of ChatGPT or possibly some other AI tool.
We allow our community members to use AI assistance when writing articles as long as they abide by our guidelines. Please review the guidelines and edit your post to add a disclaimer.
Failure to follow these guidelines could result in DEV admin lowering the score of your post, making it less visible to the rest of the community. Or, if upon review we find this post to be particularly harmful, we may decide to unpublish it completely.
We hope you understand and take care to follow our guidelines going forward!
Thanks for flagging it, I appreciate the heads up. I'll read through the guidelines and add the disclaimer to the post. Sorry for the extra work on your end.