The Cascade ·

Center anything with place-items

Two lines of CSS to center a child both ways, and why grid beats the old tricks.

  • #layout
  • #grid
Centering used to take a pile of tricks: auto margins, absolute positioning with negative offsets, a table-cell hack. With grid it takes two declarations.
Live examplePoint at anything to see its CSS
.stage {
  display: grid;
  place-items: center;
  min-height: 100vh;
  background: #f4efe6;
}

The whole technique

Turn the parent into a grid container, then set place-items: center. That shorthand sets both align-items and justify-items, so the child is centered on both axes.

.stage {
  display: grid;
  place-items: center;
  min-height: 100%;
}

Why it works

A grid with no explicit tracks creates one implicit cell that fills the container. place-items then positions the child inside that cell.

When to reach for something else

  • Several children in a row or column: use flexbox with justify-content and align-items.
  • Centering a single block horizontally only: margin-inline: auto is enough.