effectsintermediate

Z-Index

Controls the stacking order of overlapping elements along the z-axis (depth).

Plain English

Z-index decides which element appears on top when elements overlap. Think of it as layers in Photoshop — higher numbers sit in front, lower numbers sit behind. Modals need to appear above everything, tooltips above their trigger, and dropdown menus above the page content. Without z-index management, UI elements fight each other for the foreground and things break.

Technical

z-index only works on positioned elements (position: relative, absolute, fixed, or sticky). It creates and interacts with stacking contexts — a z-index of 999 inside a low-stacking-context parent will still appear behind elements outside that parent. Common stacking context triggers: position + z-index, opacity < 1, transform, filter, will-change. Understanding stacking contexts is essential for debugging z-index issues.

Live Demo

z-index: 10

Back layer

z-index: 20

Middle layer

z-index: 30

Front layer

Stack order

z-30

Front (top)

z-20

Middle

z-10

Back (bottom)

Higher z-index = closer to the user.

Usage

✓ Good usage

Use a documented z-index scale (10/100/200/300/400) so every developer on the team knows that modals are always 300 and tooltips are always 100.

✗ Bad usage

Using z-index: 9999 to "fix" a stacking issue without understanding why it is happening — this is a red flag that often causes worse problems.

Common mistakes

AI Prompt Example

Copy this into Claude, Cursor, Bolt, or v0.

Use a z-index scale: sticky header at 10, dropdowns at 100, modals at 300, and toast notifications at 400. Document the scale in a comment at the top of the CSS.