Card grids

Topic 3 of 4 in Layout patterns7 min readBeginnerFree course

Overview

Card grids are everywhere — courses, products, articles, team members. They look simple, but real content makes them hard: the number of cards changes, titles have different lengths, images come in every shape, and screens range from 320px to 2560px. This lesson combines techniques from earlier chapters into one robust pattern.

Anatomy of a robust card grid: three cards, one with a much longer title, whose images, titles and Enrol buttons line up. auto-fit, minmax(min(16rem, 100%), 1fr): columns appear as space allows. aspect-ratio: 16 / 9 + object-fit: cover: images never distort or jump. grid-template-rows: subgrid: titles, text and buttons line up across cards. gap: 1.5rem: consistent gutters, no margin hacks. Each line solves a real-world problem: unknown numbers of cards, mixed title lengths, images in different shapes, and screens narrower than a column’s minimum.

The complete pattern

HTML
<ul class="cards">
  <li class="card">
    <img src="flexbox.jpg" width="1200" height="675" alt="" />
    <h3><a href="/courses/flexbox">Flexbox</a></h3>
    <p>Lay out items in a row or column.</p>
    <footer><span class="price">$19</span> <span>4 hours</span></footer>
  </li>
  …
</ul>
CSS
.cards {
  list-style: none;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr));
  gap: 1.5rem;
}

.card {
  display: grid;
  grid-row: span 4;
  grid-template-rows: subgrid;
  row-gap: 0.75rem;
  position: relative;
}

.card img {
  inline-size: 100%;
  block-size: auto;
  aspect-ratio: 16 / 9;
  object-fit: cover;
}

.card footer { display: flex; justify-content: space-between; align-items: center; }

Let’s take each decision in turn.

1. The grid: auto-fit and minmax

repeat(auto-fit, minmax(min(16rem, 100%), 1fr)) gives as many columns as fit, at least 16rem wide, sharing extra space — and never overflowing a container narrower than 16rem. One card or a hundred, phone or ultrawide, it adapts.

auto-fit stretches cards to fill the row when there are few of them. If a single search result shouldn’t become one enormous card, use auto-fill instead, which keeps the empty column tracks.

2. The images: a fixed shape

aspect-ratio: 16 / 9 with object-fit: cover makes every image the same shape, cropping instead of stretching — so square, tall and wide uploads all look tidy. The width and height attributes in the HTML let the browser reserve space before the image loads, so the grid doesn’t jump.

Use alt="" when the image is decorative and the card’s title already says what the card is about; otherwise describe the image.

3. Alignment: subgrid

Without subgrid, a card with a two-line title pushes its description and footer down, out of line with its neighbours. With grid-row: span 4 and grid-template-rows: subgrid, the four parts of every card in a row share the same four row tracks, each sized by the tallest content in that row. Titles, descriptions and footers line up, whatever the content.

The footer is pushed to the bottom automatically, because its row starts at the same line in every card.

4. The clickable card

Users expect to click anywhere on a card. Wrapping the whole card in an <a> works, but screen readers then read the entire card as one long link, and you can’t have other links or buttons inside it.

The better pattern keeps a single link on the title and stretches its clickable area over the card:

CSS
.card { position: relative; }

.card h3 a::after {
  content: "";
  position: absolute;
  inset: 0;           /* cover the whole card */
}

.card:focus-within {
  outline: 2px solid #2d5bd7;
  outline-offset: 2px;
}

The link’s ::after pseudo-element is absolutely positioned against the card (the nearest positioned ancestor), so a click anywhere on the card hits the link. There’s one tab stop per card, and the link text is just the title. :focus-within draws the focus ring around the whole card when the link is focused.

If a card also needs its own buttons (like “Add to wishlist”), give them position: relative; z-index: 1 so they sit above the stretched link.

5. Semantics: use a list

Marking the grid up as <ul> with <li> cards means screen readers announce “list, 12 items” and let users skip through items. Grid works on any element, so you lose nothing visually. (Safari removes list semantics when list-style: none is set; adding role="list" to the <ul> restores them if your audience relies on it.)

Variations

Make the first card bigger with a span:

CSS
@media (width >= 48rem) {
  .card.featured { grid-column: span 2; }
}

With spans in an auto-fit grid, consider grid-auto-flow: dense to fill gaps — but only if card order doesn’t matter.

Horizontal cards in a list view

When the container is wide enough, a container query can turn each card horizontal:

CSS
.card-wrap { container-type: inline-size; }
@container (width >= 40rem) {
  .card { grid-template-columns: 16rem 1fr; }
}

Equal heights without subgrid

If cards only need to be the same height (not aligned inside), grid already does that: items stretch to fill their row. Add display: flex; flex-direction: column to the card and margin-top: auto on the footer to push it to the bottom.

Checklist for every card grid: auto-fit with min(), fixed image ratio with object-fit, subgrid for alignment, one stretched link per card, and a list for semantics.

Exercise

The course catalogue

Build .catalogue as a production-ready card grid: as many columns as fit with a 16rem minimum and no overflow on tiny screens; 1.5rem gaps; every image 16 / 9 and cropped; card titles, descriptions and the footer row aligned across cards with subgrid; and the whole card clickable through the title link while the .price stays separate text. Keep the card’s link the only focusable element.

HTML
<ul class="catalogue">
  <li class="card"><img alt="" width="600" height="600" /><h3><a href="#">Flexbox</a></h3><p>Lay out items in a row or column.</p><footer><span class="price">$19</span><span>4h</span></footer></li>
  <li class="card"><img alt="" width="600" height="300" /><h3><a href="#">Grid layout from first principles to production</a></h3><p>Two-dimensional layout.</p><footer><span class="price">$29</span><span>6h</span></footer></li>
  <li class="card"><img alt="" width="400" height="600" /><h3><a href="#">Container queries</a></h3><p>Components that adapt to the space they are given, wherever they are.</p><footer><span class="price">$19</span><span>3h</span></footer></li>
</ul>

<style>
  * { box-sizing: border-box; }
  .catalogue { list-style: none; padding: 0; margin: 0; }
  .card { border: 1px solid #d6d6d2; border-radius: 12px; overflow: hidden; }
  .card img { background: #c9d8f8; }
  .card > :not(img) { margin: 0; padding-inline: 1rem; }
  /* your code here */
</style>

Summary

  • Start with repeat(auto-fit, minmax(min(16rem, 100%), 1fr)) for a grid that reflows without breakpoints.
  • Give images aspect-ratio and object-fit: cover (plus width and height attributes) so every card has the same shape.
  • Use subgrid rows so titles, text and footers line up across each row of cards.
  • Make the whole card clickable by stretching the title link’s ::after over the card — one link, one tab stop.
  • Use a list (<ul>) for a set of cards so screen readers announce how many there are.