日本語

Why CSS z-index Doesn’t Work and How to Use isolation

​​CSS​HTML,JavaScript,Playwright

If an element does not come to the front even with z-index: 9999, there are two things to check first. One is that the z-index has no effect at all, because the element is not positioned and is not a flex item or grid item. The other is that one of its ancestors creates a stacking context, so the z-index is only compared inside that context. isolation: isolate uses the second behavior on purpose, to keep overlaps inside a component from leaking out.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. To judge which element appears on top, I used document.elementFromPoint() at overlapping positions. I have not tested Firefox, Safari, or real smartphones.

Elements that z-index applies to and elements it does not

The MDN page for z-index says that z-index controls stacking order for positioned elements (where position is not static), flex items, and grid items. The example on the MDN page on using z-index also states that z-index has no effect on elements that are not positioned.

In the test environment, I overlapped an element with z-index: 10 and an element placed after it with position: absolute and no z-index.

html
<div id="a" style="z-index: 10">a</div>
<div id="b" style="position: absolute; top: 50px; left: 50px">b</div>

The element on top was b, which had no z-index. When I added position: relative to a, a came on top.

Flex items and grid items respond to z-index without any position. The CSS Flexbox specification and the CSS Grid specification say that when z-index is not auto, the item behaves as if it were position: relative, even if position is static. In the test environment, of two flex items overlapped with a negative margin, adding only z-index: 1 to the earlier one put it on top. Two grid items placed in the same cell behaved the same way.

When an ancestor creates a stacking context, its children cannot be compared with outside elements

If an element is positioned but still does not respond, suspect an ancestor. According to the MDN page on stacking contexts, elements inside a stacking context are stacked separately from elements outside it. Once the stacking inside is done, the whole context is treated as a single unit within its parent context. No matter how large a z-index a child has, it can only be compared with elements in the same context.

In the test environment, I overlapped a parent containing a child with z-index: 9999 and an element outside the parent with z-index: 1.

html
<div id="parent" style="position: relative">
  <div id="child" style="position: absolute; z-index: 9999">child</div>
</div>
<div id="other" style="position: absolute; top: 50px; left: 50px; z-index: 1">other</div>

When the parent had only position: relative, the child (9999) was on top. When I added any one of the following to the parent, the element on top changed to the outside element (1).

Declaration added to the parent Element on top
None (only position: relative) Child (z-index: 9999)
z-index: 0 or z-index: 1 Outside element (z-index: 1)
opacity: 0.99 Outside element
transform: translateZ(0) Outside element
filter: blur(0) Outside element
isolation: isolate Outside element
will-change: transform Outside element
contain: paint Outside element

Declarations that barely change the appearance, such as opacity: 0.99 and filter: blur(0), gave the same result. The MDN stacking context page lists other conditions that create a stacking context, including position: fixed and position: sticky, mix-blend-mode, clip-path, backdrop-filter, and container-type for container queries.

Note that z-index: 0 also changes the result. As described on the MDN page for z-index, for a positioned element an integer z-index creates a new stacking context even when the value is 0. With auto, it does not. The CSS Positioned Layout specification also says that a relative or absolute element with z-index: auto does not create a stacking context.

Finding the ancestor that causes it

When there are many ancestors, it is hard to find by eye which one creates the stacking context. I wrote a function that checks the computed styles of the ancestors in order with getComputedStyle(). Load it into the page, then call it with the element you want to check.

js
// Check the ancestors of an element for common conditions that may create a stacking context
function findStackingAncestors(el) {
  const found = [];
  for (let e = el.parentElement; e && e !== document.documentElement; e = e.parentElement) {
    const s = getComputedStyle(e);
    const parent = e.parentElement ? getComputedStyle(e.parentElement).display : "";
    const reasons = [];
    if (s.zIndex !== "auto" && (s.position !== "static" || /flex|grid/.test(parent)))
      reasons.push(`z-index: ${s.zIndex}`);
    if (s.position === "fixed" || s.position === "sticky") reasons.push(`position: ${s.position}`);
    if (Number(s.opacity) < 1) reasons.push(`opacity: ${s.opacity}`);
    for (const p of ["transform", "scale", "rotate", "translate", "filter", "backdrop-filter", "perspective", "clip-path", "mask-image"])
      if (s[p] && s[p] !== "none") reasons.push(`${p}: ${s[p]}`);
    if (s.mixBlendMode !== "normal") reasons.push(`mix-blend-mode: ${s.mixBlendMode}`);
    if (s.isolation === "isolate") reasons.push("isolation: isolate");
    if (/transform|opacity|filter|z-index/.test(s.willChange)) reasons.push(`will-change: ${s.willChange}`);
    if (/layout|paint|strict|content/.test(s.contain)) reasons.push(`contain: ${s.contain}`);
    if (/size/.test(s.containerType)) reasons.push(`container-type: ${s.containerType}`);
    if (reasons.length) found.push({ element: e, reasons });
  }
  return found;
}

In the test environment, I loaded this function into the page as a script and called it with #target from the following page.

html
<main style="opacity: 0.99">
  <div class="layout" style="display: flex">
    <section class="panel" style="z-index: 2; transform: translateZ(0)">
      <div class="wrap" style="position: relative">
        <div id="target" style="position: absolute; z-index: 9999">menu</div>
      </div>
    </section>
  </div>
</main>

Starting from the nearest ancestor, these two were found. The div.wrap, which has only position: relative, does not create a stacking context, so it does not appear in the list.

