← All posts

CSS subgrid: line up titles, text and buttons across a card grid

Card titles and buttons that never line up? With grid-template-rows: subgrid, each row of content matches the tallest card, with no fixed heights.

CSS subgrid: line up titles, text and buttons across a card grid

Key points

  • subgrid lets the contents of several cards share the same grid rows.
  • Titles, text and buttons line up with the tallest card in the row, automatically.
  • Pin each element to its own row so cards with missing parts don’t shift.

Case studies, a services list, a pricing page: three cards side by side, one title on a single line, the next one wrapping onto two. Now the paragraphs start at different heights and the buttons form a staircase. The usual patches, a min-height on titles or clamped text, break the moment the client edits a card, and on a bilingual site they break for certain, because the French title is rarely the same length as the English one. CSS finally has a proper fix.

How it works

In CSS Grid each card is a grid item, but its children can’t see the parent’s tracks. grid-template-rows: subgrid changes that: the card spans several parent rows and its children are placed on them directly. Every card in a row then shares one “title” row, as tall as the longest title.

<ul class="cards">
  <li class="card">
    <img src="atelier.jpg" alt="" width="640" height="400">
    <h3>Brochure site redesign</h3>
    <p>A short title, a two-line summary…</p>
    <a href="/projects/redesign/">View project</a>
  </li>
  <!-- … -->
</ul>
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
  gap: 2rem 1.5rem;
}

.card {
  display: grid;
  grid-row: span 4;              /* image, title, text, button */
  grid-template-rows: subgrid;
  gap: .75rem;
  padding: 1rem;
  border: 1px solid #ddd;
  border-radius: .5rem;
}

span 4 matches the four children. The card can set its own, tighter gap while the space between rows of cards stays larger. No height is hard-coded anywhere: if a title wraps to three lines, the whole row adjusts.

Cards with missing pieces

This is what trips people up on our projects. When a card has no image, its title moves up into row one, its text into row two, and that card falls out of line. Give every kind of element an explicit row:

.card > img { grid-row: 1; }
.card > h3  { grid-row: 2; }
.card > p   { grid-row: 3; }
.card > a   { grid-row: 4; align-self: end; }

A card without an image now just leaves row one empty. The same goes for an optional “New” badge or price: reserve a row for each optional element, and make the span cover all of them.

Browser support and a fallback

Current versions of Chrome, Edge, Firefox and Safari all support subgrid. For older browsers we add a fallback that at least keeps buttons at the bottom of each card:

@supports not (grid-template-rows: subgrid) {
  .card { display: flex; flex-direction: column; }
  .card > a { margin-top: auto; }
}

Titles won’t line up across cards in that case, but the layout stays tidy. It’s the approach most sites used before subgrid existed.

When to skip it

When cards stack one per row on mobile, cross-card alignment no longer matters, yet the code keeps working, so there’s no need for a media query. For a masonry-style layout where cards are meant to have different heights, subgrid is simply the wrong tool.

In WordPress, the editor’s Grid block doesn’t offer this option. Add a custom class to the block and ship these rules with the theme.

FAQ

Do I still need to truncate long text?

Not for alignment. If a text really is too long, shorten it at the source rather than hiding part of it with line-clamp.

Does subgrid work for columns too?

Yes, with grid-template-columns: subgrid. It’s useful for pricing tables or forms laid out on a grid.

Need service or portfolio pages that hold up with real content in every language? We build layouts that survive whatever your team types into them.

Share LinkedIn Facebook X WhatsApp
</>

webtestudio

Web agency: custom WordPress sites, premium themes and SaaS software.

Discuss your project →