CSS Subgrid is a powerful feature that extends CSS Grid Layout by allowing child grids to inherit the track sizing and positioning from their parent grid. This creates seamless alignment between nested grid containers, solving one of the most challenging aspects of complex grid layouts.
What is CSS Subgrid?
CSS Subgrid enables a grid item to become a grid container that inherits the grid tracks (rows or columns) from its parent grid. Instead of defining its own independent grid tracks, a subgrid aligns its items with the parent grid’s lines, creating perfect alignment across multiple levels of nesting.
Key Benefits:
- Perfect alignment between parent and child grid items
- Consistent spacing across nested layouts
- Simplified responsive design patterns
- Better content organization in complex layouts
Basic Subgrid Syntax
The subgrid feature is activated using the subgrid value for the grid-template-columns or grid-template-rows properties:
.parent-grid {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: repeat(3, 100px);
gap: 20px;
}
.child-subgrid {
display: grid;
grid-column: 1 / -1; /* Span entire parent width */
grid-template-columns: subgrid; /* Inherit parent columns */
grid-template-rows: subgrid; /* Inherit parent rows */
}
Visual Example: Basic Subgrid Implementation
Notice how the subgrid items (red) align perfectly with the parent grid columns (blue)
Subgrid Direction Options
You can inherit tracks in one or both dimensions:
Column-only Subgrid
.subgrid-columns {
grid-template-columns: subgrid; /* Inherit parent columns */
grid-template-rows: repeat(2, 60px); /* Define own rows */
}
Row-only Subgrid
.subgrid-rows {
grid-template-columns: repeat(3, 1fr); /* Define own columns */
grid-template-rows: subgrid; /* Inherit parent rows */
}
Full Subgrid
.full-subgrid {
grid-template-columns: subgrid; /* Inherit parent columns */
grid-template-rows: subgrid; /* Inherit parent rows */
}
Practical Example: Card Layout with Subgrid
Product Title
This is a short description of the product that fits nicely.
Another Product
A longer description that demonstrates how subgrid helps maintain consistent alignment even with varying content lengths across different cards.
Third Item
Medium length description here.
Notice how all card elements align perfectly despite different content lengths
Interactive Subgrid Demo
Click the buttons above to see different subgrid configurations in action
Advanced Subgrid Patterns
Named Grid Lines Inheritance
Subgrids inherit named grid lines from their parent, making it easier to position items using semantic names:
.parent {
display: grid;
grid-template-columns: [start] 1fr [middle] 2fr [end];
grid-template-rows: [header] 100px [content] 1fr [footer] 50px;
}
.subgrid-child {
display: grid;
grid-template-columns: subgrid;
grid-template-rows: subgrid;
grid-column: start / end;
grid-row: content / footer;
}
.nested-item {
grid-column: start / middle; /* Uses inherited line names */
grid-row: header; /* Uses inherited line names */
}
Gap Inheritance
Subgrids inherit the gap values from their parent grid, but you can override them:
.parent-with-gap {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 20px;
}
.subgrid-custom-gap {
display: grid;
grid-template-columns: subgrid;
grid-column: 1 / -1;
gap: 10px; /* Override parent gap */
}
Browser Support and Fallbacks
Browser Support Status:
- Firefox: Full support since version 71
- Safari: Support since version 16
- Chrome/Edge: Support since version 117 (September 2023)
Feature Detection and Fallbacks
/* Feature detection */
@supports (grid-template-columns: subgrid) {
.modern-subgrid {
display: grid;
grid-template-columns: subgrid;
}
}
/* Fallback for browsers without subgrid support */
@supports not (grid-template-columns: subgrid) {
.fallback-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
}
Common Use Cases
1. Form Layouts
Create consistent form layouts where labels and inputs align perfectly across different sections:
.form-container {
display: grid;
grid-template-columns: max-content 1fr;
gap: 10px 20px;
}
.form-section {
display: grid;
grid-template-columns: subgrid;
grid-column: 1 / -1;
gap: inherit;
}
2. Card Grids
Ensure card elements align across rows, regardless of content length variations.
3. Complex Page Layouts
Create sophisticated page layouts where nested components maintain alignment with the main grid structure.
Performance Considerations
CSS Subgrid is performant because it doesn’t create additional layout calculations. Instead, it references the parent grid’s already computed track sizes. This makes it more efficient than attempting to manually synchronize multiple independent grids.
Best Practices:
- Use subgrid when you need perfect alignment between nested grid containers
- Consider fallback strategies for older browsers
- Test your layouts across different screen sizes
- Use named grid lines for better maintainability
Troubleshooting Common Issues
Issue: Subgrid Not Working
Solution: Ensure the parent element has display: grid and the child spans across the desired tracks using grid-column or grid-row.
Issue: Unexpected Spacing
Solution: Remember that subgrids inherit gap values. Override with specific gap values if needed.
Issue: Browser Compatibility
Solution: Use @supports queries to provide fallbacks for browsers without subgrid support.
Future of CSS Subgrid
With increasing browser support, CSS Subgrid is becoming a standard tool for complex layout design. It simplifies many layout challenges that previously required complex workarounds or JavaScript solutions. As adoption grows, we can expect to see more sophisticated design patterns and frameworks incorporating subgrid capabilities.
CSS Subgrid represents a significant advancement in web layout capabilities, offering developers the precision and control needed for complex, nested grid layouts while maintaining clean, semantic HTML structures.
- What is CSS Subgrid?
- Basic Subgrid Syntax
- Visual Example: Basic Subgrid Implementation
- Subgrid Direction Options
- Practical Example: Card Layout with Subgrid
- Interactive Subgrid Demo
- Advanced Subgrid Patterns
- Browser Support and Fallbacks
- Common Use Cases
- Performance Considerations
- Troubleshooting Common Issues
- Future of CSS Subgrid








