Learn CSS Visually - CSS explained one diagram at a time - flexbox, grid, the box model and more

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.

By · Updated

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.

Four numbered boxes read top to bottom. Box 1 asks whether the element can take a z-index, which needs position other than static or a flex or grid item. Box 2 asks whether the value is really set on that element. Box 3, highlighted in red, asks whether an ancestor makes a stacking context, with opacity, transform, filter, isolation, will-change, contain and fixed or sticky position as examples. Box 4 asks whether a modal should use a dialog or popover.
The four checks in order; step 3 is the one that hides in a parent.

The same four checks as text:

  1. Can this element take a z-index? It needs position other than static, or to be a flex or grid item. Fix: add position: relative.
  2. Is the value really on that element? Read the computed style in DevTools; auto means no number of its own.
  3. 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.
  4. Is it a modal? A dialog opened with showModal(), 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.

Which elements take a z-index — measured by Learn CSS Visually in Chrome 158 (158.0.8127.91, macOS), December 3, 2026, by testing which element was on top at one overlapping point
TestResult
a plain block with z-index: 99, earlier in the HTML, against a position: relative box with no z-index, laterthe positioned box was on top
the same two, the positioned box earlier and the z-index: 99 block laterthe positioned box was still on top
a flex item with z-index: 1 and no positionit took the z-index and made a stacking context
a grid item with z-index: 1 and no positionthe same
a flex item with z-index: autono stacking context
position: relative with no z-indexno stacking context
position: relative; z-index: 0a 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.

Left side: a root stacking context holding Box A with position relative and z-index 1, inside it a dropdown with z-index 9999, and under it a sibling Box B with z-index 2. Right side: the layers the browser stacks, with Box B on top at level 2 and Box A below it at level 1, the 9999 dropdown still inside A and under B. A note says the fix is to give A a level above B, for example 3.
Why z-index 9999 inside A still sits under B at 2; the whole of A is one layer.

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.
The trap and its fix, measured by Learn CSS Visually in Chrome 158, December 3, 2026
SetupWhat 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: 2Box B
the same, with Box A raised to z-index: 3 and its child at z-index: 1A’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.

35 values tested — measured by Learn CSS Visually in Chrome 158, December 3, 2026. “Made a context” means the child at 10 lost to the sibling at 5
Made a stacking context (23)Did not (12)
position: fixed, position: sticky (no z-index on either)opacity: 1
opacity: 0.99transform: none
transform: translateX(0)filter: none
translate: 0 0, rotate: 0deg, scale: 1will-change: top
filter: blur(0), backdrop-filter: blur(1px)will-change: scroll-position
perspective: 500pxcontain: style, contain: size
clip-path: inset(0), mask-image: linear-gradient(...)container-type: inline-size, container-type: size
mix-blend-mode: multiply, isolation: isolateoverflow: hidden, overflow: auto
will-change: transform, will-change: opacitybox-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.

Where the sources differ — compiled by Learn CSS Visually from MDN’s stacking context page (last modified September 28, 2026), CSS Containment Level 2 (Working Draft, 17 September 2022) and Level 3 (Working Draft, 18 August 2022), CSS Positioned Layout Level 3 (Working Draft, 7 October 2025), the 1998 CSS2 Recommendation and our Chrome 158 tests, December 3, 2026
QuestionOne sourceAnother sourceChrome 158
Does container-type make a stacking context?MDN: yes, Element with a container-type value size or inline-size setContainment Level 3 says size and inline-size apply layout containment; Level 2 says The layout containment box creates a stacking contextno 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-visibilitywe found no spec text to compare for these twoview-transition-name: x and content-visibility: auto both trapped the child
Which elements take a z-index?CSS2 (1998): positioned elementsMDN: flex and grid items with a z-index other than auto tooflex 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 contextPositioned Layout 3: Fixed and sticky positioned boxes nonetheless form a stacking contextfixed 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.

Ways to fix a trapped element — compiled by Learn CSS Visually from the W3C drafts above and our Chrome 158 tests, December 3, 2026
FixExampleWho it suitsWhere it fails
Raise the trapping ancestorheader z-index: 3 above a banner at 2a menu inside one header or cardthe whole ancestor now covers its neighbours
Remove the triggerdelete a needless transform: translateZ(0)effects added “just in case”the animation may need it
Move the popup to the end of bodya tooltip appended to bodymenus, tooltips and toastsit no longer inherits the parent’s position without scripting
Use the top layer<dialog> with showModal(), or a popovermodals and pop-upsneeds the dialog or popover markup
Cap a component on purpose.card { isolation: isolate; }keeping a card’s inner z-index values from leakingthe 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).

Top layer against z-index — measured by Learn CSS Visually in Chrome 158, December 3, 2026
SetupOn top
modal dialog, no z-index, against a box at z-index: 2147483647the dialog
dialog inside a transformed z-index: 1 parent, dialog at z-index: -5, same neighbourthe dialog
popover="manual" opened with showPopover(), same neighbourthe 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.

Ties and limits — measured by Learn CSS Visually in Chrome 158, December 3, 2026
SetupOn top
2147483648 first, 2147483647 laterthe later one (both read as 2147483647)
99999999999 first, 2147483647 laterthe later one
2147483647 first, 2147483646 laterthe first one
two boxes at z-index: 7the one later in the HTML
z-index: 0 first, auto laterthe later one
z-index: auto first, 0 laterthe 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.

Sticky header against a later card — measured by Learn CSS Visually in Chrome 158, December 3, 2026
HeaderLater cardOn top
position: sticky; top: 0, no z-indextransform: translateZ(0), no z-indexthe card
position: sticky; top: 0; z-index: 1transform: translateZ(0), no z-indexthe header
position: sticky; top: 0; z-index: 1position: relative; z-index: 2the 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.

Negative z-index — measured by Learn CSS Visually in Chrome 158, December 3, 2026
ParentChild at z-index: -1
plain block with a backgroundbehind the parent’s background
the same with isolation: isolateabove the parent’s background
the same with opacity: 0.99 or contain: layoutabove 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-index and 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

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.

How this was written: Written from the CSS2 Recommendation (12 May 1998), the CSS Positioned Layout Module Level 3 (Working Draft, 7 October 2025), CSS Containment Level 2 (Working Draft, 17 September 2022) and Level 3 (Working Draft, 18 August 2022), the CSS 2.2 draft, MDN Web Docs’ stacking context page (last modified 28 September 2026), Can I use, and our own Chrome 158 tests, all read or run on December 3, 2026. Every yes or no in the tables is a measurement we took in Chrome 158 on that date. Code samples are simplified for learning.

Oh, one more thing…

If this helped, you’ll like our free updates: the insights we discover while researching, and the moments worth sharing with other people learning CSS — no spam, no promotions.

One short email when there’s something worth your time. Unsubscribe any time.