惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
B
Blog RSS Feed
C
Check Point Blog
D
Docker
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
量子位
有赞技术团队
有赞技术团队
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
M
MIT News - Artificial intelligence
B
Blog
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
月光博客
月光博客

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Mastering CSS Grid Subgrid: A Complete Guide
Nick Benksim · 2026-05-28 · via DEV Community

Nick Benksim

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!