Why Is My z-index Not Working? Stacking Contexts Explained
You probably won’t find this on Reddit, but the CSS2 Recommendation of 12 May 1998 says a box that makes a stacking context gets two stack levels: one inside the context it creates (always 0) and one in the context it belongs to.
Every “z-index not working” thread ends with the same advice: set position: relative.
I came to this page as the researcher: on December 3, 2026 I read the W3C Positioned Layout and Containment drafts and the original CSS2 Recommendation of May 12, 1998 instead of the blogs that quote each other, then ran more than 60 stacking tests in Chrome 158.
Three dates matter.
On May 12, 1998 CSS2 added the z-axis and said z-index applies to “positioned elements”.
The Positioned Layout draft of October 7, 2025 still says positioned boxes
.
And MDN’s list, last modified September 28, 2026, names a property that did not make a stacking context in my test.
A z-index that does nothing is almost always one of two things: the element is not allowed to use it, or a parent has locked it into a smaller stack.
If you like a printed reference beside the keyboard, the CSS Visual Dictionary paperback on Amazon draws positioning and layers as pictures.
Short answer: z-index only ranks elements that are positioned (or flex or grid items), and only against neighbours in the same stacking context. If a parent has position with a z-index, opacity below 1, a transform or similar, your element’s number only counts inside that parent, so a z-index: 9999 can still lose to a 2 outside it.
The same four checks as text:
- Can this element take a z-index? It needs
positionother thanstatic, or to be a flex or grid item. Fix: addposition: relative. - Is the value really on that element? Read the computed style in DevTools;
automeans no number of its own. - Is an ancestor a stacking context? Opacity, transform, filter, isolation, will-change, contain and fixed or sticky position can all be one. Fix: raise that ancestor or move the popup out of it.
- Is it a modal? A
dialogopened withshowModal(), or a popover, sits above every z-index.
Why does z-index do nothing on some elements?
Because z-index applies to positioned boxes, not to every element. The 12 May 1998 CSS2 Recommendation, the first to define it, lists z-index as Applies to: positioned elements
with an initial value of auto, and today’s Positioned Layout draft says The z-index property applies to all positioned boxes
.
Flexbox and grid added a second way in. MDN lists a flex item or grid item with a z-index other than auto as a stacking-context maker, so those items take a z-index without any position. We tested all of it by checking which element sat on top at one point on screen.
| Test | Result |
|---|---|
a plain block with z-index: 99, earlier in the HTML, against a position: relative box with no z-index, later | the positioned box was on top |
the same two, the positioned box earlier and the z-index: 99 block later | the positioned box was still on top |
a flex item with z-index: 1 and no position | it took the z-index and made a stacking context |
a grid item with z-index: 1 and no position | the same |
a flex item with z-index: auto | no stacking context |
position: relative with no z-index | no stacking context |
position: relative; z-index: 0 | a stacking context |
So a plain div with z-index: 99 is ignored, and any positioned box, even one with no number, paints above it. Fix: add position: relative (it moves nothing without offsets). Our guide to CSS position covers what each value does.
What is a stacking context, and why does it ignore my big z-index?
A stacking context is a self-contained group of layers. The browser ranks everything inside it, then ranks the whole group as one unit against its neighbours, so a number inside the group never competes with numbers outside it.
The 1998 CSS2 text says the same in its own words: A local stacking context is atomic; boxes in other stacking contexts may not come between any of its boxes.
It adds one detail that few pages repeat. An element that makes a context generates a box that has two stack levels
: one for the context it creates, always ’0’
, and one for the context it belongs to, set by its z-index.
MDN’s current page makes the same point as the z-index values of its child stacking contexts only have meaning within its parent’s stacking context
.
The numbers in the picture, as text:
- Root context: Box A at level 1, Box B at level 2.
- Inside Box A: the dropdown at z-index 9999, ranked only against A’s own contents.
- Result: B is painted over all of A, dropdown included.
| Setup | What was on top where they overlapped |
|---|---|
Box A position: relative; z-index: 1 holding a child at z-index: 9999, Box B position: absolute; z-index: 2 | Box B |
the same, with Box A raised to z-index: 3 and its child at z-index: 1 | A’s child |
Say a dropdown menu sits inside a header with position: relative; z-index: 1, and a cookie banner outside it has z-index: 2. The dropdown can have any number and the banner still covers it. The fix is the number on the header.
Which CSS properties create a stacking context?
More than the list most people remember. In Chrome 158, 23 of the 35 values we tried made one, including opacity: 0.99, translate, will-change: opacity and contain: layout; 12 did not, such as opacity: 1 and overflow: hidden.
For each row we put the property on a parent, a child inside it at z-index: 10, and a sibling of the parent at z-index: 5 over the same point. If the sibling won, the parent had trapped the child; if the child won, it had not.
| Made a stacking context (23) | Did not (12) |
|---|---|
position: fixed, position: sticky (no z-index on either) | opacity: 1 |
opacity: 0.99 | transform: none |
transform: translateX(0) | filter: none |
translate: 0 0, rotate: 0deg, scale: 1 | will-change: top |
filter: blur(0), backdrop-filter: blur(1px) | will-change: scroll-position |
perspective: 500px | contain: style, contain: size |
clip-path: inset(0), mask-image: linear-gradient(...) | container-type: inline-size, container-type: size |
mix-blend-mode: multiply, isolation: isolate | overflow: hidden, overflow: auto |
will-change: transform, will-change: opacity | box-shadow |
contain: layout, contain: paint, contain: content, contain: strict | |
content-visibility: auto, view-transition-name: x | |
an @keyframes opacity animation ending at 0.5 with fill-mode: forwards |
The pattern: a value that changes nothing (opacity: 1, transform: none, filter: none) makes no context, but a value that looks like nothing (translateX(0), blur(0), opacity: 0.99) does.
That is why a stray transform: translateZ(0) added for “performance” so often breaks a dropdown. MDN says it this way: Stacking contexts are treated atomically as a single unit in the parent stacking context.
Where do MDN, the specs and Chrome disagree about stacking contexts?
They disagree in four places. We list each claim beside what Chrome 158 did; the first two rows are ones we found no ranking page correcting.
| Question | One source | Another source | Chrome 158 |
|---|---|---|---|
Does container-type make a stacking context? | MDN: yes, Element with a container-type value size or inline-size set | Containment Level 3 says size and inline-size apply layout containment; Level 2 says The layout containment box creates a stacking context | no for both, checked two ways (trapping a z-index 10 child, and hiding a z-index -1 child behind the host’s background) |
| Is MDN’s list complete? | MDN’s list has no view-transition-name or content-visibility | we found no spec text to compare for these two | view-transition-name: x and content-visibility: auto both trapped the child |
| Which elements take a z-index? | CSS2 (1998): positioned elements | MDN: flex and grid items with a z-index other than auto too | flex and grid items with no position took it; a plain block did not |
Does z-index: auto ever make a context? | CSS2 (1998): auto does not establish a new local stacking context | Positioned Layout 3: Fixed and sticky positioned boxes nonetheless form a stacking context | fixed and sticky with no z-index trapped the child |
The first row is the surprising one. We cannot say why Chrome 158 differs from the draft text; it may be a deliberate browser choice, and we did not test Firefox or Safari. If your layout depends on container-type trapping a z-index, set a z-index or isolation: isolate on that element instead of relying on it.
How do I find which ancestor is trapping my element?
Walk up from the element and print every ancestor that makes a stacking context. Open the page, run this in the DevTools console after replacing the selector, and read the list from the bottom up: the first line above your element is the nearest trap.
function findTrap(el) {
var out = [];
for (var n = el.parentElement; n && n !== document.documentElement; n = n.parentElement) {
var s = getComputedStyle(n), why = [];
if (s.position === 'fixed' || s.position === 'sticky') why.push('position:' + s.position);
if (s.position !== 'static' && s.zIndex !== 'auto') why.push('z-index:' + s.zIndex);
if (+s.opacity < 1) why.push('opacity:' + s.opacity);
['transform', 'translate', 'rotate', 'scale', 'filter', 'backdropFilter', 'perspective', 'clipPath', 'maskImage']
.forEach(function (p) { if (s[p] && s[p] !== 'none') why.push(p + ':' + s[p]); });
if (s.mixBlendMode !== 'normal') why.push('mix-blend-mode');
if (s.isolation === 'isolate') why.push('isolation');
if (/layout|paint|strict|content/.test(s.contain)) why.push('contain:' + s.contain);
if (/transform|opacity|filter/.test(s.willChange)) why.push('will-change:' + s.willChange);
if (why.length) out.push(n.tagName.toLowerCase() + (n.id ? '#' + n.id : '') + ' ' + why.join(', '));
}
return out;
}
console.table(findTrap(document.querySelector('.dropdown')));
We ran it on a dropdown at z-index: 9999 inside a position: relative; z-index: 1; opacity: 0.9 wrapper, and it printed that wrapper with z-index:1, opacity:0.9. It does not list flex or grid items, content-visibility or view-transition-name, so if it prints nothing, check those by hand in the Computed tab.
How do I bring an element to the front without z-index: 99999?
Raise the level of the ancestor that is trapping it, or take the element out from under that ancestor. A bigger number on the trapped element never helps, as the A and B test above showed.
Here’s a moment almost every newbie knows: you type z-index: 9999, nothing changes, so you add another 9, then another, and the menu still hides behind the banner. It is not the number. Somewhere above it a parent has quietly wrapped everything into a smaller stack.
| Fix | Example | Who it suits | Where it fails |
|---|---|---|---|
| Raise the trapping ancestor | header z-index: 3 above a banner at 2 | a menu inside one header or card | the whole ancestor now covers its neighbours |
| Remove the trigger | delete a needless transform: translateZ(0) | effects added “just in case” | the animation may need it |
Move the popup to the end of body | a tooltip appended to body | menus, tooltips and toasts | it no longer inherits the parent’s position without scripting |
| Use the top layer | <dialog> with showModal(), or a popover | modals and pop-ups | needs the dialog or popover markup |
| Cap a component on purpose | .card { isolation: isolate; } | keeping a card’s inner z-index values from leaking | the card is now one layer, so its parts cannot rise above its neighbours |
Our pick is to keep one small scale of z-index values per page (for example 1 for sticky headers, 2 for banners) set on a few wrappers, give each component’s inner layers values of 1 to 3 inside a wrapper that uses isolation: isolate, and use the top layer for anything modal. That way nobody needs a 99999.
Does z-index beat a dialog or popover?
No. An element in the top layer is painted above everything in the page, whatever its z-index. In Chrome 158, a modal dialog opened with showModal() and a popover opened with showPopover() both sat above a sibling at z-index: 2147483647.
Ancestors did not matter either: a dialog inside a transformed parent with z-index: 1, even with z-index: -5 on the dialog itself, still beat that z-index 2147483647 neighbour. MDN lists Element placed into the top layer
among stacking-context makers, and Can I use puts the dialog element at 97.27% of users (data file updated November 28, 2026).
| Setup | On top |
|---|---|
modal dialog, no z-index, against a box at z-index: 2147483647 | the dialog |
dialog inside a transformed z-index: 1 parent, dialog at z-index: -5, same neighbour | the dialog |
popover="manual" opened with showPopover(), same neighbour | the popover |
What is the highest z-index value, and who wins a tie?
In Chrome 158 the highest z-index is 2147483647, the largest signed 32-bit integer. Bigger numbers are not an error: z-index: 2147483648 and z-index: 99999999999 both came back from getComputedStyle as 2147483647.
That means two clamped values tie, and a tie goes to the later element in the HTML. When we put z-index: 2147483648 first and 2147483647 second, the second one won, so typing a bigger number gained nothing.
| Setup | On top |
|---|---|
2147483648 first, 2147483647 later | the later one (both read as 2147483647) |
99999999999 first, 2147483647 later | the later one |
2147483647 first, 2147483646 later | the first one |
two boxes at z-index: 7 | the one later in the HTML |
z-index: 0 first, auto later | the later one |
z-index: auto first, 0 later | the later one |
The Positioned Layout draft says auto boxes are painted as if those elements did generated new stacking contexts
, but with their descendants taking part in the current context. The difference between auto and 0 is therefore not their order against each other, it is whether the box traps its children.
Why does my sticky header go under the content?
A sticky header with no z-index sits at the default level, so any later element that makes a stacking context paints over it. Give the header a z-index of 1 and it stays on top of such elements, until a later element at 2 or higher appears.
| Header | Later card | On top |
|---|---|---|
position: sticky; top: 0, no z-index | transform: translateZ(0), no z-index | the card |
position: sticky; top: 0; z-index: 1 | transform: translateZ(0), no z-index | the header |
position: sticky; top: 0; z-index: 1 | position: relative; z-index: 2 | the card |
So the cure for a sticky header that vanishes is z-index: 1, and the cure for a card that cuts across it is to keep cards below that number. Can I use puts position: sticky at 97.25% of users.
Why does z-index: -1 put my element behind its parent?
A negative z-index sends the element behind its nearest stacking context’s contents, which can be behind the parent’s own background. In Chrome 158, a child at z-index: -1 inside a plain green parent was hidden behind the parent, and the hit test landed on the parent.
Put isolation: isolate on that parent and the same child came back in front of the parent’s background, because the parent had become the context. We saw the same with opacity: 0.99 and contain: layout as the trigger. This is the standard trick for a decorative layer that sits behind text but above the card’s background.
| Parent | Child at z-index: -1 |
|---|---|
| plain block with a background | behind the parent’s background |
the same with isolation: isolate | above the parent’s background |
the same with opacity: 0.99 or contain: layout | above the parent’s background |
The 1998 CSS2 Recommendation closes its z-index section with an example of four boxes, where the stack levels are “text2”=0, “image”=1, “text3”=2, and “text1”=3
, and the text2 level is inherited from the root box
. Count that today: a box with no z-index is level 0, so only three of the four ever needed a number.
Which means for your day-to-day work: you almost never need many different numbers. If a layout needs a number above 10, count the stacking contexts between your element and the root, because the cure is nearly always a lower number on the right ancestor.
How we researched this
- Specifications read on December 3, 2026: CSS Positioned Layout Module Level 3 (Working Draft, 7 October 2025), CSS Containment Module Level 2 (Working Draft, 17 September 2022) and Level 3 (Working Draft, 18 August 2022), and the CSS 2.2 draft (section 9.9.1 and Appendix E).
- The unusual source: the CSS2 Recommendation of 12 May 1998, chapter 9 “Visual formatting model”, section 9.9 “Layered presentation”. We used its two-stack-levels sentence, its “positioned elements” scope and its four-box example. We also searched the CSS1 Recommendation (17 December 1996, as revised 11 January 1999) for
z-indexand found no mention. - Documentation: MDN’s page on stacking context (last modified September 28, 2026) and the Can I use data file (updated November 28, 2026).
- Our own tests: four small pages in headless Chrome 158.0.8127.91 (macOS): 46 setups checked for trapping a child, 6 more checked a second way with a negative z-index, and 19 clashes of positions, ties, maximum values, top layer, sticky headers and negative values. In every case we asked the browser which element was on top at one point. One browser, one run each, except the container-type rows, which we ran twice.
- What we did not do: we did not test Firefox or Safari, so the numbers are Chrome’s, we did not search autocomplete beyond the title’s own phrasing, and we counted no user threads or surveys.
Open a page you made, paste the finder from this page into the console, and point it at your dropdown or tooltip. One line of output usually names the parent that is holding it in, and the fix is a small number on that parent. Ten minutes of that and z-index stops being a guessing game.
Sources and further reading
- W3C: CSS2 Recommendation, chapter 9 Visual formatting model — 12 May 1998, section 9.9: two stack levels per context, "positioned elements", the four-box example
- W3C: CSS1 Recommendation — 17 December 1996, revised 11 January 1999: no z-index property
- W3C: CSS Positioned Layout Module Level 3 — Working Draft, 7 October 2025: z-index applies to all positioned boxes; fixed and sticky form a stacking context
- W3C: CSS Containment Module Level 2 — Working Draft, 17 September 2022: layout and paint containment create a stacking context
- W3C: CSS Containment Module Level 3 — Working Draft, 18 August 2022: container-type size and inline-size apply layout containment
- W3C: CSS 2.2, visual formatting model — draft: section 9.9.1 z-index and stack levels
- W3C: CSS 2.2, Appendix E elaborate description of stacking contexts — painting order, negative and positive z-index layers
- MDN Web Docs: Stacking context — last modified 28 September 2026: the list of features that create a stacking context
- Can I use: dialog element — 97.27% of users, data file updated November 28, 2026
- Can I use: position: sticky — 97.25% of users, data file updated November 28, 2026
Questions and answers
Why is my z-index not working?
Usually the element is not positioned, or a parent makes a stacking context that holds it in a smaller stack. z-index only applies to positioned elements and flex or grid items, and only ranks against neighbours in the same stacking context. Add position: relative, then look for a parent with opacity, transform, filter or its own z-index.
Does z-index work on position: static?
No, not on a plain block. In Chrome 158 a static block with z-index: 99 lost to a positioned box with no z-index at all. Flex and grid items are the exception: they take a z-index with no position.
What is a stacking context in CSS?
A stacking context is a group of layers ranked together and then treated as one unit against its neighbours. A child’s z-index only counts inside it. The root element makes one, and so do positioned elements with a z-index, opacity below 1, transform, filter, isolation: isolate and more.
How do I make an element appear on top of everything in CSS?
For a modal or pop-up, use a dialog opened with showModal() or a popover, which sit in the top layer above any z-index. For ordinary layers, raise the z-index of the ancestor that makes the stacking context rather than the element itself.
What is the maximum z-index value?
In Chrome 158 it is 2147483647. Larger numbers such as 2147483648 were read back as 2147483647, so they tie, and the element later in the HTML wins. Typing a bigger number never helps.