DEV Community

Nick Benksim
Nick Benksim

Posted on • Originally published at csscodelab.com

Mastering CSS Grid Subgrid: A Complete Guide

Mastering CSS Grid Subgrid: A Complete Guide

Grab your coffee, pull up a chair, and let's talk about one of the most satisfying CSS features to finally hit prime-time browser support: CSS Grid Subgrid.

You know the exact headache I’m talking about. You’re building a classic card layout. Each card has a thumbnail, a title, a short description, and a "Read More" button at the bottom. The designer wants all the titles to align perfectly across the row, all the descriptions to align, and all the buttons to sit exactly on the same baseline at the bottom. It looks gorgeous in Figma when every title is exactly one line. But then real-world content hits the staging server. One card has a three-line title, another has a two-word title, and suddenly your beautiful grid looks like a jagged, staggered mess.

Subgrid solves this exact, frustrating problem once and for all by letting nested elements "listen" directly to the parent grid.

How we suffered before

Before subgrid became widely supported, aligning child elements across sibling containers was an absolute nightmare. We had to resort to a laundry list of ugly workarounds that made our clean markup cry.

First, we tried hardcoding heights. We would throw a min-height or a fixed height on the card titles. This worked okay until someone resized the browser, changed the font size, or localized the site into German. Suddenly, the text would overflow, overlap, or look completely broken. If you've ever had to handle responsiveness issues related to sizing hacks, you might appreciate our guide on utilizing mathematical functions in CSS to handle sizing dynamically, but even mathematical functions couldn't save us from the inherent disconnect of isolated card containers.

Then came the JavaScript rescue mission. We would write helper scripts to calculate the height of every single title card on the row, find the tallest one, and apply that height to all of them inline. It worked, but it was bad for performance, caused layout shifts, and felt like a massive over-engineering feat for a simple layout task.

Lastly, some of us just flattened the HTML structure. We would put all the titles in one row, all the descriptions in another, and all the buttons in a third row inside the main grid. Sure, it aligned perfectly, but it completely destroyed semantic HTML, killed screen-reader accessibility, and made DOM manipulation a nightmare.

The modern way in 2026

With CSS Grid Subgrid, those hacky days are officially over. Now, a child element can opt into its parent’s grid tracks. Instead of creating an isolated grid inside each card, the card itself says: "Hey, I'm going to span three rows of the parent grid, and my children are going to align themselves directly to those rows."

To implement this, you set up your main parent grid as usual. Then, you make the card container span multiple grid rows. Finally, you apply grid-template-rows: subgrid to the card. Now, the card’s children (title, description, button) automatically inherit and snap to the exact row heights defined by the master grid across the entire row. This is the ultimate evolution of grid layouts, building beautifully on the concepts of creating complex magazine layouts with standard grids.

Ready-to-use code snippet

Here is a clean, minimal, and fully functional implementation of a card deck using CSS Grid Subgrid. Notice how the card container spans three rows, and its children automatically align perfectly across all cards, regardless of content length.

<!-- HTML Structure -->
<div class="grid-container">
  <!-- Card 1 -->
  <article class="card">
    <h3>Short Title</h3>
    <p>A quick description goes here.</p>
    <button>Read More</button>
  </article>

  <!-- Card 2 -->
  <article class="card">
    <h3>This is a Super Long Title That Spans Multiple Lines Automatically</h3>
    <p>This description has slightly more content to show off how the card content adjusts perfectly.</p>
    <button>Read More</button>
  </article>

  <!-- Card 3 -->
  <article class="card">
    <h3>Medium Title</h3>
    <p>Another short description.</p>
    <button>Read More</button>
  </article>
</div>

<style>
/* The Parent Grid */
.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  /* Each card spans a block of 3 rows: title, paragraph, button */
  grid-template-rows: repeat(auto-fill, auto auto auto);
  gap: 30px 20px;
  padding: 20px;
  background-color: #f4f4f9;
}

/* The Grid Item (Subgrid Container) */
.card {
  display: grid;
  /* Tell the card to span exactly 3 rows of the parent grid */
  grid-row: span 3;
  /* Magic begins here: inherit row definitions from the parent grid */
  grid-template-rows: subgrid;
  
  background: #ffffff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}

.card h3 {
  margin: 0;
  font-family: sans-serif;
  color: #333;
}

.card p {
  margin: 0;
  font-family: sans-serif;
  color: #666;
  line-height: 1.5;
}

.card button {
  align-self: start;
  justify-self: start;
  padding: 10px 16px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: bold;
}
</style>

Common beginner mistake

The single most common mistake developers make when adopting subgrid is forgetting to define the track span on the subgrid container itself.

If you set grid-template-rows: subgrid on a card, but you don't explicitly tell that card how many tracks of the parent grid it is supposed to span (for example, using grid-row: span 3), the browser won't know how many rows to reserve for the child elements. By default, it will fall back to spanning just 1 row, causing all your internal card elements (the title, description, and button) to overlap, stack weirdly, or collapse into a single row track.

Always remember the golden rule of subgrid: the container holding the subgrid must explicitly define its track span (e.g., grid-row: span [number of children] or grid-column: span [number of columns]) so the layout engine knows exactly which parent slices to hand down to the nested elements.

🔥 We publish more advanced CSS tricks, ready-to-use snippets, and tutorials in our Telegram channel. Subscribe so you don't miss out!

Top comments (0)