Line Up Pool Cards With Subgrid

Rosa’s site now has type and spacing that scale with the window and grow when she zooms. Further down the home page, though, she has another problem. When she compares the three pools to pick one for the weekend, her eye has to hunt because each card’s opening times start at a different height, and so do its lane badges.

In this lesson, you’ll make every card’s name, opening times, and lane badges line up across the row, whatever the length of their text. You’ll do it with subgrid, and you’ll check the result in the browser’s Grid overlay instead of trusting your eyes.

See Why the Cards Don’t Line Up

Cards line up only if they share rows, and these cards don’t. Your home page so far has only the header and the “Today” panel, so this lesson’s starting files add a section listing the pools below that panel, with a card for each of the three pools and their styles. Open this lesson’s folder from the course’s download, on the course page, and start Live Preview there. The cards sit in a list:

Language: HTML
<ul class="pool-list">
  <li class="pool-card">
    <h3>Riverside Lido</h3>
    <p>Every day, 6:00 a.m. to 8:00 p.m.</p>
    <ul class="lane-badges">
      <li>Fast</li>
      <li>Medium</li>
      <li>Slow</li>
    </ul>
  </li>
  <!-- The Leisure Centre's and Northgate Baths' cards -->
</ul>

The list is a grid, and each card stacks its three parts in a flex column. You’ll find both rules in the components layer of styles.css:

Language: CSS
.pool-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
  gap: var(--space-gap);
  margin-block-start: var(--space-gap);
  padding: 0;
  list-style: none;
}

.pool-card {
  display: flex;
  flex-direction: column;
  gap: var(--space-gap);
  padding: var(--space-inset);
  background-color: var(--color-surface-card);
  border: 2px solid var(--color-accent);
}

Text wraps differently at different widths, so set one first. Open DevTools with F12 (Cmd+Option+I on macOS). Then turn on the device toolbar with Ctrl+Shift+M (Cmd+Shift+M on macOS) and set the width to 1280 pixels. Keep that width until the checks at the end of the lesson.

Scroll down to the cards in Live Preview. The Leisure Centre’s name wraps onto two lines, and its opening times run to three, so everything below them starts lower than in the other two cards:

Three pool cards side by side. The middle card's two-line name pushes its opening times and lane badges lower than in the cards on either side.
The Pool Cards Before Subgrid

To see what the grid itself is doing, find <ul class="pool-list"> in the Elements panel, and click the grid badge next to it. That turns on the Grid overlay, which draws the grid’s tracks on the page, with the gaps between them hatched. Hovering over the element in the Elements panel shows the same lines until you move the pointer away:

DevTools highlights the pool list's grid, which has a single row. Each visible card fills one column of that row, with its parts at different heights.
One Grid Row, Three Separate Card Layouts

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.