Skip to content

CSS z-index

The CSS z-index property controls the stacking order of overlapping elements. It determines which elements appear in front of or behind others and is commonly used for dropdowns, modals, sticky headers, tooltips, notifications, and overlays.

What Is CSS z-index?

The z-index property specifies the stack level of positioned elements. Elements with a higher z-index value appear above elements with a lower z-index value.

.modal {
  position: fixed;
  z-index: 1000;
}

Think of z-index as layers stacked on top of each other along the Z-axis. The larger the z-index value, the closer the element appears to the user.

Why CSS z-index Is Important

  • Controls overlapping elements.
  • Creates modals and popup dialogs.
  • Builds dropdown menus.
  • Manages sticky navigation layers.
  • Supports tooltips and notifications.
  • Prevents UI elements from appearing behind content.
  • Creates professional application layouts.

CSS z-index Cheatsheet

Value Description Typical Usage
auto Default stacking order. Normal document flow.
0 Base layer. Cards and containers.
1-10 Small layer elevation. Badges and highlights.
100 Above regular content. Sticky headers.
500 Navigation layer. Mega menus.
1000 High priority layer. Modals and dialogs.
9999 Maximum emergency layer. Debugging only.

z-index Requires Positioning

The z-index property only works on positioned elements.

.card {
  position: relative;
  z-index: 10;
}

Position values that work with z-index:

  • relative
  • absolute
  • fixed
  • sticky

Understanding Stacking Order

When elements overlap, the browser determines which one appears on top.

.red-box {
  position: absolute;
  z-index: 1;
}

.blue-box {
  position: absolute;
  z-index: 2;
}

The blue box appears above the red box because its z-index value is higher.

What Is a Stacking Context?

A stacking context is an independent stacking environment. Elements inside one stacking context cannot escape and overlap elements in another stacking context simply by increasing z-index values.

.parent {
  position: relative;
  z-index: 1;
}

.child {
  position: absolute;
  z-index: 9999;
}

Even though the child has a large z-index value, it remains inside its parent's stacking context.

Common z-index Use Cases

Sticky Header

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

Dropdown Menu

.dropdown-menu {
  position: absolute;
  z-index: 500;
}

Modal Dialog

.modal {
  position: fixed;
  z-index: 1000;
}

Tooltip

.tooltip {
  position: absolute;
  z-index: 1100;
}

Recommended z-index Layer System

Large applications often define a standard layering system.

Layer Recommended z-index
Page Content 0
Cards 10
Sticky Headers 100
Dropdown Menus 500
Side Panels 700
Modals 1000
Tooltips 1100
Notifications 1200

CSS z-index and Accessibility

  • Ensure modals receive keyboard focus.
  • Do not hide important content behind overlays.
  • Make tooltips accessible using keyboard navigation.
  • Test layered components with screen readers.
  • Avoid creating invisible elements above interactive controls.

CSS z-index and SEO

z-index does not directly affect search rankings, but poor layering can negatively impact user experience, accessibility, and Core Web Vitals.

  • Keep important content visible.
  • Avoid intrusive overlays.
  • Prevent navigation from being hidden.
  • Ensure mobile usability.
  • Improve engagement by reducing UI frustrations.

Common z-index Mistakes

  • Using random values like 999999 everywhere.
  • Forgetting position properties.
  • Ignoring stacking contexts.
  • Creating overlapping UI elements accidentally.
  • Not testing mobile layouts.
  • Blocking content with overlays.
  • Using z-index instead of proper layout techniques.

CSS z-index Best Practices

  • Create a project-wide z-index scale.
  • Use meaningful layer names.
  • Avoid excessively large values.
  • Understand stacking contexts.
  • Keep layering simple and predictable.
  • Test modals, dropdowns, and sticky elements.
  • Document your z-index system.

Key Takeaways

  • z-index controls stacking order.
  • Higher values appear above lower values.
  • Positioning is usually required.
  • Stacking contexts affect z-index behavior.
  • Use organized layer systems in large projects.
  • Test accessibility and mobile layouts.
  • Avoid unnecessary large z-index values.

Pro Tip

Instead of using random z-index values, define a design-system layer scale such as 0, 10, 100, 500, and 1000. This makes large applications much easier to maintain and debug.