CSS Subgrid Tutorial: A Comprehensive Guide to Advanced Grid Layouts
CSS Grid is a powerful layout system that revolutionized web design, allowing developers to create complex, responsive layouts with ease. While CSS Grid provides robust tools for structuring layouts, the introduction of subgrid in CSS Grid Level 2 takes its capabilities to the next level. This tutorial will explain what CSS subgrid is, why it’s useful, how to implement it, and provide practical examples to help you master this feature. What is CSS Subgrid? CSS subgrid is a feature that allows a nested grid (a grid inside another grid) to inherit the track sizing (rows and columns) of its parent grid. This means that child elements within a nested grid can align perfectly with the parent grid’s tracks, creating consistent and harmonious layouts without the need for complex workarounds. Before subgrid, aligning nested grid items with the parent grid required manual coordination of column and row sizes, often leading to fragile or repetitive CSS. Subgrid simplifies this process by allowing nested grids to “borrow” the parent grid’s track definitions, ensuring alignment and consistency across the layout. Key Benefits of Subgrid Alignment Consistency: Nested grid items align seamlessly with the parent grid’s tracks. Simplified Code: Reduces the need for duplicate track definitions or manual sizing calculations. Responsive Design: Makes it easier to maintain consistent layouts across different screen sizes. Flexibility: Enables complex layouts with nested components that respect the parent grid’s structure. Browser Support As of June 2025, CSS subgrid is supported in all major modern browsers, including Chrome, Firefox, Edge, and Safari (since Safari 16). However, always check Can I Use for the latest browser compatibility, especially for older versions. For unsupported browsers, you can use fallbacks like regular CSS Grid or Flexbox. How Subgrid Works To use subgrid, you need a parent grid container and a nested grid container. The nested grid can then be set to use the subgrid value for its grid-template-columns or grid-template-rows (or both), allowing it to inherit the parent’s track sizing. Basic Syntax /* Parent grid */ .parent-grid { display: grid; grid-template-columns: 1fr 2fr 1fr; grid-template-rows: auto 1fr auto; } /* Nested grid using subgrid */ .nested-grid { display: grid; grid-template-columns: subgrid; /* Inherits parent’s column tracks */ grid-template-rows: subgrid; /* Inherits parent’s row tracks */ } The subgrid value tells the nested grid to use the parent grid’s column or row tracks instead of defining its own. This ensures that the nested grid’s items align with the parent grid’s structure. Step-by-Step Example: Building a Card Layout with Subgrid Let’s create a practical example to demonstrate subgrid: a card layout where each card contains a header, content, and footer that align with the parent grid’s columns. HTML Structure Header Content Footer Header Content Footer CSS with Subgrid /* Parent grid */ .parent-grid { display: grid; grid-template-columns: 1fr 2fr 1fr; gap: 20px; padding: 20px; } /* Card as a nested grid */ .card { display: grid; grid-template-columns: subgrid; /* Inherit parent’s columns */ grid-column: span 3; /* Card spans all 3 columns */ gap: 10px; background: #f9f9f9; border: 1px solid #ddd; padding: 10px; } /* Card sections */ .card-header, .card-content, .card-footer { grid-column: 1 / -1; /* Span all columns within the card */ padding: 10px; } .card-header { background: #007bff; color: white; } .card-content { background: #e9ecef; } .card-footer { background: #28a745; color: white; } Explanation Parent Grid: The .parent-grid defines a grid with three columns (1fr 2fr 1fr) and a gap of 20px. Nested Grid: Each .card is a nested grid that uses grid-template-columns: subgrid to inherit the parent’s column tracks. The grid-column: span 3 ensures the card spans all three columns of the parent grid. Card Sections: The .card-header, .card-content, and .card-footer span all columns within the card’s subgrid using grid-column: 1 / -1. Styling: Basic styling is applied to differentiate the card sections visually. This setup ensures that the card’s internal elements align with the parent grid’s columns, creating a clean and consistent layout. Advanced Use Case: Nested Form Layout Subgrid is particularly useful for aligning form elements across multiple sections. Let’s create a form with labeled inputs that align perfectly using subgrid. HTML Structure Name Email Address Phone CSS with Subgrid /* Parent grid */ .form-grid { display: grid; grid-template-columns: minmax(100px, 1fr) 2fr; gap: 15px; padding: 20px; max-width: 600px; mar

CSS Grid is a powerful layout system that revolutionized web design, allowing developers to create complex, responsive layouts with ease. While CSS Grid provides robust tools for structuring layouts, the introduction of subgrid in CSS Grid Level 2 takes its capabilities to the next level. This tutorial will explain what CSS subgrid is, why it’s useful, how to implement it, and provide practical examples to help you master this feature.
What is CSS Subgrid?
CSS subgrid is a feature that allows a nested grid (a grid inside another grid) to inherit the track sizing (rows and columns) of its parent grid. This means that child elements within a nested grid can align perfectly with the parent grid’s tracks, creating consistent and harmonious layouts without the need for complex workarounds.
Before subgrid, aligning nested grid items with the parent grid required manual coordination of column and row sizes, often leading to fragile or repetitive CSS. Subgrid simplifies this process by allowing nested grids to “borrow” the parent grid’s track definitions, ensuring alignment and consistency across the layout.
Key Benefits of Subgrid
- Alignment Consistency: Nested grid items align seamlessly with the parent grid’s tracks.
- Simplified Code: Reduces the need for duplicate track definitions or manual sizing calculations.
- Responsive Design: Makes it easier to maintain consistent layouts across different screen sizes.
- Flexibility: Enables complex layouts with nested components that respect the parent grid’s structure.
Browser Support
As of June 2025, CSS subgrid is supported in all major modern browsers, including Chrome, Firefox, Edge, and Safari (since Safari 16). However, always check Can I Use for the latest browser compatibility, especially for older versions. For unsupported browsers, you can use fallbacks like regular CSS Grid or Flexbox.
How Subgrid Works
To use subgrid, you need a parent grid container and a nested grid container. The nested grid can then be set to use the subgrid
value for its grid-template-columns
or grid-template-rows
(or both), allowing it to inherit the parent’s track sizing.
Basic Syntax
/* Parent grid */
.parent-grid {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: auto 1fr auto;
}
/* Nested grid using subgrid */
.nested-grid {
display: grid;
grid-template-columns: subgrid; /* Inherits parent’s column tracks */
grid-template-rows: subgrid; /* Inherits parent’s row tracks */
}
The subgrid
value tells the nested grid to use the parent grid’s column or row tracks instead of defining its own. This ensures that the nested grid’s items align with the parent grid’s structure.
Step-by-Step Example: Building a Card Layout with Subgrid
Let’s create a practical example to demonstrate subgrid: a card layout where each card contains a header, content, and footer that align with the parent grid’s columns.
HTML Structure
class="parent-grid">
class="card">
class="card-header">Header
class="card-content">Content
class="card-footer">Footer
class="card">
class="card-header">Header
class="card-content">Content
class="card-footer">Footer
CSS with Subgrid
/* Parent grid */
.parent-grid {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
gap: 20px;
padding: 20px;
}
/* Card as a nested grid */
.card {
display: grid;
grid-template-columns: subgrid; /* Inherit parent’s columns */
grid-column: span 3; /* Card spans all 3 columns */
gap: 10px;
background: #f9f9f9;
border: 1px solid #ddd;
padding: 10px;
}
/* Card sections */
.card-header,
.card-content,
.card-footer {
grid-column: 1 / -1; /* Span all columns within the card */
padding: 10px;
}
.card-header {
background: #007bff;
color: white;
}
.card-content {
background: #e9ecef;
}
.card-footer {
background: #28a745;
color: white;
}
Explanation
-
Parent Grid: The
.parent-grid
defines a grid with three columns (1fr 2fr 1fr
) and a gap of 20px. -
Nested Grid: Each
.card
is a nested grid that usesgrid-template-columns: subgrid
to inherit the parent’s column tracks. Thegrid-column: span 3
ensures the card spans all three columns of the parent grid. -
Card Sections: The
.card-header
,.card-content
, and.card-footer
span all columns within the card’s subgrid usinggrid-column: 1 / -1
. - Styling: Basic styling is applied to differentiate the card sections visually.
This setup ensures that the card’s internal elements align with the parent grid’s columns, creating a clean and consistent layout.
Advanced Use Case: Nested Form Layout
Subgrid is particularly useful for aligning form elements across multiple sections. Let’s create a form with labeled inputs that align perfectly using subgrid.
HTML Structure
class="form-grid">
class="form-section">
Name
type="text" placeholder="Enter name">
Email
type="email" placeholder="Enter email">
class="form-section">
Address
type="text" placeholder="Enter address">
Phone
type="tel" placeholder="Enter phone">
CSS with Subgrid
/* Parent grid */
.form-grid {
display: grid;
grid-template-columns: minmax(100px, 1fr) 2fr;
gap: 15px;
padding: 20px;
max-width: 600px;
margin: 0 auto;
}
/* Form section as a nested grid */
.form-section {
display: grid;
grid-template-columns: subgrid; /* Inherit parent’s columns */
grid-column: 1 / -1; /* Span all columns */
gap: 10px;
}
/* Form elements */
label {
grid-column: 1; /* First column for labels */
font-weight: bold;
}
input {
grid-column: 2; /* Second column for inputs */
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}
Explanation
-
Parent Grid: The
.form-grid
defines a two-column layout: a label column (minmax(100px, 1fr)
) and an input column (2fr
). -
Nested Grid: Each
.form-section
usesgrid-template-columns: subgrid
to inherit the parent’s column tracks. It spans both columns withgrid-column: 1 / -1
. -
Form Elements: Labels are placed in the first column (
grid-column: 1
), and inputs in the second (grid-column: 2
), ensuring perfect alignment across sections. -
Responsive Design: The
minmax
function ensures the label column doesn’t shrink below 100px, maintaining readability.
This layout ensures that all labels and inputs align consistently, even across different form sections.
Tips for Using Subgrid Effectively
- Use Subgrid Sparingly: Subgrid is powerful but not always necessary. For simple layouts, regular CSS Grid or Flexbox may suffice.
-
Combine with Other Grid Features: Use subgrid alongside
auto-fit
,minmax
, or named grid lines for more dynamic layouts. -
Fallbacks for Older Browsers: Use feature queries (
@supports
) to provide fallbacks:
@supports not (grid-template-columns: subgrid) {
.nested-grid {
grid-template-columns: 1fr 2fr 1fr; /* Fallback */
}
}
-
Test Responsiveness: Ensure subgrid layouts adapt well to different screen sizes using media queries or responsive units like
fr
andvw
. - Debugging: Use browser DevTools to inspect the grid lines and ensure subgrid tracks align as expected.
Common Pitfalls and Solutions
-
Pitfall: Subgrid doesn’t work if the parent isn’t a grid container.
-
Solution: Ensure the parent element has
display: grid
.
-
Solution: Ensure the parent element has
-
Pitfall: Nested grid items don’t align if
grid-column
orgrid-row
spans are incorrect.- Solution: Double-check span values and ensure the nested grid spans the intended tracks.
-
Pitfall: Subgrid ignores parent’s
gap
property.-
Solution: Explicitly set
gap
on the nested grid if needed.
-
Solution: Explicitly set
Conclusion
CSS subgrid is a game-changer for creating complex, aligned layouts with minimal effort. By allowing nested grids to inherit their parent’s track sizing, subgrid eliminates the need for manual coordination and ensures consistency across your design. Whether you’re building card layouts, forms, or intricate dashboards, subgrid empowers you to create robust, responsive layouts that are both maintainable and visually appealing.
To deepen your understanding, experiment with the examples provided, combine subgrid with other CSS Grid features, and explore real-world use cases. With practice, subgrid will become an indispensable tool in your web design toolkit.
For further learning, check out the MDN Web Docs on Subgrid and try building your own layouts to see subgrid in action.