What Makes Subgrid Different
Traditional CSS Grid allows you to create layouts where child elements align to the parent grid's columns and rows. But when those children become grid containers themselves, their own children lose that connection. They create new, independent grids with their own track definitions. Without subgrid, maintaining alignment across these nested structures requires manual sizing, calculations, or JavaScript--all of which complicate maintenance and responsiveness.
Subgrid changes the game by allowing nested grids to inherit the track definitions (both sizes and names) from their parent grid container. When you declare grid-template-rows: subgrid or grid-template-columns: subgrid, the nested grid uses the exact same tracks as its parent, ensuring perfect alignment at any viewport size.
This capability is particularly valuable for modern web development projects that require pixel-perfect layouts across diverse content structures.
The Problem with Nested Grids
Why Alignment Breaks Without Subgrid
Consider a common scenario: a card-based layout where each card contains an image, title, description, and action button. Without subgrid, even if all cards sit on the same parent grid, their internal content can't share consistent row heights. One card's title might span two lines while another's uses just one, throwing off the alignment of buttons and descriptions below.
The same problem appears in form layouts, where labels and inputs need vertical alignment across multiple form groups. Or in pricing tables, where feature lists must align across tiers. Traditional solutions involve fixed heights, CSS custom properties with careful calculations, or accepting misaligned layouts.
For developers working on responsive web design, these alignment challenges become even more complex as content reflows across viewport sizes. Subgrid provides a declarative solution that works at any breakpoint without additional JavaScript.
Subgrid Syntax and Implementation
Basic Syntax
The subgrid value can be applied to grid-template-columns, grid-template-rows, or both. When you use subgrid, the nested grid inherits the track definitions from the parent in the specified dimension(s):
.parent-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto auto auto;
}
.child-grid {
display: grid;
grid-column: span 2;
grid-template-columns: subgrid;
grid-template-rows: repeat(2, auto);
}
In this example, the child grid uses the parent's column tracks while defining its own row structure. For full alignment across both dimensions, declare subgrid for both properties.
As noted in MDN's comprehensive documentation, subgrid allows nested grid containers to participate in their ancestor's grid layout, maintaining consistent alignment throughout the component hierarchy.
Real-World Use Cases
Card Layouts with Perfect Alignment
Card interfaces are perhaps the most common use case for subgrid. When product cards, blog post previews, or dashboard widgets need aligned content, subgrid provides the solution. Each card can maintain its own semantic structure while the titles, images, metadata, and action buttons all align across the entire grid.
Consider a portfolio layout where each project card contains an image, project title, client name, and view button. With subgrid, even when project descriptions vary significantly in length, the view buttons can still align perfectly--because they're placed on the same row track defined by the parent grid.
Form Layouts
Form alignment presents similar challenges. Labels, inputs, help text, and error messages all need consistent vertical rhythm across form sections. Subgrid allows you to wrap related fieldsets in grid containers while maintaining alignment across the entire form.
This approach is particularly valuable for custom web applications that require complex form interfaces with consistent styling across all input fields and labels.
1.form-section {2 display: grid;3 grid-template-columns: 1fr 2fr;4 grid-template-rows: subgrid;5 grid-row: span 4; /* Label, input, help text, error message */6}7 8/* Each form group inherits the parent form's row structure,9 ensuring consistent spacing and alignment */Advanced Subgrid Patterns
Nested Subgrids
Subgrid can be nested multiple levels deep, with each level inheriting from its direct parent. This allows complex component hierarchies while maintaining precise track alignment at each level. When nesting subgrids, you reference the direct parent's grid lines for placement, not the original top-level container.
As demonstrated in Prismic's implementation guide, this nested structure enables sophisticated layouts where alignment propagates through every level of the component tree.
Line Name Inheritance
One of subgrid's most powerful features is the ability to use parent grid line names when placing items in nested grids. When the parent grid defines named lines, those names are available throughout the subgrid hierarchy:
.parent {
display: grid;
grid-template-columns: [start] 1fr [middle] 1fr [end];
}
.grandchild {
grid-column: start / middle; /* Uses parent's line name */
}
Gap Inheritance
Subgrids inherit gap values from their parent grid by default, but this can be overridden at each level. This enables nuanced spacing systems while maintaining consistency where needed throughout your front-end architecture.
Subgrid Browser Support
4
Major Browsers
2023
Baseline Support
100%
CSS Solution
0
JavaScript Required
Best Practices and Common Pitfalls
Understanding Implicit vs Explicit Tracks
Subgrid only works with explicitly defined tracks. If your parent grid relies on implicit row creation (content flowing into auto-generated rows), subgrid cannot inherit those. Always define the tracks you want subgrid to use.
Line Number Scope
Inside a subgrid, line numbers restart from 1. This is intentional--it means components can be designed independently, knowing their internal line numbers will always be consistent regardless of where they're placed on the parent grid.
Developer Tools
Modern browser dev tools provide excellent subgrid visualization. Chrome and Firefox both highlight subgrid tracks, show inherited line names, and visualize the grid hierarchy. Use these tools to debug alignment issues.
Balancing Semantics and Structure
Subgrid allows you to use semantic HTML while achieving precise layouts. Don't flatten your DOM structure just to enable grid participation--wrap elements appropriately for accessibility and maintainability, then use subgrid to bridge those wrapper elements into the parent grid's alignment system.
For teams building scalable web platforms, this balance between semantic structure and visual precision is essential for long-term maintainability.
Frequently Asked Questions
Sources
-
MDN Web Docs - CSS Subgrid - Official documentation covering the subgrid specification, browser support, and fundamental concepts
-
Josh W. Comeau - Brand New Layouts with CSS Subgrid - Comprehensive tutorial with interactive examples and practical use cases
-
Prismic - CSS Subgrid Guide: Syntax, Examples & Best Practices - Practical implementation guide with CodePen demos