WebCraft

Note #05 ยท

CSS isolation: isolate, and a z-index that stays inside

CSS isolation: isolate forces an element to become a stacking context, so every z-index inside it is ranked only against the other contents of that box. A badge with z-index: 9999 in an isolated card can end up underneath a neighbouring element with z-index: 1 , because the finished card is handed to the parent stacking order as one unit.

isolation: auto z-index: 9999
isolation: isolate z-index: 9999

outside sibling, z-index: 1

Left: the badge paints over the dark bar. Right: the identical badge stays under it.

CSS isolation: isolate forces an element to become a stacking context, so every z-index inside it is ranked only against the other contents of that box. A badge with z-index: 9999 in an isolated card can end up underneath a neighbouring element with z-index: 1, because the finished card is handed to the parent stacking order as one unit.

What the declaration changes

The property takes two keywords. With auto, an element gets a stacking context only when some other property on it requires one. With isolate, the context is mandatory. It applies to all elements (in SVG, to container elements, graphics elements and graphics-referencing elements), does not inherit, cannot be animated, and starts at auto. It has been available across browsers since January 2020, and it is defined in the Compositing and Blending module.

isolation: auto;    /* context only if another
  property forces one */
isolation: isolate; /* context guaranteed */

You do not have to position the element or give it an index, and the box keeps the size it already had. That makes it a convenient declaration to hang on a component root such as a card, a menu wrapper or a chart container. It is intended for use next to mix-blend-mode and z-index: an isolated group gives a blended child something self-contained to blend within. Our mix-blend-mode: difference example shows the blending behaviour on its own.

A two-card demo where 9999 loses to 1

This editorial example puts two identical cards in one row. Each holds a badge at z-index: 9999 that hangs below the card border, and a dark bar at z-index: 1 crosses the bottom of the row. One declaration separates the two cards.

<div class="wc-isolation-row">
  <div class="wc-isolation-card">
    <code class="wc-isolation-code">isolation:
  auto</code>
    <span class="wc-isolation-badge">z-index:
  9999</span>
  </div>
  <div class="wc-isolation-card
  wc-isolation-card--sealed">
    <code class="wc-isolation-code">isolation:
  isolate</code>
    <span class="wc-isolation-badge">z-index:
  9999</span>
  </div>
  <p class="wc-isolation-overlay">outside sibling,
  z-index: 1</p>
</div>
.wc-isolation-row          { position: relative; }
  /* z-index auto: no context */
.wc-isolation-card         { position: relative; }
  /* also no context */
.wc-isolation-card--sealed { isolation: isolate; }
  /* context starts here */
.wc-isolation-badge        { position: absolute;
  bottom: -1.4rem; z-index: 9999; }
.wc-isolation-overlay      { position: absolute;
  bottom: 0; height: 2.5rem; z-index: 1; }

The row and the left card are positioned but keep z-index: auto, so neither creates a context. The left badge therefore competes in the same context as the dark bar, where 9999 beats 1. The right card is a context of its own. Its badge is ranked only against the card's other contents, and the finished card enters the outer order as a single unit at the level of a positioned box with an automatic index, which puts the bar at z-index: 1 on top of it. Both badges still paint outside their card borders, so nothing was clipped here; the painting order is what changed.

Other properties that create a stacking context

Stacking bugs usually trace back to a context nobody intended. The index in your stylesheet is real, but the browser compares it inside a smaller group than you assumed. When a component paints in the wrong order, check its ancestors against this list.

On the elementCreates a context when
position: absolute or relativez-index is anything other than auto
position: fixed or stickyalways
flex or grid itemz-index is anything other than auto
opacityvalue below 1
transform, scale, rotate, translate, filter, backdrop-filter, perspective, clip-path, maskvalue other than none
mix-blend-modevalue other than normal
contain, container-typelayout or paint (including strict and content); size or inline-size
will-changenames any property that would create one
isolationvalue isolate

The document root is a context as well. A @keyframes animation of a context-creating property such as opacity, with animation-fill-mode: forwards, leaves the context in place after the animation ends.

Limits: clipping is separate, and the top layer wins

overflow: hidden and overflow: clip cut the pixels that fall outside a box. isolation: isolate decides which elements a z-index gets compared with. The demo separates the two: the sealed card clips nothing, its badge still hangs over the row, and it still loses to the bar. If you want that badge cut off at the card edge too, add an overflow value. Our note on overflow: clip and sticky positioning covers the clipping half.

The top layer is the limit you cannot work around with this property. Fullscreen content and popovers are placed there along with their ::backdrop, above the ordinary document stacking order, and each of them forms a stacking context of its own. Isolation on an ancestor does not pull a top-layer element back into a component, and the indices inside your component are never compared with it. A dialog opened with showModal() or a native popover will cover an isolated card whatever numbers the card uses.

A short routine for keeping component z-index local

The routine below is an editorial suggestion for component authors. The specification does not prescribe any numbering scheme.

  1. Put isolation: isolate on the component root, the element that owns the raised children, and leave its z-index at auto unless the component itself has to beat a sibling.
  2. Inside, a small scale is enough: 1 for a raised badge, 2 for a local overlay. Those numbers mean nothing outside the component, so they never need to grow.
  3. If the whole component has to sit above a neighbour, give the root an explicit index as well. Isolation bounds the children; it does not lift the root.
  4. When something still escapes, walk the ancestors against the trigger table above. If the offender is a dialog, popover or fullscreen element, restructure the markup instead of raising numbers, because that element is in the top layer.

Once each component owns a context, the page only has to order a handful of units. A sticky header at z-index: 10 then stays above every card, and a badge inside a card can keep whatever local number it needs.

Questions and answers

Does isolation: isolate clip children that overflow the box?

No. It changes painting order, not clipping. In the demo both badges still paint outside the card border; the isolated one simply ends up under the sibling bar. Add overflow: hidden or overflow: clip if you also want the pixels cut.

How is it different from position: relative; z-index: 0?

Both create a stacking context. The relative-plus-index route needs positioning (or a flex or grid item) and adds an index that competes with siblings in the parent context. isolation: isolate works on any element and leaves position and z-index alone, so the outer ordering stays as it was.

Why does a modal dialog still cover my isolated component?

Because the dialog is rendered in the top layer, along with its ::backdrop, above ordinary stacking contexts. Isolation on an ancestor cannot contain a top-layer element, and the z-index values inside your component are never compared with it.

Sources

Checked 1 December 2026.