---
title: "Why is z-index not working?"
description: "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."
url: https://www.danny.engineering/answer/css/why-is-z-index-not-working
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["CSS"]
---

# Why is z-index not working?

## 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.


```css
/* Make sure the element can take a z-index */
.dropdown {
  position: relative;
  z-index: 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:


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


### 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:


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


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:


```typescript
import { createPortal } from 'react-dom';

function Modal({ children }: { children: React.ReactNode }) {
  return createPortal(<div className="modal">{children}</div>, document.body);
}
```


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:


```css
.card {
  isolation: isolate;
}
```


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:


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


## 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.

## Related reading

- [MDN: Stacking context](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_positioned_layout/Stacking_context)
- [MDN: isolation](https://developer.mozilla.org/en-US/docs/Web/CSS/isolation)
