Why z-index Stops Working: Stacking Contexts Explained
The dropdown menu opens, and half of it disappears behind the hero image below. You give the menu z-index: 10 . Nothing. You try 100, then 9999, then the classic 2147483647, the largest value a 32-bit integer allows.

The dropdown menu opens, and half of it disappears behind the hero image below. You give the menu z-index: 10. Nothing. You try 100, then 9999, then the classic 2147483647, the largest value a 32-bit integer allows. The menu stays stubbornly underneath, and the hero image has no z-index at all.
I saw this exact bug filed more than any other layering issue during my years in cross-browser QA, and nearly every report blamed the browser. The browser was right every time. The z-index on the menu was working; it was just being compared against the wrong things. Once you know how stacking contexts group elements, the fix usually takes one line, and DevTools can show you which ancestor caused it in under a minute.
What z-index actually compares
The mental model most of us start with is a single global pile: every positioned element gets a number and the browser sorts them all. That is not how it works. The page is a tree of stacking contexts, and z-index only orders elements inside the same context.
Think of each stacking context as a sealed sheet of paper. Elements on that sheet can be layered among themselves using z-index. But when the browser paints the page, it places the whole sheet at one level relative to its siblings. A child with z-index: 9999 on a sheet that sits at level 1 can never rise above an element on a sibling sheet at level 2. The 9999 is only meaningful on its own sheet.
So in the dropdown example, the menu is probably inside a header that forms a stacking context with a lower position than the hero section, or the hero forms a context painted later in the document. Either way, the menu's number never enters the comparison you care about.
Properties that quietly create a stacking context
The root element creates one, and so does any positioned element with a z-index other than auto. Those are the cases people expect. The surprises come from properties that have nothing to do with layering on the surface. This is the list I keep pinned when debugging.
| Property or condition | Creates a context when | Where I usually find it |
|---|---|---|
position with z-index | z-index is anything but auto | Headers, cards, modals |
position: fixed or sticky | Always, even without z-index | Sticky navigation bars |
opacity | Value below 1, including 0.99 | Fade-in animations left at 0.99 |
transform | Any value other than none | Hover lifts, carousel slides, translateZ(0) hacks |
filter and backdrop-filter | Any value other than none | Frosted glass headers |
isolation: isolate | Always | Deliberate fixes |
will-change | Naming opacity, transform or similar | Performance tweaks copied from blog posts |
| Flex or grid child with z-index | z-index is set, even without position | Card grids |
contain: paint or layout | Set | Long lists tuned for rendering speed |
The one that catches teams most often, in my experience, is transform. An animation library adds transform: translateY(0) to a section after its entrance animation finishes. Nobody sees it in the source, because it is applied inline at runtime, and the section now traps every dropdown inside it.
The opacity: 0.99 case is a close second. Someone fades a header in, stops the animation one step short of 1, and the header becomes a context with an ordinary paint order.
Finding the culprit in DevTools
Guessing from the stylesheet wastes time because the culprit is often an inline style or a parent several levels up. I go straight to the browser.
In Chrome or Edge, open DevTools, press Ctrl+Shift+P (Cmd+Shift+P on a Mac), type "Layers" and open the Layers panel. It draws the page as a 3D stack you can rotate, and selecting a layer shows why the browser composited it, such as "has a 3D transform" or "has active animation". Firefox has no equivalent panel, so there I use the manual walk below, which honestly is often quicker for this particular bug anyway.
When a panel is not available, a manual walk works. Select the element that is losing, then click up the DOM tree one parent at a time. In the Computed tab, filter for z-index, transform, opacity, filter and position. The first ancestor with any of those values set, below the common ancestor it shares with the winning element, is your trap. On a typical marketing page this takes four to six clicks. More panels worth knowing are in my notes on DevTools features most developers never open.
Why "just raise the number" makes it worse
The standard advice in code review comments is to bump the z-index until it works, or to set up a z-index scale in variables: 100 for dropdowns, 200 for sticky headers, 300 for modals, and so on. A scale is fine as far as it goes. It does not solve the problem, because a scale assumes a single global pile, which we have established does not exist.
Raising numbers has a cost too. Each bump that seems to work is usually working because it put an element above something in the same context, not because it fixed the real cause. A few months later someone adds a transform to a parent and the menu breaks again, and the next developer raises it once more. I once audited a stylesheet with 41 different z-index values, the highest at 99999, on a page with maybe six things that actually needed layering. Specificity has a similar spiral, which I described in why specificity battles make stylesheets harder to maintain.
The better habit is to create contexts on purpose. Put isolation: isolate on each major component, such as a card, a header or a widget. That gives every component its own sheet, keeps its internal z-index values local, and stops them leaking out. Then order the components themselves with small values, 1 to 5, at the top level of the page.
Three fixes, in the order I try them
First, remove the accidental context. If the trapping ancestor has transform, opacity: 0.99 or a leftover will-change that serves no purpose, delete it. This fixes roughly half the cases I have seen.
Second, if the context is needed, raise the ancestor instead of the child. Give the header containing the dropdown position: relative; z-index: 2 so its whole sheet sits above the hero.
Third, take the element out of the tree. For menus, tooltips and dialogs, the top layer avoids the whole problem. A native <dialog> opened with showModal(), or any element using the popover attribute, renders above every stacking context on the page regardless of where it sits in the DOM. Support is solid in current Chrome, Edge, Firefox and Safari. Other layout bugs with similar roots, such as the sticky header that will not stick, are covered in the guide to position: sticky failures.
FAQ
Why does z-index not work on my element?
Either the element is not positioned and not a flex or grid child, so z-index is ignored, or an ancestor creates a stacking context that limits it.
Does opacity create a stacking context?
Yes, any opacity below 1 does. That includes values like 0.99 left over from animations.
What does isolation: isolate do?
It creates a new stacking context without any visual side effect, which makes it the cleanest way to contain z-index values inside a component.
Can a child ever appear above its parent's siblings?
Only if the parent does not form a stacking context, or if the child is moved to the top layer with a modal dialog or popover.
More in Front-End
Front-End
How to Find the Element Causing Horizontal Scroll
A tester filed a bug last March that said "page wobbles sideways on my phone". No screenshot, no device model.
Front-End
How Front-End Retry Code Quietly Overloads Your API
A payments endpoint we run went dark for about 90 seconds on a Tuesday afternoon.
Front-End
Event Delegation in JavaScript and When to Use It
You render a list of 300 rows, attach a click listener to each delete button, and everything works until new rows arrive from...