Stacking and z-index

Topic 3 of 4 in Positioning7 min readBeginnerFree course

Overview

When boxes overlap — a dropdown over content, a sticky header over scrolling text, a modal over everything — something decides which one is on top. Usually it’s simple: give the element a higher z-index. But sometimes z-index: 9999 still loses, and the reason is the stacking context. Understanding it ends the z-index arms race.

Default painting order

Without any z-index, a browser paints a page in layers, roughly:

  1. the root background,
  2. normal-flow blocks, in source order,
  3. floats,
  4. inline content (text, images),
  5. positioned elements, in source order.

So by default, positioned elements appear above non-positioned ones, and later elements paint over earlier ones. A sticky header near the top of the HTML can therefore be covered by a positioned card that comes later — the classic reason headers need a z-index.

z-index

z-index moves an element up or down the stack. It applies to:

  • positioned elements (relative, absolute, fixed, sticky),
  • flex items and grid items (even without position).

On a plain position: static block, z-index is ignored — DevTools shows it as inactive.

CSS
.header { position: sticky; top: 0; z-index: 10; }
.modal  { position: fixed; inset: 0; z-index: 100; }
.behind { position: relative; z-index: -1; }   /* behind its siblings' normal content */

Higher values paint on top. Negative values go behind normal-flow content.

Stacking contexts

Here’s the twist. Some elements form a stacking context: a self-contained group whose children are layered inside it, and which is then layered as one unit among its siblings.

The header has z-index 2. The main area has z-index 1 and creates a stacking context; inside it, a dropdown has z-index 9999, but that 9999 only competes inside main. The header (2) is painted above everything in main (1), including its z-index 9999 dropdown. Things that create a stacking context include position with z-index, opacity below 1, transform, filter, isolation: isolate, and flex or grid items with z-index.

Think of stacking contexts like folders on a desk: you can rearrange the sheets inside a folder however you like, but the whole folder is either above or below the other folder. A sheet numbered 9999 in a folder at level 1 is still underneath a folder at level 2.

That’s why the dropdown loses: its parent main has z-index: 1, creating a context at level 1. The header is at level 2. Nothing inside main can rise above the header, whatever its z-index.

What creates a stacking context

The most common triggers:

  • the root element (<html>),
  • position: absolute or relative with a z-index other than auto,
  • position: fixed or sticky (always),
  • a flex or grid item with a z-index other than auto,
  • opacity less than 1,
  • transform, filter, backdrop-filter, perspective, clip-path, mask,
  • mix-blend-mode other than normal,
  • isolation: isolate,
  • will-change naming one of the properties above,
  • contain: paint or contain: layout.

Several of those aren’t obviously “layering” properties. Adding opacity: 0.99 or a transform for an animation can suddenly trap a dropdown — a classic mysterious bug.

Fixing z-index problems

When something is behind something else despite a higher z-index:

  1. Find the stacking contexts. Walk up from the element that’s losing; find the first ancestor that creates a context. Its z-index (not the element’s) is what’s compared against the other element’s context.
  2. Remove an unnecessary context — often a z-index set “just in case” on a parent, or a transform that’s no longer needed.
  3. Or move the element. Dropdowns, tooltips and modals often belong outside their component’s context entirely — rendered near the end of <body>, in a <dialog> (which uses the browser’s top layer, above every z-index), or as a popover.

The top layer deserves a mention: elements shown with dialog.showModal() or the Popover API render above everything on the page, regardless of z-index or stacking contexts. For modals and menus, it removes z-index problems entirely.

isolation: isolate

Sometimes you want a stacking context: a component that uses negative z-index internally (say, a decorative background behind its text) should keep that layering inside itself, rather than slipping behind the page’s background:

CSS
.card {
  position: relative;
  isolation: isolate;          /* create a context with no side effects */
}
.card::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;                 /* behind the card's text, but not behind the page */
  background: linear-gradient(…);
}

isolation: isolate creates a stacking context and does nothing else — no offset, no opacity, no transform. It’s the cleanest way to contain a component’s layering.

A z-index scale

Instead of random numbers, define a small scale once and use it everywhere:

CSS
:root {
  --z-dropdown: 10;
  --z-sticky: 20;
  --z-overlay: 30;
  --z-modal: 40;
  --z-toast: 50;
}

.header { z-index: var(--z-sticky); }
.menu   { z-index: var(--z-dropdown); }

With a scale, nobody has to guess whether 9999 is high enough, and the order of layers is documented in one place.

When z-index “doesn’t work”: it’s almost always either (1) the element isn’t positioned or a flex/grid item, or (2) it’s trapped in a stacking context. Check those two before changing any numbers.

Exercise

The dropdown behind the header

A .menu dropdown inside .content should appear above the sticky .header, but it shows underneath even with z-index: 9999. Find out why (look at what .content creates), then fix it without changing the menu’s z-index — and keep the header above normal scrolling content. Your fix will make the card’s decorative background disappear behind .content — use isolation: isolate on the card so its internal z-indexes can’t leak out.

HTML
<header class="header">Header</header>
<main class="content">
  <div class="menu">Dropdown menu</div>
  <div class="card"><span class="card-bg"></span><p>Card text</p></div>
</main>

<style>
  .header { position: sticky; top: 0; z-index: 2; height: 4rem; background: #1b1d21; color: white; }
  .content { position: relative; z-index: 1; margin-top: -2rem; background: #f7f7f5; }
  .menu { position: absolute; top: 0; left: 1rem; z-index: 9999; background: #eaf0fc; padding: 3rem 1rem 1rem; }
  .card { position: relative; margin-top: 10rem; }
  .card-bg { position: absolute; inset: 0; z-index: -1; background: #fff6e5; }
</style>

Summary

  • Elements paint in a defined order: backgrounds, normal-flow blocks, floats, inline content, then positioned elements.
  • z-index works on positioned elements and on flex and grid items; higher values paint on top.
  • A stacking context is a group that’s layered as one unit; z-index values only compete inside the same context.
  • Positioned elements with a z-index, opacity below 1, transforms, filters and isolation: isolate all create stacking contexts.
  • Use a small, documented z-index scale and isolation: isolate on components instead of escalating numbers.