More CSS answers

by Danny
Published 2 min read

A z-index that seems ignored is usually on a non-positioned element or trapped inside a stacking context. Learn what creates stacking contexts and how to fix layering bugs.

The short answer

#

z-index only competes with other elements in the same stacking context, and it only applies to positioned elements or flex and grid items. If a modal with z-index: 9999 still sits under a header, one of its ancestors has created a stacking context that is itself lower than the header.


_10
/* Make sure the element can take a z-index */
_10
.dropdown {
_10
position: relative;
_10
z-index: 10;
_10
}

Why it happens

#

The browser does not compare every z-index on the page against every other. It groups elements into stacking contexts. Inside one context, children are layered by their z-index. The whole context is then layered as a single unit against its siblings, using the parent's own z-index.

That means a child with z-index: 9999 inside a parent with z-index: 1 can never appear above a sibling of that parent with z-index: 2. The 9999 only matters inside its own group.

Many properties create a new stacking context, often without you realising. The common ones are:

  • position: relative or absolute together with a z-index other than auto
  • position: fixed or sticky
  • opacity less than 1
  • transform, filter, backdrop-filter, perspective or clip-path with any value other than none
  • mix-blend-mode other than normal
  • isolation: isolate
  • will-change naming one of the properties above
  • contain: paint or contain: layout
  • a flex or grid item with a z-index other than auto

The other cause is simpler: on an element with position: static (the default) that is not a flex or grid item, z-index has no effect at all.

How to fix it

#

Make the element positioned

If the element is not a flex or grid item, give it a position so that z-index applies:


_10
.badge {
_10
position: relative;
_10
z-index: 1;
_10
}

Find the ancestor that traps it

Walk up from the element in developer tools and look for any of the properties listed above. This console snippet logs likely culprits for the selected element:


_12
let el = $0.parentElement;
_12
while (el) {
_12
const s = getComputedStyle(el);
_12
const creates =
_12
(s.position !== 'static' && s.zIndex !== 'auto') ||
_12
s.position === 'fixed' || s.position === 'sticky' ||
_12
Number(s.opacity) < 1 ||
_12
s.transform !== 'none' || s.filter !== 'none' ||
_12
s.isolation === 'isolate';
_12
if (creates) console.log(el, s.position, s.zIndex, s.opacity, s.transform);
_12
el = el.parentElement;
_12
}

Once you know which ancestor is responsible, either raise that ancestor's z-index, or remove the property that creates the context if it is not needed (a leftover transform: translateZ(0) is a common one).

Move the element out of the trap

Modals, toasts and tooltips should not live deep inside a component that has its own stacking context. Render them near the end of body instead. In React, a portal does this:


_10
import { createPortal } from 'react-dom';
_10
_10
function Modal({ children }: { children: React.ReactNode }) {
_10
return createPortal(<div className="modal">{children}</div>, document.body);
_10
}

Even better, the native dialog element opened with showModal(), and elements using the popover attribute, render in the browser's top layer, which sits above everything else regardless of z-index.

Use isolation to contain a component

If a component's internal z-index values are leaking out and fighting with the rest of the page, give the component root its own context:


_10
.card {
_10
isolation: isolate;
_10
}

Now anything inside .card is layered only against other things inside .card.

Keep a small z-index scale

Instead of escalating to 9999, define a few named layers and stick to them:


_10
:root {
_10
--z-dropdown: 10;
_10
--z-sticky: 20;
_10
--z-overlay: 30;
_10
--z-toast: 40;
_10
}

Common pitfalls

#
  • Raising the number higher and higher. If 10 does not work, 99999 will not either. Look for the stacking context.
  • Animations that add transform or opacity****. The element may layer correctly at rest and jump behind something mid-animation.
  • Negative z-index****. A child with z-index: -1 can disappear behind its parent's background unless the parent creates a stacking context.
  • overflow: hidden clipping. If the element is cut off rather than hidden behind something, that is clipping, not stacking. No z-index will fix it.
#

Was this resource helpful?