WebTech1223411 logo WebTech1223411Web tech, read closely
Front-End

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.

Overlapping translucent layers of a web page drawn in a 3D browser layers panel

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 conditionCreates a context whenWhere I usually find it
position with z-indexz-index is anything but autoHeaders, cards, modals
position: fixed or stickyAlways, even without z-indexSticky navigation bars
opacityValue below 1, including 0.99Fade-in animations left at 0.99
transformAny value other than noneHover lifts, carousel slides, translateZ(0) hacks
filter and backdrop-filterAny value other than noneFrosted glass headers
isolation: isolateAlwaysDeliberate fixes
will-changeNaming opacity, transform or similarPerformance tweaks copied from blog posts
Flex or grid child with z-indexz-index is set, even without positionCard grids
contain: paint or layoutSetLong 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.

MC
Maren Castellanos

Maren worked in cross-browser QA before editing, and knows the DevTools Network tab better than most people know their inbox. She writes about how browsers load and render pages and about the tools that show what is really happening.

More posts by Maren

More in Front-End

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp foxstudiosscratch gamevui123 gleasongrade greetingsday hacklabasu haskell iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten qvantor rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot skrtips spotlesshaven strcoding tetapatrium topselller uneplanteartificielle vipertechcr wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto