DEV Community

Nick Benksim
Nick Benksim

Posted on • Originally published at csscodelab.com

CSS Subgrid: why we waited so long for this and how to use it

CSS Subgrid: Why We Waited So Long and How to Use It

Grab a cup of coffee and pull up a chair. Today, we are talking about a CSS feature that felt like a myth for years, a true unicorn of frontend development. Yes, I am talking about CSS Subgrid. If you have ever designed a card-based layout, a product catalog, or a complex magazine dashboard and wanted the internal elements of those cards—like headers, images, and footers—to align perfectly with each other across different columns, you know the exact pain we are solving today.

For a long time, the web layout system had a massive blind spot: nested grids were completely blind to the parent grid's tracks. Subgrid changes this completely by letting child elements inherit and align directly to the columns or rows of their parent. Let's dive into why this is a game-changer, how we used to suffer, and how to write clean, modern CSS to solve this once and for all.

How We Suffered Before (The Dark Age of CSS Hacks)

Remember how we used to align card footers before Subgrid? It was a wild west of brittle workarounds and dirty code. We had a few go-to techniques, and none of them were pretty:

  • The Min-Height Guessing Game: We would hardcode a min-height on the card headers and description paragraphs. If a content manager added one extra word to a title, the whole layout broke, text overlapped, or things looked completely uneven.
  • The Flexbox Push: We used to set display: flex and flex-direction: column on the cards, then put margin-top: auto on the footer. Sure, the footer pushed to the bottom of the card, but the headers and text blocks in neighboring cards still didn’t line up horizontally. It looked incredibly sloppy.
  • The JavaScript Height-Equalizer: The ultimate sin. We wrote JS scripts to calculate the heights of all sibling elements on resize, finding the tallest one and applying that height to the rest. It was terrible for performance, caused layout shifts, and felt like a complete hack.

We already had powerful tools, as we discussed in our article on Advanced CSS Grid: Creating Complex Magazine Layouts, but even the strongest grid layout hit a hard wall when it came to deeply nested components. The child elements simply lived in their own independent layout contexts.

The Modern Way in 2026: Hello, Subgrid!

In 2026, CSS Subgrid is globally supported and fully production-ready. The concept is beautifully simple: instead of defining a new, independent grid inside a grid item, you tell the browser to use the parent's grid tracks.

By declaring grid-template-rows: subgrid (or columns), the nested grid elements align themselves precisely to the parent grid lines. If one card's description gets longer and expands its row, the corresponding rows in all other cards in that row expand to match. No JavaScript, no hardcoded heights, no hacks. Just pure, declarative browser magic.

Ready-to-Use Code Snippet

Let's look at a practical, clean example. Here is a classic three-column layout where each card has a header, a body paragraph, and a footer. Thanks to Subgrid, all three sections align perfectly across all cards, regardless of content length.

<!-- HTML Structure -->
<div class="container">
  <!-- Card 1 -->
  <div class="card">
    <h3>Short Title</h3>
    <p>This is a short description.</p>
    <footer>Footer 1</footer>
  </div>

  <!-- Card 2 -->
  <div class="card">
    <h3>This Is A Way Longer Card Title That Spans Multiple Lines</h3>
    <p>This card has a bit more content. It needs more space to breathe.</p>
    <footer>Footer 2</footer>
  </div>

  <!-- Card 3 -->
  <div class="card">
    <h3>Medium Title</h3>
    <p>Standard description goes here.</p>
    <footer>Footer 3</footer>
  </div>
</div>

<style>
/* Parent Grid */
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

/* 
  The Magic: Each card spans 3 rows of the parent grid, 
  and passes those rows down to its children.
*/
.card {
  display: grid;
  grid-row: span 3;
  grid-template-rows: subgrid;
  background-color: #f4f4f9;
  padding: 15px;
  border-radius: 8px;
}

.card h3 {
  margin: 0;
  background-color: #e0e0f8;
  padding: 10px;
}

.card p {
  margin: 0;
  padding: 10px 0;
}

.card footer {
  background-color: #333;
  color: #fff;
  padding: 10px;
  border-radius: 4px;
}
</style>

Common Beginner Mistake

When developers first try to implement Subgrid, they almost always run into the exact same issue: forgetting that a subgrid is still a grid.

You cannot just write grid-template-rows: subgrid and expect it to work out of the box. You must explicitly set display: grid (or display: inline-grid) on the subgridded element (the .card in our example). If you miss this, the browser will ignore your subgrid values, and you will find yourself staring at a broken layout, scratching your head.

Another common slip-up is forgetting to tell the card how many rows it should span. Because your subgrid needs to know which parent rows to occupy, you must use grid-row: span X (where X is the number of child levels inside your card). If you have a header, content, and footer, your card needs to span exactly 3 rows.

If you ever find yourself struggling to figure out why your subgrid tracks aren't aligning correctly, don't worry. You can easily visualize these tracks by checking out our guide on How to Debug CSS Grid and Flexbox in Developer Tools. Turn on the grid overlay, and you will see exactly how your subgrids align with the parent tracks!

🔥 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)