CSS z-index Not Working? How Stacking Context Works

Short answer: z-index controls which element sits on top when elements overlap. A higher number wins, but only on positioned elements (any position except static) and on flex or grid items. If it’s set and still “doesn’t work”, a parent has created a stacking context, and the element can never rise above its parent’s level, no matter how big the number is.

Below: the two reasons z-index fails, a live demo of the stacking context trap, every common property that creates one, and a small z-index scale that keeps things predictable.

How z-index works

Overlapping elements are painted in order. By default, later elements in the HTML are painted on top of earlier ones. z-index changes that order: a positioned element with z-index: 2 is painted above one with z-index: 1.

  • The default value is auto.
  • Only whole numbers work. z-index: 1.5 is invalid, and the browser ignores the declaration.
  • Negative values are allowed and put the element behind its siblings.
  • It isn’t inherited. A child of a z-index: 5 element has z-index: auto.

The catch: numbers are only compared between elements in the same stacking context. That’s where most bugs come from.

Reason 1: the element isn't positioned

On a regular block with position: static (the default), z-index does nothing. Give the element position: relative; without offsets like top or left, it stays exactly where it was. If you’re unsure which position value to pick, see position absolute vs relative.

Flex and grid items are the exception: their z-index works without any position.

.badge {
  z-index: 10; /* ignored: the element is static */
}

.badge {
  position: relative;
  z-index: 10; /* works */
}

/* Flex and grid items don't need position */
.toolbar {
  display: flex;
}
.toolbar > .active {
  z-index: 1; /* works */
}

Reason 2: a parent creates a stacking context

A stacking context is a group of elements that’s painted as one layer. Inside it, children are sorted by their own z-index. Outside it, the whole group is compared to other elements by the parent’s level only.

So a menu with z-index: 999 inside a card that forms a stacking context is stuck at the card’s level. If the next card has z-index: 1 and the first card has no z-index of its own, the next card wins and covers the menu.

In the demo, the left card has transform: translateY(0), which is invisible but creates a stacking context. On the right, the same card also gets z-index: 2, so the whole card, menu included, moves above the next card.

Demo:

<div class="zstack-demo">
  <div class="zstack-demo__col">
    <p class="zstack-demo__label">Trapped</p>
    <div class="zstack-demo__card zstack-demo__card--lifted">
      Card with <code>transform</code>
      <div class="zstack-demo__menu">Menu, z-index: 999</div>
    </div>
    <div class="zstack-demo__card zstack-demo__card--next">Next card, z-index: 1</div>
  </div>
  <div class="zstack-demo__col">
    <p class="zstack-demo__label">Fixed</p>
    <div class="zstack-demo__card zstack-demo__card--lifted zstack-demo__card--raised">
      Same card + <code>z-index: 2</code>
      <div class="zstack-demo__menu">Menu, z-index: 999</div>
    </div>
    <div class="zstack-demo__card zstack-demo__card--next">Next card, z-index: 1</div>
  </div>
</div>
.zstack-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 24px;
  padding: 24px 16px;
  border-radius: 16px;
  background: #0f172a;
  color: #e2e8f0;
  font: 14px/1.4 system-ui, sans-serif;
}
.zstack-demo__label {
  margin: 0 0 8px;
  font-weight: 700;
  color: #94a3b8;
}
.zstack-demo code {
  font-family: ui-monospace, monospace;
  color: #93c5fd;
}
.zstack-demo__card {
  padding: 12px;
  border-radius: 10px;
  min-height: 72px;
}
.zstack-demo__card--lifted {
  position: relative;
  background: #1e293b;
  border: 1px solid #334155;
  transform: translateY(0);
}
.zstack-demo__card--raised {
  z-index: 2;
}
.zstack-demo__menu {
  position: absolute;
  z-index: 999;
  left: 12px;
  right: 12px;
  top: 52px;
  padding: 10px;
  border-radius: 8px;
  background: #facc15;
  color: #0f172a;
  font-weight: 700;
  min-height: 56px;
}
.zstack-demo__card--next {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: flex-end;
  min-height: 88px;
  margin-top: 12px;
  background: #2563eb;
  color: #fff;
  font-weight: 600;
}

See the live demo

What creates a stacking context

