liveBook · Manning

Z-index

As explained in CSS in Depth, Second Edition

The z-index property specifies the stacking order of a positioned or stacking-context element.

The z-index property controls which positioned elements appear in front when their boxes overlap. Larger integer values paint in front of smaller values; negative values can place elements behind static elements. section 6.4.1 section 6.4.2

Use z-index with these rules in mind:

  • Apply it only to positioned elements; static elements cannot have their stacking order changed this way. section 6.4.2
  • Treat every z-indexed positioned element as the root of a new stacking context containing all its descendants. section 6.4.2 section 6.4.3
  • Compare stacking contexts before comparing descendants: a child with z-index 100 cannot escape its parent context and overtake an element in a higher context. section 6.4.3

Putting a modal body above its backdrop

A fixed modal can remain anywhere in the markup, but assigning the backdrop z-index 1 and the modal body z-index 2 makes the body paint in front. Both elements are positioned, so the values take effect. section 6.4.1 section 6.4.2

Listing 6.13 Creating the stacking contexts
body {
  margin: 40px;
}

.box {
  display: inline-block;
  width: 200px;
  line-height: 200px;
  text-align: center;
  border: 2px solid black;
  background-color: #ea5;
  margin-left: -60px;
  vertical-align: top;
}

.one { margin-left: 0; }
.two { margin-top: 30px; }
.three { margin-top: 60px; }

.positioned {                 #1
  position: relative;         #1
  background-color: #5ae;     #1
  z-index: 1;                 #1
}                             #1

.absolute {
  position: absolute;
  top: 1em;
  right: 1em;
  background-color: #fff;
  border: 2px dashed #888;
  z-index: 100;             #2
  line-height: initial;
  padding: 1em;
}

Experiment with two overlapping positioned boxes and a nested absolute element. Although the nested element has z-index 100, its parent stacking context can remain behind the other box. section 6.4.3

--z-loading-indicator: 100;
--z-nav-menu:          200;
--z-dropdown-menu:     300;
--z-modal-backdrop:    400;
--z-modal-body:        410;

A named scale keeps interface layers understandable: loading indicator, navigation menu, dropdown, modal backdrop, and modal body receive progressively higher values. section 6.4.3

How CSS in Depth, Second Edition covers it

How deep it goes
Explains integer ordering, positioning constraints, stacking contexts, and nested-context limits through worked examples.
Examples
Uses a modal backdrop and body, overlapping boxes, a nested absolute element, and a named layer scale.
What it assumes
Builds on document flow and relative, absolute, fixed, and positioned-ancestor behavior.
What you can do after
Diagnose stacking conflicts with browser developer tools and assign deliberate z-index values to positioned layers.
How it connects
Connects z-index with positioned elements, document flow, absolute positioning, fixed positioning, and stacking contexts.

Also explained in

Compare how 2 books explain z-index →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.