Stacking and z-index
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:
- the root background,
- normal-flow blocks, in source order,
- floats,
- inline content (text, images),
- 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.
.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.
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: absoluteorrelativewith az-indexother thanauto,position: fixedorsticky(always),- a flex or grid item with a
z-indexother thanauto, opacityless than 1,transform,filter,backdrop-filter,perspective,clip-path,mask,mix-blend-modeother thannormal,isolation: isolate,will-changenaming one of the properties above,contain: paintorcontain: 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:
- 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.
- Remove an unnecessary context — often a
z-indexset “just in case” on a parent, or atransformthat’s no longer needed. - 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:
.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:
: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.
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.
<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><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 had position: relative + z-index: 1, which creates a stacking
context. Everything inside it — including the menu — is painted as part
of .content, at level 1, below the header's level 2. The menu's 9999 only
competes with siblings inside .content.
Fix: don't give .content a z-index, so it doesn't create a context.
*/
.content { position: relative; margin-top: -2rem; background: #f7f7f5; }
.menu { position: absolute; top: 0; left: 1rem; z-index: 9999; background: #eaf0fc; padding: 3rem 1rem 1rem; }
/*
Now that .content isn't a stacking context, the card's z-index: -1
background would drop behind .content's own background and vanish.
isolation: isolate keeps it inside the card.
*/
.card { position: relative; margin-top: 10rem; isolation: isolate; }
.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-indexworks 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: isolateall create stacking contexts. - Use a small, documented z-index scale and
isolation: isolateon components instead of escalating numbers.