Z-Index & Stacking
Every positioned element sits on an invisible Z axis — the depth axis
that points toward your screen. z-index
controls the order in which elements stack on this axis. Higher values sit on top.
Tailwind ships z-index utilities from z-0
to z-50, plus negative values and
z-auto. Understanding
stacking contexts — and why z-index sometimes seems to "not work" —
is one of the most important CSS concepts you'll encounter.
isolation utility — creating
intentional stacking contexts
tailwind.config.js
How Z-Index Works
The Z axis runs perpendicular to your screen — elements with a higher z-index appear closer to the viewer and render on top of elements with lower values. There are two critical rules to understand before using z-index:
position set to relative, absolute,
fixed, or sticky — not static (the default).
Adding z-50 to a static element has zero effect.
<!-- ✗ WRONG — z-index has NO effect on static elements -->
<div class="z-50">This z-index is ignored (position is static by default)</div>
<!-- ✓ CORRECT — add a position utility first -->
<div class="relative z-50">Now z-50 works</div>
<div class="fixed z-50 inset-0">Modal backdrop</div>
<div class="sticky top-0 z-40">Sticky header</div>
<!-- Stacking order — higher value = on top -->
<div class="relative z-10">Behind</div>
<div class="relative z-30">In front of z-10, behind z-40</div>
<div class="relative z-40">On top of all above</div>
All Tailwind Z-Index Utilities
Tailwind ships a predefined set of z-index values designed for a layered UI system. They use steps of 10 so you can easily insert values between layers using custom config.
| Class | CSS value | Common UI use |
|---|---|---|
| -z-10 | z-index: -10 | Decorative backgrounds, pseudo-element gradients |
| z-0 | z-index: 0 | Reset; base stacking level |
| z-10 | z-index: 10 | Dropdowns, tooltips, hover cards |
| z-20 | z-index: 20 | Sticky table headers, date pickers |
| z-30 | z-index: 30 | Fixed navbars (above sticky headers) |
| z-40 | z-index: 40 | Side drawers, off-canvas menus |
| z-50 | z-index: 50 | Modals, full-screen overlays |
| z-auto | z-index: auto | Browser default stacking order |
Stacking Contexts — The Root of Z-Index Confusion
A stacking context is an isolated Z-axis environment. Elements inside a stacking context are stacked relative to each other, and the entire context is treated as a single unit relative to the outside world. Z-index values inside a context cannot affect elements outside it — no matter how large the value.
<!-- ── The problem: z-999 stuck behind z-1 ──────────────── -->
<!-- Parent A creates a stacking context (e.g. opacity or transform) -->
<div class="relative z-1 opacity-90"> <!-- opacity creates a stacking context -->
<div class="absolute z-[999]">
Dropdown — CANNOT appear above Parent B despite z-999!
</div>
</div>
<!-- Parent B at z-2 — the entire context of A is behind B -->
<div class="relative z-2">
This sits above everything inside Parent A
</div>
<!-- ── The fix: move the dropdown out of the stacking context ── -->
<!-- Option 1: use a portal / teleport (React, Vue) to move the -->
<!-- dropdown to the body's direct child -->
<!-- Option 2: remove the property creating the stacking context -->
<!-- (remove opacity, transform, will-change, etc.) -->
<!-- Option 3: add isolation-auto to break the context -->
Many CSS properties create a stacking context. Here are the most common ones you'll encounter in Tailwind projects:
opacity-95 through opacity-5 all create a stacking
context.scale-*, rotate-*, translate-* all create a
stacking context.blur-*,
brightness-*, backdrop-blur-* create stacking
contexts.
isolate class
intentionally creates a stacking context without any visual effect.
isolation — Intentional Stacking Contexts
Tailwind's isolate utility sets
isolation: isolate, which creates a stacking
context without any visual side effect. It's the clean way to say
"treat everything inside this element as its own stacking group."
class="isolate"
isolate
class from a parent or component library.class="isolation-auto"
<!-- Isolate a card's z-index context so its badge never -->
<!-- escapes to overlap external elements ─────────────── -->
<div class="isolate relative rounded-2xl overflow-hidden">
<img class="block w-full" src="..." alt="...">
<!-- This badge's z-50 is scoped INSIDE the isolate container -->
<span class="absolute top-3 right-3 z-50 bg-red-500 text-white ...">
NEW
</span>
<!-- Gradient overlay also scoped to this context -->
<div class="absolute inset-0 bg-gradient-to-t from-black/70 to-transparent z-10"></div>
</div>
<!-- Building a component library — always isolate reusable components -->
<div class="isolate"> <!-- DatePicker component -->
<input class="...">
<div class="absolute z-50 ...">Calendar dropdown</div>
</div>
Designing a Z-Index Layer System
The key to avoiding z-index wars is using a deliberate layer system where each UI layer has a defined z-index range. Tailwind's 10-step increments are designed exactly for this. Here is a production-ready layer system that maps well to Tailwind's built-in classes.
<!-- ── Layer -10: Decorative backgrounds ──────────────── -->
<div class="absolute -z-10 inset-0
bg-gradient-to-br from-cyan-500/10 via-transparent to-purple-500/10"></div>
<!-- ── Layer 10: Dropdown ─────────────────────────────── -->
<div class="absolute top-full left-0 z-10 mt-2 w-56
bg-white dark:bg-gray-900 rounded-xl shadow-xl border
border-gray-100 dark:border-gray-800">
Dropdown menu
</div>
<!-- ── Layer 20: Sticky header ────────────────────────── -->
<header class="sticky top-0 z-20 bg-white dark:bg-gray-950 border-b">
Sticky page header — above content, below fixed nav
</header>
<!-- ── Layer 30: Fixed navigation ─────────────────────── -->
<nav class="fixed top-0 inset-x-0 z-30
bg-white/90 dark:bg-gray-950/90 backdrop-blur-sm border-b">
Always-visible fixed navbar — above sticky header
</nav>
<!-- ── Layer 40: Side drawer ──────────────────────────── -->
<aside class="fixed inset-y-0 right-0 z-40 w-80
bg-white dark:bg-gray-900 shadow-2xl">
Cart drawer — above navbar, below modal
</aside>
<!-- ── Layer 50: Modal ────────────────────────────────── -->
<div class="fixed inset-0 z-50 flex items-center justify-center
bg-black/60 backdrop-blur-sm">
<div class="bg-white dark:bg-gray-900 rounded-2xl p-8 max-w-md w-full mx-4 shadow-2xl">
Modal — highest layer, above everything
</div>
</div>
Custom Z-Index Values in Config
Tailwind's built-in z-index values (0, 10, 20, 30, 40, 50) cover most use cases, but
you can extend or add custom values in tailwind.config.js.
Common additions include a layer for toast notifications (above modals) and specific
intermediate values between the built-in steps.
module.exports = {
content: ["./src/**/*.{html,js,jsx,tsx}"],
theme: {
extend: {
zIndex: {
/* Add values between built-in steps */
'5': '5', // z-5 — between base and dropdown
'15': '15', // z-15 — between dropdown and sticky
'25': '25', // z-25
/* Toast layer — above modals */
'60': '60', // z-60 — toast / snackbar notifications
'70': '70', // z-70 — above toasts
/* Semantic names (alternative approach) */
'dropdown': '10', // z-dropdown
'sticky': '20', // z-sticky
'fixed': '30', // z-fixed
'drawer': '40', // z-drawer
'modal': '50', // z-modal
'toast': '60', // z-toast
},
},
},
plugins: [],
};
/* Usage examples with custom names */
<div class="fixed z-toast ...">Toast notification</div>
<div class="fixed z-modal ...">Modal</div>
<nav class="sticky top-0 z-sticky ...">Sticky header</nav>
/* Arbitrary z-index values (no config needed) */
<div class="relative z-[999]">Arbitrary z-index</div>
<div class="relative z-[1000]">Above z-999</div>
Debugging Z-Index Issues
Z-index bugs are among the most frustrating CSS problems. Here is a systematic debugging approach and the most common causes with their fixes.
| Symptom | Most likely cause | Fix |
|---|---|---|
| z-index has no effect | Element has position: static |
Add relative, absolute, fixed, or sticky |
| Element is behind despite high z-index | It's inside a stacking context with a lower z-index than the element it needs to be above | Move element out of its stacking context (portal), or increase the stacking context's z-index |
| Dropdown is hidden behind sticky header | Dropdown's stacking context has lower z-index than header | Increase the dropdown trigger's z-index, or make sure the dropdown's z-index is higher than the header's |
| Tooltip disappears behind next card | Each card creates its own stacking context (e.g. via transform animation)
|
Use isolate on cards,
or move the tooltip to a portal at the body level |
| Modal backdrop doesn't cover navbar | Navbar has higher z-index than the modal | Increase modal z-index to be higher than the navbar (z-50 vs z-30) |
| Elements overlap incorrectly without z-index | Painting order — later elements in DOM paint on top by default | This is correct behaviour. Add z-index if you need to change the order |
/* ── Debugging checklist ─────────────────────────────── */
/* Step 1: Does the element have a position? */
/* Open DevTools, inspect the element, check: position != static */
/* Step 2: Identify every ancestor that creates a stacking context */
/* Look for ancestors with: opacity < 1, transform, filter, will-change */
/* Step 3: Find the "containing" stacking context of both elements */
/* The element that should be on top needs its CONTEXT to be higher z-index */
/* Step 4: Quick test — temporarily set z-index: 9999 */
<div class="relative z-[9999]">
If this is STILL behind something, you have a stacking context problem.
If it's now on top, just lower the value to a reasonable number.
</div>
/* Step 5: Use browser DevTools — "3D View" or "Layers" panel */
/* Chrome DevTools → Rendering tab → Enable "Layer borders" */
/* Firefox DevTools → Layers (3D view) in the toolbar */
/* Common patterns to find stacking context sources */
.problem {
opacity: 0.99; /* Creates stacking context! */
transform: translateZ(0); /* Creates stacking context! */
will-change: transform; /* Creates stacking context! */
filter: blur(0); /* Creates stacking context! */
}
Negative Z-Index
Tailwind provides -z-10 which sets
z-index: -10. Negative z-index values push
elements behind everything else in their stacking context, including the
background of their parent element. Perfect for decorative shapes and background effects.
<!-- ── Decorative gradient blob behind hero text ─────── -->
<section class="relative overflow-hidden py-24">
<!-- Decorative blobs behind the content -->
<div class="absolute -z-10 top-0 left-1/4
w-96 h-96 bg-cyan-400/20 rounded-full
filter blur-3xl"></div>
<div class="absolute -z-10 bottom-0 right-1/4
w-96 h-96 bg-purple-400/20 rounded-full
filter blur-3xl"></div>
<!-- Hero content sits above the blobs -->
<div class="relative z-10 text-center">
<h1 class="text-6xl font-black">Hero Title</h1>
</div>
</section>
<!-- ── Grid pattern behind a section ─────────────────── -->
<section class="relative">
<div class="absolute -z-10 inset-0
bg-[linear-gradient(to_right,#80808012_1px,transparent_1px),
linear-gradient(to_bottom,#80808012_1px,transparent_1px)]
bg-[size:24px_24px]"></div>
<div class="py-20 px-4">Content on top of grid</div>
</section>
- Z-index only works on positioned elements — the element must have
relative,absolute,fixed, orsticky. Addingz-50to astaticelement has no effect. - Tailwind z-index scale: -z-10 → z-0 → z-10 → z-20 → z-30 → z-40 → z-50 → z-auto. Use steps of 10 so you can insert values between layers if needed.
- Stacking contexts are isolated Z-axis environments. An element inside a stacking context with z-index: 1 can never appear above an element outside that context — no matter how large its own z-index value is.
- Common stacking context triggers:
opacity < 1, anytransform, anyfilter,will-change: transform/opacity, andisolation: isolate. - Use
isolateto intentionally create a stacking context on a component — prevents its internal z-index values from escaping and interfering with other elements on the page. - Recommended UI layer system: decorations (-z-10) → content (z-0) → dropdowns (z-10) → sticky (z-20) → fixed nav (z-30) → drawers (z-40) → modals (z-50) → toasts (z-60, custom).
- Negative z-index (-z-10) pushes elements behind all content — perfect for decorative gradient blobs, grid patterns, and pseudo-element backgrounds.
- Debugging: if raising z-index to 9999 still doesn't work, you have a stacking context problem. Look for ancestors with opacity, transform, filter, or will-change properties.
Quick Check — Questions & Answers
Click each question to reveal the detailed answer.
opacity < 1, a transform, a filter, or an
explicit z-index).Z-index values inside a stacking context are only compared within that context. No matter how large you make the z-index inside the context, the entire context is treated as one unit relative to outside elements.
Fixes:
1. Move the dropdown to a portal (render it as a direct child of
<body> using React's createPortal or Vue's
<Teleport>)2. Remove the property creating the stacking context from the parent (remove
opacity-*, transform, filter, or
will-change)3. Increase the z-index of the stacking context container itself
isolate sets isolation: isolate, which creates a new
stacking context on the element without any visual side effect. All
elements inside the isolate container are stacked relative to each other, and the
entire container is treated as a single unit by the outside world.Use it when:
• Building reusable components that use z-index internally — you don't want those values escaping and clashing with other components on the page
• A card component uses
z-10 for an internal badge but you don't want
that badge appearing above an external dropdown• Building a component library — add
isolate to all component roots as
a best practiceThink of it as "what happens in this component, stays in this component."
This means that if you add
opacity-95 (or any value from
opacity-5 to opacity-99) to a parent element, all children
are stacked within that context. Any z-index on those children only has meaning
within the parent — not relative to external elements.The same applies to:
transform (any value), filter (any
value), backdrop-filter, will-change: transform or
will-change: opacity.Common Tailwind gotcha: adding
backdrop-blur-sm to a
navbar creates a stacking context on it — if your dropdown's parent is below this
navbar in the DOM, it may get stuck behind it.
z-50 (z-index: 50). Since modals
typically use z-50, toast notifications need a value higher than
50.The recommended approach is to add a custom z-index value in
tailwind.config.js:zIndex: { '60': '60', 'toast': '60' }Then use
z-60 or z-toast on your toast container:<div class="fixed top-4 right-4 z-60">Toast</div>Always make sure the toast's
fixed element is a direct child of
<body> (or rendered via a portal) so it's not inside any stacking
context that would restrict its z-index.
z-0 sets z-index: 0 — an explicit numeric
value. The element participates in the stacking context with the value 0, which
does create a stacking context when combined with a non-static
position.z-auto sets z-index: auto — the browser
uses the natural painting order (DOM order) to determine stacking. Critically,
z-index: auto does NOT create a stacking context
(unlike z-index: 0).Practical difference: a
relative z-0 element creates a
stacking context (its children are stacked within it). A
relative z-auto element does not create a stacking context on its own —
children can escape it (assuming no other context-creating property).
MCQ Quiz — Test Yourself
Select the best answer. The correct option is revealed after you click.
z-50
to a div but it has no effect.
What is the most likely reason?isolate utility do?
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.