Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Reference › z-index

CSS Reference

z-index

Controls the stacking order of positioned elements. Higher numbers appear on top.

Last updated: October 2026

Definition: The z-index property controls the stacking order of overlapping elements. Higher values appear on top of lower values. It only works on positioned elements (relative, absolute, fixed, sticky). Understanding stacking contexts is critical to using z-index correctly.

🎬 Live Example

Three overlapping boxes with different z-index values.

z-index: 1
z-index: 2
z-index: 3

💡 All three boxes are positioned with position: absolute. The z-index determines which appears on top.

📝

Basic Syntax

.element {
    position: relative;  /* Required */
    z-index: 10;
}

Without position, z-index does nothing. The default value is auto.

🎯 z-index Values

Value Meaning
autoDefault. Element stacks based on DOM order.
0Creates a stacking context. Sits on the baseline.
1, 10, 100Positive integers. Higher is on top.
-1Behind the parent's background. Use carefully.
9999Common "on top of everything" value. Avoid.
1

Why z-index Fails — The Position Rule

The #1 reason developers get frustrated with z-index.

/* ❌ Does nothing — element is static */
.box {
    z-index: 100;
}

/* ✅ Works — element is positioned */
.box {
    position: relative;
    z-index: 100;
}

💡 Fix: If z-index isn't working, add position: relative first. 90% of z-index problems are solved by this.

2

The Defined z-index Scale

Never use random z-index values. Define a scale once and stick to it.

:root {
    --z-base: 0;
    --z-dropdown: 10;
    --z-sticky: 100;
    --z-fixed: 200;
    --z-modal-backdrop: 900;
    --z-modal: 1000;
    --z-popover: 1100;
    --z-tooltip: 1200;
    --z-toast: 1300;
}

.dropdown { z-index: var(--z-dropdown); }
.navbar   { z-index: var(--z-sticky);   }
.modal    { z-index: var(--z-modal);    }
.toast    { z-index: var(--z-toast);    }

This prevents "z-index wars" — the situation where every developer uses bigger numbers than the last one to force elements on top.

3

Stacking Contexts

A stacking context is a scoped area where z-index values only compete with other elements in the same context.

What creates a stacking context:

  • Positioned element with a z-index value other than auto.
  • Element with transform, filter, or perspective.
  • Element with opacity less than 1.
  • Element with mix-blend-mode other than normal.
  • Element with isolation: isolate.
  • Element with will-change on certain properties.
  • Flex or Grid items with z-index other than auto.

Consequence: A child element can never appear on top of a sibling of its parent's stacking context, no matter how high its z-index is. The parent's stacking context is the ceiling.

4

Common Real-World z-index Values

/* Dropdown menus */
.dropdown-content {
    position: absolute;
    z-index: 10;
}

/* Sticky header */
.header {
    position: sticky;
    top: 0;
    z-index: 100;
}

/* Fixed sidebar */
.sidebar {
    position: fixed;
    z-index: 200;
}

/* Modal backdrop */
.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 900;
}

/* Modal itself */
.modal {
    position: fixed;
    z-index: 1000;
}

/* Toast notification */
.toast {
    position: fixed;
    z-index: 9999;
}
5

How to Fix z-index Problems

A systematic debugging approach:

  1. Check position. The element must have position: relative, absolute, fixed, or sticky.
  2. Check the parent's stacking context. Open DevTools and look at the ancestors. If any have transform, filter, opacity: < 1, or a z-index, they create a stacking context.
  3. Check sibling order. Elements with the same z-index stack by DOM order. If your element is earlier in the DOM, it's behind.
  4. Check for negative z-index ancestors. A parent with negative z-index puts its children behind the parent's siblings.
  5. Use the DevTools "Layers" panel. Chrome's Layers panel visualizes stacking contexts and helps spot the culprit.

🎯 Common z-index Use Cases

Element Typical z-index
Dropdown menu10
Sticky header100
Fixed sidebar200
Back-to-top button500
Modal backdrop900
Modal dialog1000
Popover / tooltip1100
Toast notification1300

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the CSS z-index property?

z-index controls the stacking order of positioned elements. Higher values appear on top of lower values. It only works on positioned elements — relative, absolute, fixed, or sticky.

Why is my z-index not working?

The most common reason is that the element isn't positioned. Add position: relative, absolute, fixed, or sticky. The second most common reason is that the element is inside a stacking context that prevents it from being compared with the target.

What is a stacking context?

A stacking context is a local scoped area where z-index values are relative only to other elements in the same context. It's created by positioned elements with z-index, transform, filter, opacity less than 1, and other properties.

What z-index values should I use?

Use a defined scale, not random numbers. Example: base content (0), dropdowns (10-100), sticky headers (100-500), modals (1000+), toasts (9999). Define these as CSS variables so the whole team uses the same values.

Can z-index be negative?

Yes. Negative z-index places the element behind its parent's background. Use it sparingly — it often causes confusion and can hide content behind other elements.

🎯 What's Next?

← All CSS Reference