Tutorial / CSS

A card grid that wraps without a framework

Beginner1 min6 Dec 2026

A tutorial index, a tool catalog, and a set of solutions all want the same layout: as many cards as the row can hold, then a wrap. Media queries can do it, and they tend to drift out of date the third time the card width changes.

The grid

CSS
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: 1rem;
}

auto-fit collapses empty tracks, so a single card on a wide screen stretches instead of sitting in a lonely column with a blank track beside it. minmax stops a card from shrinking below a readable width.

Pair it with padding, not a fixed height

Let the content decide the height. A fixed height clips a long title the first time a translation, or a longer error message, lands in the card.

  • Use 16rem when the card holds a title and two lines of text.
  • Drop toward 14rem for dense catalogs such as language lists.
  • Keep the gap in the same unit as the page padding so the rhythm matches.

Related