CSS Subgrid for Card Layouts: A Practical Tutorial From Markup to Fallbacks

November 17, 2026

If you have built a pricing, blog, or feature-card grid, you have probably seen this: the cards look fine at first, but uneven titles push the body copy and call-to-action buttons out of alignment. CSS subgrid fixes that without fixed heights, magic margins, or duplicating content. This tutorial walks through one runnable card component, a basic grid, the subgrid enhancement, support checks, and a Flexbox fallback.

responsive website card ui

Why card rows stop aligning

Each card is usually an independent box. Inside that box, the title, metadata, description, and button size themselves according to their own content. When one card has a two-line title and another has one line, everything below the title shifts down by a different amount.

A common attempt is to give the text block a min-height or use :nth-child() line assumptions. That is brittle: it breaks when font size changes, copy is translated, or cards wrap to a different number of columns. The layout needs shared row tracks, not fixed content heights.

Subgrid lets a child grid item participate in its parent’s row tracks. In a card layout, that means every card can place its title in row 1, meta in row 2, text in row 3, and button in row 4.

css grid web design

Recommended HTML

The most reliable structure is also the simplest. Make every .card a direct grid item and make the visible sections direct children of that card. Avoid li > article > div chains that require several nested subgrids.

<div class="card-grid">
  <article class="card">
    <h3 class="card-title">Fast setup</h3>
    <p class="card-meta">5 min read</p>
    <p class="card-text">
      Start with semantic markup and let the browser align repeated regions.
    </p>
    <a class="card-button" href="#">Learn more</a>
  </article>

  <article class="card">
    <h3 class="card-title">A longer title that wraps onto a second line</h3>
    <p class="card-meta">Guide</p>
    <p class="card-text">
      The button still lines up because each card uses the same four shared row tracks.
    </p>
    <a class="card-button" href="#">View example</a>
  </article>

  <article class="card">
    <h3 class="card-title">Fallback ready</h3>
    <p class="card-meta">CSS</p>
    <p class="card-text">Older browsers get a clean Flexbox card instead of a broken grid.</p>
    <a class="card-button" href="#">See fallback</a>
  </article>
</div>

If you must include an image, keep the structure predictable. Either add a fifth shared row at the parent level, such as auto auto auto 1fr auto, and let every card contain an image in that row, or wrap only the middle content deliberately. A wrapper cannot magically inherit selected tracks; if it spans rows 2–4, its own internal grid would need subgrid as well. For most card alignment problems, the flat structure above is easier to verify.

Base Grid styles

Start with component styles that work everywhere. This is the visual foundation before any subgrid enhancement.

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 2rem;
  font-family: system-ui, sans-serif;
  background: #f5f7fb;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1.25rem;
  max-width: 1000px;
  margin: 0 auto;
}

.card {
  display: flex;
  flex-direction: column;
  padding: 1.25rem;
  background: #fff;
  border: 1px solid #d9dee8;
  border-radius: 14px;
}

.card-title {
  margin: 0;
  font-size: 1.15rem;
  line-height: 1.3;
}

.card-meta {
  margin: 0.5rem 0 0;
  font-size: 0.85rem;
  color: #667085;\n}

.card-text {
  margin: 0.75rem 0 1.25rem;
  color: #344054;\n  flex: 1;\n}

.card-button {
  margin-top: auto;
  display: inline-block;
  padding: 0.7rem 1rem;
  border-radius: 10px;
  background: #2457f5;\n  color: #fff;\n  text-decoration: none;
  font-weight: 600;
}

.card-button:hover {
  background: #1e46c4;\n}

What happens without subgrid support

In the fallback state, every card is a flex column. The description uses flex: 1, so it absorbs available space, and the button uses margin-top: auto, which pins it to the bottom. Buttons align when cards in the same row have equal height. Titles and metadata do not share exact cross-card row tracks, so their baselines can still differ.

Subgrid enhancement

Now define four row tracks on the outer grid. Each card spans all four rows and delegates its own row template to the parent with grid-template-rows: subgrid.

@supports (grid-template-rows: subgrid) {
  .card-grid {
    grid-template-rows: auto auto 1fr auto;
    align-items: stretch;
  }

  .card {
    grid-row: span 4;
    display: grid;
    grid-template-rows: subgrid;
    row-gap: 0;
  }

  .card-title {
    grid-row: 1;
    align-self: start;
  }

  .card-meta {
    grid-row: 2;
    margin-top: 0;
  }

  .card-text {
    grid-row: 3;
    margin: 0.75rem 0 1.25rem;
  }

  .card-button {
    grid-row: 4;
    margin-top: 0;
    align-self: start;
  }
}

The important line is grid-row: span 4. Without it, a card may occupy only one implicit row and have no four-track range to inherit. The parent’s 1fr row gives the description area flexible space, while the final auto row holds the buttons. Because all cards share those tracks, a taller title expands row 1 for the whole card row.

Use align-items: stretch if you want full-height cards. If you prefer buttons that only hug their labels, keep align-self: start on the button; the shared track still aligns the top of every button area.

CSS and JavaScript support detection

The CSS feature query for rows is straightforward:

@supports (grid-template-rows: subgrid) {
  /* row subgrid styles */
}

Do not assume that one subgrid check covers both dimensions. Columns and rows are separate properties. If an enhancement depends on both, query both:

@supports ((grid-template-rows: subgrid)
  and (grid-template-columns: subgrid)) {
  /* both row and column subgrid are usable */
}

This matters when a card or wrapper inherits column tracks from its parent and row tracks from another level. A browser can support one declared subgrid axis while your layout assumes both.

For logging or progressive enhancement in JavaScript, use the same property/value pairs through CSS.supports():

const supportsRowSubgrid = CSS.supports('grid-template-rows', 'subgrid');
const supportsColumnSubgrid = CSS.supports('grid-template-columns', 'subgrid');

document.documentElement.classList.toggle(
  'has-row-subgrid',
  supportsRowSubgrid
);

Prefer CSS @supports for styling. JavaScript is useful when you need to change behavior, display a small hint, or send analytics about which rendering path was chosen.

Flexbox fallback

The base styles above already provide the fallback, so the pattern is progressive enhancement rather than a separate stylesheet. The key fallback rules are:

.card {
  display: flex;
  flex-direction: column;
}

.card-text {
  flex: 1;
}

.card-button {
  margin-top: auto;
}

This gives older browsers a usable result: equal-height grid items, flexible body text, and bottom-aligned buttons. The subgrid block then removes the flex-specific spacing assumptions and maps each direct child into the shared tracks.

Keep margins intentional inside the enhancement layer. For example, the fallback relies on margin-top: auto, but subgrid does not need that rule on the button. Resetting it to 0 avoids unexpected extra space in the final row.

Browser notes

Test in the browsers your project actually supports. Subgrid ships in modern Chromium, Firefox, and Safari releases, but older browser versions may understand Grid without understanding subgrid. That is exactly why the base layout should remain valid on its own.

A few practical checks will save debugging time:

    <li>Confirm that each card spans the same number of parent rows as the parent defines for card content.</li> <li>Keep title, meta, text, and button as direct children of the subgrid card.</li> <li>Use grid-template-rows: subgrid for vertical card alignment; use column subgrid only when the child really needs to inherit column tracks.</li> <li>Inspect the fallback after writing the enhancement: @supports rules can override margins, gaps, and display modes.</li> </ul>

    With this structure, the card grid remains simple in HTML, clean in older browsers, and precisely aligned in browsers that support subgrid.