CSS Basics

CSS Z-Index

Master element stacking and layering with z-index. Control which elements appear in front and understand stacking contexts for complex layouts.

How does z-index work in CSS?

z-index decides which overlapping elements appear in front: a higher value is closer to the viewer. It only works on positioned elements (relative, absolute, fixed, sticky) and on flex or grid items, and it only competes within the same stacking context.

.modal-backdrop { position: fixed; inset: 0; z-index: 100; }
.modal          { position: fixed; z-index: 101; }
/* Common scale */
:root { --z-dropdown: 10; --z-sticky: 20; --z-modal: 100; --z-toast: 200; }

Z-Index Stacking Demo

Code Editor

<div class="stage">
  <div class="box a">z-index: 1</div>
  <div class="box b">z-index: 3</div>
  <div class="box c">z-index: 2</div>
</div>
<p>Change the numbers in the CSS tab to reorder the stack.</p>

Preview

Understanding Z-Index

Z-index controls the stacking order of positioned elements, determining which elements appear in front of others. It's essential for creating layered interfaces, modals, dropdowns, and any component that needs to appear above other content.

Key Rule

Z-index only works on positioned elements (position: relative, absolute, fixed, or sticky). Elements with position: static ignore z-index completely.

Stacking Contexts

A stacking context is a group of elements that are stacked together as a unit. Elements within a stacking context can only compete with siblings in the same context.

What Creates Stacking Contexts

  • • Positioned element with z-index ≠ auto
  • • Opacity < 1
  • • Transform ≠ none
  • • Filter ≠ none
  • • Isolation: isolate
  • • Will-change with certain values
  • • CSS containment

Context Rules

  • • Children can't escape parent's context
  • • Higher parent context wins
  • • Z-index only compares within same context
  • • Understanding prevents "z-index wars"

Z-Index Scale Systems

Recommended Z-Index Scale

Base Layers

  • -1: Hidden behind
  • 0: Base level
  • 10: Docked elements

Navigation

  • 100: Sticky headers
  • 200: Dropdowns
  • 300: Mobile menus

Overlays

  • 1000: Modals
  • 1100: Popovers
  • 1200: Tooltips

System

  • 9000: Skip links
  • 9999: Debug tools

Common Interface Patterns

Modal System

Backdrop:z-index: 1040
Dialog:z-index: 1050

Navigation

Header:z-index: 100
Dropdown:z-index: 200

Best Practices & Debugging

Best Practices

  • • Use a consistent z-index scale
  • • Document your z-index system
  • • Prefer CSS custom properties
  • • Avoid very high numbers (999999)
  • • Test across different browsers
  • • Use isolation: isolate when needed

Common Issues

  • • Forgetting position property
  • • Not understanding stacking contexts
  • • Z-index wars (escalating values)
  • • Ignoring parent context limitations
  • • Performance impact of many layers
  • • Accessibility issues with overlays

Debugging Tips

Visual Debugging

  • • Add colored borders to positioned elements
  • • Use browser dev tools 3D view
  • • Create z-index indicators
  • • Log stacking contexts to console

Investigation Steps

  • • Check if element is positioned
  • • Find parent stacking context
  • • Compare z-index within same context
  • • Test with isolation: isolate

CSS z-index — step by step

1 / 4
1

How z-index Works

z-index controls the stacking order of positioned elements. Higher values appear in front. It only works on elements with position other than static.

1 of 4
⚠️z-index Without Position Does Nothing
Setting z-index on position: static elements has no visual effect. You must set position: relative, absolute, fixed, or sticky for z-index to participate in the stacking order.

🎯 Quick Check

Which CSS property creates a stacking context WITHOUT requiring z-index?

CSS z-index: summary

Key points

  • z-index has no effect on a position: static element that isn't a flex or grid item.
  • A new stacking context is created by position with a z-index other than auto, by opacity below 1, and by transform, filter or isolation: isolate, among others.
  • Children can never escape their parent's stacking context: a child with z-index: 9999 still sits below a sibling of its parent that's in front.
  • Keep a small, named scale (10, 20, 100…) in CSS variables instead of escalating to 99999.

Common mistakes

  • Raising z-index higher and higher when the real problem is a parent's stacking context.
  • Forgetting to add position: relative so the z-index applies.
  • A transform on an ancestor unexpectedly creating a stacking context and trapping a dropdown.

Frequently asked questions

›Why is my z-index not working?

Either the element isn't positioned (add position: relative), or it's inside a stacking context created by a parent with z-index, opacity < 1, transform or filter. In that case its z-index only applies inside that parent.

›What is a stacking context?

It's a group of elements that are layered together as one unit. Elements inside it are ordered against each other, and then the whole group is placed among its siblings using the parent's z-index.

›Can z-index be negative?

Yes. A negative z-index places the element behind its siblings, and even behind its parent's content if the parent doesn't form a stacking context.