Tutorials CSS z-index & Stacking
Ch 24 / 60
Chapter 24 Intermediate 7 min read CSS

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.

What you'll learn
How stacking order works without z-index
What z-index does and when it works
What creates a stacking context
The recommended z-index scale for production apps
Common z-index bugs and how to fix them
isolation: isolate — scoping stacking contexts

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

1. Root element background
The <html> element's background and border paint first — behind everything.
2. Block elements (non-positioned)
div, p, section etc. with position: static paint in document order.
3. Float elements
Floated elements paint above non-positioned block elements.
4. Inline elements
Inline content (text, inline boxes) paints above floats.
5. Positioned elements (z-index: auto / 0)
Elements with position: relative/absolute/fixed/sticky and no z-index paint above everything — in document order among themselves.
1. Non-positioned block (static)
2. Float element
3. Inline text
4. Positioned element
(no z-index)
5. Positioned, later DOM ↑

↑ Later positioned elements in the DOM paint in front of earlier ones (all other things equal)

CSS — Stacking order without z-index
/* 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: 1
z-index: 3 ← front
z-index: 2
CSS — z-index basics
/* 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 only works on positioned elements
Setting 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.

🌐 Root stacking context (html) — z-index compares here at the top level
Context A — z-index: 1
Child A1 — z-index: 100
Child A2 — z-index: 9999 ← still behind Context B!
z-index only compares within A
Context B — z-index: 2
Child B1 — z-index: 1
Child B2 — z-index: 2
Context B (z:2) is above Context A (z:1)

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)

CSS — Stacking context example
/* ── 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
The most surprising one
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:

--z-below
-1
-1
--z-base
0
0
--z-raised
1
1
--z-dropdown
100
100
--z-sticky
200
200
--z-navbar
300
300
--z-overlay
400
400
--z-modal
500
500
--z-toast
600
600
--z-tooltip
700
700
CSS — z-index scale with custom properties
/* ── 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:

❌ Bug: z-index ignored
z-index:999
position:static
z-index:1
position:relative
Red box (z:1) is on top despite the blue box having z:999, because blue has position:static.
✓ Fix: add position
z-index:999
position:relative ✓
z-index:1
position:relative
Add position: relative to the element with z-index:999. Now it correctly appears on top.
❌ Bug: trapped in stacking context
Context A
z-index: 1
child
z-index:9999
❌ trapped
Context B
z-index: 2
on top of all A
Child in Context A has z:9999 but Context A has z:1. Context B (z:2) covers everything in Context A.
✓ Fix: raise the parent context
Context A
z-index: 3 ✓
child
z-index:9999
✓ visible
Context B
z-index: 2
now behind A
Raise Context A's z-index to 3 (above B's 2). Now the child is visible because A's context is above B.
CSS — Common z-index bug fixes
/* ── 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."

❌ Without isolation — z-index bleeds
Card component
inner z-index:50
competes with everything on page
badge z:999
✓ With isolation:isolate — scoped
Card — isolation: isolate
inner z-index:50
scoped — only within this card
badge z:999
CSS — isolation: isolate
/* 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 */
}
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering z-index, stacking context, position, isolation and all CSS layout properties.
Download PDF

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:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 24

Questions & Answers
There are two main reasons:

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.
A stacking context is an independent rendering layer. Elements inside a stacking context are painted as a unit, and their z-index values only compete within that context — not globally.

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, perspective

The most surprising ones are opacity and transform because they are commonly used for animations and visual effects without developers realising they create stacking contexts.
Without any z-index, the browser paints elements back to front in this order:

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.
Multiple Choice Questions
Question 1 of 5
Which element will appear on top: Element A with z-index: 9999; position: static or Element B with z-index: 1; position: relative?
Question 2 of 5
Element A has 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?
Question 3 of 5
Which property creates a stacking context without requiring position or z-index?
Question 4 of 5
Among non-positioned elements with the same stacking level, what determines which appears on top?
Question 5 of 5
Why does applying transform to a parent element break position: fixed on its children?
Chapter Summary
  • Natural stacking order (back to front, no z-index): root background → non-positioned blocks → floats → inline elements → positioned elements (DOM order).
  • z-index sets 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: 500 etc. Prevents z-index wars.
  • Bug #1: z-index ignored → add position: relative to the element.
  • Bug #2: z-index trapped → raise or remove the parent stacking context.
  • Bug #3: position: fixed broken → transform on 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.
~7 min read
Intermediate level
Chapter 24 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel