1. Flexbox: center horizontally and vertically

The most common pattern for cards, heroes, and empty states.

Perfect center
Centered

justify-content and align-items center the child.

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 12rem;
}

2. Grid: place-items center

One property on the grid container centers all children.

Grid center
Centered

place-items: center is shorthand for align and justify.

.parent {
  display: grid;
  place-items: center;
  min-height: 12rem;
}

3. Margin auto for horizontal centering

Works on block-level elements with a set width.

margin-inline: auto
Block centered

The block needs width less than 100%.

.card {
  width: min(20rem, 100%);
  margin-inline: auto;
}

Frequently asked questions

Flexbox vs grid for centering?
Both work. Flex is ideal when you also distribute space along an axis; grid place-items:center is the shortest one-liner.
How do I center without flex or grid?
Use position absolute with top/left 50% and transform translate(-50%, -50%), or margin auto for horizontal only.
Why is my div not centering?
Check parent height, display type, and whether the child is inline. Inline elements need text-align:center on the parent.