CSS z-index & Stacking Context
When elements overlap on a page, CSS must decide which one appears in front. z-index is the property that controls this — but it only works within
stacking contexts. Understanding why z-index: 9999
sometimes doesn't work is one of the most valuable debugging skills in CSS.
z-index does and when it
worksisolation: isolate — scoping
stacking contextsStacking Order Without z-index
Even without any z-index set, the browser has a
precise paint order. Elements are rendered from back to front in this order:
<html> element's background and
border paint first — behind everything.div, p, section etc. with position: static paint in document order.position: relative/absolute/fixed/sticky and no z-index paint above
everything — in document order among themselves.(no z-index)
↑ Later positioned elements in the DOM paint in front of earlier ones (all other things equal)
/* Paint order back → front (without z-index):
1. Root background
2. Non-positioned block elements (DOM order)
3. Float elements
4. Inline elements / text
5. Positioned elements (DOM order among themselves)
*/
/* Without z-index: later in DOM = in front */
<div class="box a">...</div> /* paints first (behind) */
<div class="box b">...</div> /* paints on top (both positioned) */
.box { position: absolute; }
.a { top: 10px; left: 10px; }
.b { top: 30px; left: 30px; }
/* .b is on top because it comes after .a in the HTML */
z-index — Controlling the Stack
z-index sets an explicit stacking level for a
positioned element. Higher = in front, lower = behind. Negative values push elements behind
their parent. It only works on positioned elements (not position: static).
/* z-index requires position: relative/absolute/fixed/sticky */
.behind { position: relative; z-index: 1; }
.middle { position: relative; z-index: 2; }
.infront { position: relative; z-index: 3; }
/* Negative z-index — behind parent background */
.deco {
position: absolute;
z-index: -1;
}
/* z-index on static element — IGNORED */
.static {
position: static; /* default */
z-index: 9999; /* ← has NO effect */
}
/* Values can be any integer, including negative */
.way-behind { z-index: -100; }
.normal { z-index: 0; }
.modal { z-index: 500; }
z-index on an element with position: static (the default) has no effect. You must also
set position to relative, absolute, fixed, or sticky. This is the #1 reason beginners think z-index is broken.
Stacking Context — The Key Concept
A stacking context is an independent layer — like a transparency sheet placed on a pile. Elements inside a stacking context are painted together as a unit. Their z-index values only compete within that context, not globally across the entire page.
Think of it like this: if Card A creates a stacking context
with z-index: 1 and Card B creates a stacking context with z-index: 2, then everything inside Card A is behind everything
inside Card B — even if a child inside Card A has z-index: 9999.
Children cannot escape their parent stacking context — Child A2 z:9999 is still below all of Context B because Context A (z:1) is below Context B (z:2)
/* ── The "z-index: 9999 doesn't work" problem ── */
.card-a {
position: relative;
z-index: 1; /* creates stacking context */
}
.card-a .tooltip {
position: absolute;
z-index: 9999; /* 9999 within context of z:1 */
}
.card-b {
position: relative;
z-index: 2; /* context z:2 is above z:1 */
}
/* .card-b covers .card-a.tooltip even though
tooltip has z-index: 9999. The tooltip is trapped
inside its context (z:1), which is below card-b (z:2) */
/* Fix: raise the parent context */
.card-a { z-index: 3; } /* now card-a context beats card-b */
What Creates a Stacking Context?
Many CSS properties unexpectedly create a stacking context. This is why your z-index may not behave as expected — something in the ancestor chain created a context you didn't intend:
| Property / Value | Creates context when | Common gotcha? |
|---|---|---|
| position + z-index | position ≠ static AND z-index ≠ auto |
✓ Very common |
| opacity | opacity < 1 (e.g. 0.99) |
✓ Sneaky — even 0.99 creates a context |
| transform | Any value other than none |
✓ Very common — animations use transform |
| filter | Any value other than none |
✓ Breaks position: fixed inside |
| isolation | isolation: isolate |
— This is intentional (see next section) |
| will-change | When value is a compositor property | ✓ Common in animation performance |
| clip-path, mask | Any value other than none | ✓ Often overlooked |
| perspective | Any value other than none | — 3D scene parents |
| mix-blend-mode | Any value other than normal | — Design effects |
| contain | layout or paint value |
— Performance optimization |
transform: translate(0, 0) creates a stacking context even when the
element doesn't visually move. This is why a parent element with a CSS animation (which uses transforms) can
trap child z-index values — and also why position: fixed elements stop
being fixed when inside a transformed ancestor.
Recommended z-index Scale
Using random z-index values like 9999 or 99999 is an anti-pattern that leads to z-index wars. A systematic scale with
CSS custom properties keeps layers predictable and maintainable:
/* ── Define scale in :root ── */
:root {
--z-below: -1; /* behind parent background */
--z-base: 0; /* default */
--z-raised: 1; /* slightly above sibling */
--z-dropdown: 100; /* dropdown menus */
--z-sticky: 200; /* sticky headers/sidebars */
--z-navbar: 300; /* fixed navigation bar */
--z-overlay: 400; /* modal backdrop/overlay */
--z-modal: 500; /* modal dialog */
--z-toast: 600; /* notification toasts */
--z-tooltip: 700; /* always-on-top tooltips */
}
/* ── Usage ── */
.navbar { position: fixed; z-index: var(--z-navbar); }
.backdrop { position: fixed; z-index: var(--z-overlay); }
.modal { position: fixed; z-index: var(--z-modal); }
.toast { position: fixed; z-index: var(--z-toast); }
.tooltip { position: absolute; z-index: var(--z-tooltip); }
.dropdown { position: absolute; z-index: var(--z-dropdown); }
Common z-index Bugs & Fixes
These are the bugs developers encounter most frequently. Understanding the root cause makes them easy to fix:
position:static
position:relative
position:static.position:relative ✓
position:relative
position: relative to the element with
z-index:999. Now it correctly appears on top.z-index: 1
z-index:9999
❌ trapped
z-index: 2
on top of all A
z-index: 3 ✓
z-index:9999
✓ visible
z-index: 2
now behind A
/* ── Bug 1: z-index not working ── */
/* Problem */
.element { z-index: 999; } /* position: static — z-index ignored */
/* Fix */
.element { position: relative; z-index: 999; }
/* ── Bug 2: z-index inside stacking context ── */
/* Problem */
.card-a { position: relative; z-index: 1; }
.card-a .tooltip { position: absolute; z-index: 9999; } /* still under .card-b */
.card-b { position: relative; z-index: 2; }
/* Fix: raise the parent stacking context */
.card-a { z-index: 3; } /* or remove z-index from card-a */
/* ── Bug 3: transform creates unexpected context ── */
/* Problem: modal behind transformed parent */
.animated-section { transform: translateY(0); }
/* Even translateY(0) creates a stacking context!
A fixed/absolute modal inside this is trapped. */
/* Fix: move the modal outside the transformed element */
isolation: isolate — Intentional Contexts
isolation: isolate creates a new stacking context
on purpose without needing position or z-index. It is the clean way to say "the z-index values inside this
component should not interact with the rest of the page."
competes with everything on page
scoped — only within this card
/* isolation: isolate — creates a stacking context
without needing position or z-index */
.card {
isolation: isolate;
/* All z-index values inside .card are now scoped
to .card's stacking context. They will NEVER
bleed out and compete with external elements. */
}
/* Perfect for component-based architecture */
.modal-component { isolation: isolate; }
.dropdown-component{ isolation: isolate; }
.card-component { isolation: isolate; }
/* Mix blend mode requires isolation to prevent
blending with elements outside the component */
.blend-wrap {
isolation: isolate;
}
.blend-wrap img {
mix-blend-mode: multiply;
/* blends with siblings inside .blend-wrap only */
}
Try It Yourself
Experiment with stacking order, the stacking context trap, and the isolation fix. Try
changing z-index values and see what happens when you add or remove transform:
Quick Check — Chapter 24
1. The element has
position: static — z-index only
works on positioned elements (relative, absolute, fixed, sticky). Setting z-index on a static element
has zero effect. The fix is to add position: relative (with no
offsets if you don't need to move it).2. The element is trapped inside a stacking context with a lower z-index — even if a child has z-index: 9999, it cannot escape its parent stacking context. If the parent context has z-index: 1 and another element has z-index: 2, the parent's entire subtree is behind that element. The fix is to raise the parent stacking context's z-index, or remove the unintended stacking context on the parent.
Properties that create a stacking context:
—
position ≠ static AND z-index
≠ auto (most common)—
opacity < 1 — even 0.99 creates one—
transform with any value other than none — includes animated
transforms—
filter with any value other than none—
isolation: isolate — intentional—
will-change with a compositor property—
clip-path, mask, mix-blend-mode, perspectiveThe most surprising ones are
opacity and transform because they are commonly used for animations and visual
effects without developers realising they create stacking contexts.
1. Root element background (html background)
2. Non-positioned block elements (divs, paragraphs with position:static) — in document order
3. Float elements — above non-positioned blocks
4. Inline elements — text and inline boxes, above floats
5. Positioned elements (with any non-static position) — above everything, in document order
Among positioned elements with no z-index (or z-index: auto): elements that appear later in the HTML document are rendered on top of elements that appear earlier. This is why the "last in DOM = on top" rule applies when no z-index is set.
isolation: isolate creates a new stacking context
intentionally, without needing position or z-index. All z-index values inside the isolated element are scoped —
they compete with each other but cannot interact with elements outside.When to use it:
— Component architecture: A card component's internal z-index values (dropdown menus, badges, tooltips) should not bleed out and accidentally appear above a global modal or navbar. Adding
isolation: isolate to the component root prevents this.—
mix-blend-mode: When you want blend modes to
apply only within a component and not blend with the page background behind it.— React/Vue components: Self-contained components that manage their own internal layering without global z-index conflicts.
position: fixed is supposed to position an element relative to the
viewport. However, when an ancestor has a transform (or filter, perspective) applied, that
ancestor creates a new stacking context and also a new "containing block" for fixed positioning.The result: the fixed element is no longer positioned relative to the viewport — it's positioned relative to the transformed ancestor. It scrolls with the page and behaves like absolute positioning.
This is a spec-defined behaviour, not a browser bug.
The fix: move the fixed element out of the transformed ancestor in the DOM — make it a direct child of
<body> or at least outside any transformed
containers. In React, using a portal (ReactDOM.createPortal) renders
the modal at the document root level, avoiding this issue entirely.
z-index: 9999; position: static or Element B with z-index: 1; position: relative?z-index: 1. A child inside A has
z-index: 9999. Element B (sibling of A) has z-index: 2. What will appear on top?
position or z-index?transform to a parent
element break position: fixed on its children?- Natural stacking order (back to front, no z-index): root background → non-positioned blocks → floats → inline elements → positioned elements (DOM order).
z-indexsets explicit stacking order. Higher = in front. Negative = behind parent. Only works on positioned elements (not static).- Stacking context — an independent layer where z-index values compete only within that context. Children cannot escape their parent's context regardless of their z-index value.
- What creates a context:
position + z-index ≠ auto,opacity < 1,transform ≠ none,filter ≠ none,isolation: isolate,will-change,clip-path, and more. - Use a z-index scale with CSS custom properties:
--z-dropdown: 100; --z-navbar: 300; --z-modal: 500etc. Prevents z-index wars. - Bug #1: z-index ignored → add
position: relativeto the element. - Bug #2: z-index trapped → raise or remove the parent stacking context.
- Bug #3:
position: fixedbroken →transformon ancestor creates a new containing block. Move fixed element outside transformed parents. isolation: isolate— intentionally creates a context for component scoping, without position or z-index. Prevents internal z-index values from bleeding to the global scope.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.