The root <html> element is one. These are the triggers you’ll run into most often:

  • position: relative or absolute with a z-index other than auto (without a z-index, they don’t)
  • position: fixed or sticky, always
  • a flex or grid item with a z-index other than auto
  • opacity below 1, even 0.99
  • transform, filter, backdrop-filter or clip-path with any value other than none
  • mix-blend-mode other than normal
  • will-change naming one of those properties, like will-change: transform
  • isolation: isolate

That’s why z-index bugs often show up after an unrelated change: a hover effect with CSS transform, a fade-in animation that leaves opacity below 1, or a frosted-glass header with backdrop-filter. MDN keeps the full list of stacking context triggers.

How to fix a trapped element

You have three options, from simplest to most robust:

  1. Raise the parent, not the child. Find the ancestor that creates the stacking context and give it a z-index higher than the element covering it. That’s the fix in the demo.
  2. Remove the trigger. If the transform or opacity on the parent isn’t needed when the menu is open, drop it in that state.
  3. Move the element out. Render the dropdown or modal near the end of <body>, outside the clipped or trapped parent. A <dialog> opened with showModal() goes further: it’s rendered in the top layer, above every z-index on the page.
/* The card creates a stacking context (transform),
   so raise the card itself */
.card {
  position: relative;
  transform: translateY(0);
  z-index: 2;
}

.card .menu {
  position: absolute;
  z-index: 1; /* only needs to beat its siblings inside the card */
}

Find the parent that traps your element

Paste this function into the browser console, then call it with the element that won’t come to the front. It walks up the ancestors and logs each one that creates a stacking context, and why. It covers the common triggers from the list above, not every one.

function findStackingContexts(el) {
  for (let node = el.parentElement; node; node = node.parentElement) {
    const s = getComputedStyle(node);
    const parent = node.parentElement && getComputedStyle(node.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 (s.opacity !== '1') reasons.push(`opacity: ${s.opacity}`);
    if (s.transform !== 'none') reasons.push('transform');
    if (s.filter !== 'none') reasons.push('filter');
    if (s.isolation === 'isolate') reasons.push('isolation: isolate');
    if (s.mixBlendMode !== 'normal') reasons.push('mix-blend-mode');
    if (reasons.length) console.log(node, reasons.join(', '));
  }
}

findStackingContexts(document.querySelector('.menu'));

Negative z-index and isolation: isolate

A child with z-index: -1 is meant to sit behind its parent’s content, for example a decorative background shape. Without a stacking context on the parent, it falls further back, behind the parent’s own background, and seems to vanish.

isolation: isolate creates a stacking context and does nothing else: no position, no z-index, no visual change. The negative child then stays inside the parent. position: relative; z-index: 0 does the same, but isolation says what you mean.

.hero {
  position: relative;
  isolation: isolate; /* keeps the shape inside .hero */
  background: #0f172a;
}

.hero::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1; /* behind the text, above the background */
  background: radial-gradient(circle at top, #2563eb, transparent 60%);
}

Use a small z-index scale

Random values like 999, 9999 and 99999 lead to an arms race. Define a few named layers once and use only those. Put isolation: isolate on self-contained components, so their internal values (1, 2, 3) stay local and never compete with the page-level layers.

:root {
  --z-dropdown: 100;
  --z-sticky: 200;
  --z-overlay: 300;
  --z-modal: 400;
  --z-toast: 500;
}

.site-header {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
}

.card {
  isolation: isolate; /* inner z-index values stay inside */
}

FAQ

Why is my z-index not working?

Either the element is position: static and not a flex or grid item, or an ancestor creates a stacking context with a lower level than the element covering it. Check the element first, then each parent for transform, opacity, filter or a z-index.

Does z-index work without position?

Only on flex and grid items. On normal block and inline elements, you need a position other than static.

What is the highest z-index value?

z-index takes integers. Chrome clamps larger values to 2147483647, the largest 32-bit signed integer. If you need that, a stacking context is almost certainly the real problem.

What does isolation: isolate do?

It creates a new stacking context without changing anything else. Use it to keep a component’s z-index values, including negative ones, contained inside the component.

Does opacity affect z-index?

Yes. Any opacity below 1 creates a stacking context, so children of a faded element can’t rise above elements outside it.