text
section.panel: z-index: 2, transform: matrix(1, 0, 0, 1, 0, 0)
main: opacity: 0.99

This function checks only the common conditions from the MDN list. It does not check values in the middle of an animation or elements placed in the top layer. If it finds nothing, compare against the full MDN list.

isolation: isolate only creates a stacking context

According to the MDN page for isolation, isolation: isolate always creates a new stacking context on the element. The initial value auto creates one only when another property requires it.

transform also creates a stacking context, but it has another effect: it changes the box (the containing block) that an absolutely positioned child uses as its reference for position. In the test environment, I placed a child with position: absolute; top: 0; left: 0 inside a parent with margin: 100px and checked. With nothing on the parent and with isolation: isolate, the child was placed at the top-left of the page (0, 0). When I added transform: translateZ(0) to the parent, the child moved to the top-left of the parent (100, 100). Adding isolation: isolate did not change the reference for the absolute child's position.

Keeping a z-index: -1 decoration above the element's background

One place where isolation: isolate helps is when you use z-index: -1 to send a decoration behind text. If you make a card position: relative and place a position: absolute; z-index: -1 decoration inside it, the decoration goes below the card's white background and is hidden. The CSS Positioned Layout specification says that a relative element with z-index: auto does not create a stacking context, and that its positioned children join the outer context. So the -1 on the decoration is compared in the page-wide context.

html
<div class="card">
  <div class="deco"></div>
  <p>カードの文字</p>
</div>
css
.card {
  position: relative;
  background: #fff;
  isolation: isolate; /* Without this, the decoration is hidden below the card's background */
}

.deco {
  position: absolute;
  z-index: -1;
  top: -14px;
  left: -14px;
  width: 260px;
  height: 110px;
  background: #d85878;
}
Two screenshots stacked vertically. The top one, "card without isolation", shows the pink decoration only where it sticks out beyond the card's border; inside the card it is hidden behind the white background. The bottom one, "card with isolation: isolate", shows the pink decoration above the card's white background, and the "Text in the card" label (Japanese in the image) overlaps it on top
Card with a z-index: -1 decoration. Without isolation (top) and with isolation: isolate (bottom)

When isolation: isolate was added to the card, the decoration was drawn above the card's background and below the text. When I checked the overlapping positions in the test environment, without isolation the card was the topmost element at the decoration's position. With it, the decoration was topmost there. At the text's position, the text stayed on top. Adding z-index: 0 to the card gave the same result.

When an isolated area cannot let a menu out, and how to fix it

A z-index inside an isolated area cannot reach beyond that area. This is a direct problem when a dropdown menu sits inside a card.

Two screenshots stacked vertically. The top one, "card 1 with isolation: isolate only", shows a menu with z-index: 100 hanging down from card 1, hidden under card 2. The bottom one, "open card 1 also with z-index: 1", shows the menu overlapping card 2 with items A and B visible
A menu extending from a card with isolation: isolate. Card 1 with nothing added (top) and with z-index: 1 added (bottom)

Card 1 and card 2 were both position: relative, and only card 1 had isolation: isolate. The menu inside card 1 had z-index: 100, but as the top screenshot shows, it was hidden under card 2, which comes after it.

In the test environment, I checked several combinations. The results were as follows.

Card 1 Card 2 Element on top where the menu overlaps card 2
position: relative only position: relative Menu
isolation: isolate position: relative Card 2
isolation: isolate Nothing (position: static) Menu
isolation: isolate position: relative; z-index: 0 Card 2
isolation: isolate and z-index: 1 position: relative Menu

Card 1 with isolation, and card 2 positioned with z-index: auto or 0, were drawn in the order they appeared in the HTML. So card 2, which came later, was on top. When card 2 was not positioned, the menu was on top.

Raising z-index only on the open component

One fix is to give the card that holds the open menu a z-index that is compared in the outer context. Adding z-index: 1 to card 1 put it above card 2, and the menu became visible. If the open menu gets an open class, you can apply the z-index to the card with :has(). In the test environment, card 1's z-index was auto while the menu was closed. Adding the open class changed it to 1, and the menu appeared above card 2.

css
.card {
  position: relative;
  isolation: isolate;
}

.card:has(.menu.open) {
  z-index: 1;
}

While the menu is open, z-index: 1 alone is enough to keep the inner overlaps contained, but when it closes the z-index returns to auto. To keep the area contained while closed too, keep isolation: isolate.

Using popover to move the menu to the top layer

Another fix is to make the menu an element with the popover attribute, which places it in the top layer. According to the MDN explanation of the top layer, the top layer is drawn above all other layers on the page.

html
<div class="card">
  <div id="menu" popover="manual">…</div>
</div>
js
document.getElementById("menu").showPopover();

In the test environment, even with card 1 still isolation: isolate and card 2 set to position: relative; z-index: 9999, the menu shown with showPopover() appeared above card 2.

However, a popover element gets position: fixed from the browser's default styles, so its position is set relative to the screen. When I showed a menu with no position specified on an 800×600px screen, it appeared near the middle of the screen (top-left at (318, 233)), not just below the card. If you want it right under the card, you need to specify its position yourself.

Browser support

According to the compatibility data (@mdn/browser-compat-data 8.1.8), isolation is supported in Chrome 41, Edge 79, Firefox 36, Safari 8, and Safari on iOS 8.

​Posted by 86