
z-index: 9999 Won't Save You: Stacking Contexts Explained
- كاتب موثّق
محتويات المقال
z-index: 9999, the carousel has z-index: 1, and someone bumps the menu to 99999. Nothing changes. Rendered in headless Chromium, a hit test at a point where the two overlap returns the carousel both times, and once the page scrolls, the carousel slides over the sticky header itself.
The common misconception
z-index always puts an element on top of every element with a lower one.
position: sticky from the header made the menu's 9999 beat the carousel's 1 at once. The name also suggests one z-axis shared by the whole page, so when a number loses, the natural reaction is a bigger number.
What actually happens
z-index ranks siblings inside one stacking context
z-index values of its children only mean something inside it.
4-6 for a child with z-index: 6 inside a parent with z-index: 4, which loses to a box at 5-0. In the hook, the sticky header forms a context at level 0 in the root, and the carousel sits at level 1. The menu's real position is 0-9999, which loses to 1 every time. Raising 9999 changes the second number, and the comparison is decided by the first.
Many properties create a stacking context
z-index is only one of the triggers, and that's why these bugs feel random. MDN's list includes:
the root element, and position: absoluteorrelativewith az-indexother thanautoposition: fixedandposition: sticky, with or withoutz-indexflex and grid items with a z-indexother thanautoopacitybelow 1,mix-blend-mode, andtransform,filter,backdrop-filter,clip-path,maskorperspectiveother thannoneisolation: isolate,will-changenaming one of those properties,contain: layoutorpaint, and size containers
opacity: 0.99 reproduced the bug, and so did backdrop-filter: blur(8px), the usual "frosted glass" header.
z-index needs a positioned box or a flex item
z-index does nothing. MDN says it applies to positioned elements, and to flex and grid items. In Chromium, a static box with z-index: 10 stayed under a later, overlapping position: relative sibling, while the same z-index on a flex item lifted it above its neighbor.
The painting order inside one context
The background and borders of the element that forms the context. Child stacking contexts with a negative z-index.Normal-flow blocks, then floats, then inline content and text. Positioned boxes with z-index: autoor0, in source order.Child stacking contexts with a positive z-index, lowest first.
z-index, such as by opacity, take their place in step 4, in source order. That's why the faded header lost to the carousel's z-index: 1.
.site-header { position: sticky; /* forms a stacking context */ top: 0;}.menu { position: absolute; z-index: 9999; /* ranks only inside .site-header */}.carousel { position: relative; z-index: 1; /* ranks in the root context */}:root { --z-carousel: 1; --z-header: 2;}.site-header { position: sticky; top: 0; z-index: var(--z-header); /* compete at the right level */}.menu { position: absolute; /* no z-index needed */}.carousel { position: relative; z-index: var(--z-carousel);}z-index is a global ranking | |
z-index creates layers | opacity below 1, transform, filter and more create contexts |
z-index works on any element | |
z-index: 99999 | showModal() goes to the top layer, above all of it |
How to apply it in your project
Find the context that traps you Start at the element that loses and walk up its ancestors in DevTools until you reach one that forms a stacking context. Compare that ancestor with the winner, inside the context they share. Fix it at that level Give the ancestor a z-indexjust above its competitor, or remove the property that created the context if nobody needs it. Small numbers are enough once you compare the right boxes.Keep a z-index scale Define the layers your app has once, as such as --z-headerand--z-toast, and use only those. A bare 9999 in a component is a sign that someone fought the wrong fight.Contain components on purpose Add isolation: isolateto a component's root when its internalz-indexvalues shouldn't leak out, and render modals and popovers in the top layer instead of racing numbers.
Key takeaways
z-indexorders boxes only within one stacking context, and the whole context is painted as one unit in its parent.Sticky and fixed positioning, opacitybelow 1,transform,filter,backdrop-filter,isolationand others create contexts, often by accident.z-indexonly works on positioned boxes and on flex or grid items.Fix stacking bugs on the ancestor that forms the context, with small numbers from a shared scale. For modals and popovers, use the top layer through showModal()or the Popover API instead of huge numbers.
التعليقات (0)