π What is subgrid?
subgrid is an advanced CSS Grid feature that allows a child grid to inherit the row and/or column tracks from its parent grid. This creates consistent alignment between nested components without manually duplicating sizes.
Note
π― Why Do We Need subgrid?
- To align nested elements exactly with the parent grid
- To avoid repeating the same grid-template definitions in child components
- To keep complex layouts consistent across UI sections
- To allow children to stretch across inherited lines
π Syntax
Basic Syntax
.child {
display: grid;
grid-template-columns: subgrid;
grid-template-rows: subgrid;
}You can use subgrid for columns, rows, or both.
π§ͺ Example: Subgrid for Columns
Column Subgrid Example
.parent {
display: grid;
grid-template-columns: 200px 1fr 1fr;
gap: 20px;
}
.child {
display: grid;
grid-column: 1 / -1; /* span full width */
grid-template-columns: subgrid; /* inherit parent columns */
}The child gridβs columns will match the parentβs column sizing exactly.
π Visual Representation

π₯ Example: Subgrid for Rows
Row Subgrid Example
.parent {
display: grid;
grid-template-rows: 100px auto 80px;
row-gap: 15px;
}
.child {
display: grid;
grid-template-rows: subgrid; /* inherit row tracks */
grid-row: 2; /* placed in the second parent row */
}Inner elements align perfectly with the parent's row structure.
π§ How subgrid Works
| Feature | Behavior |
|---|---|
| Track Sizes | Inherited from parent |
| Gaps | Inherited automatically |
| Child Placement | Uses parent's grid lines |
| Child Content | Still placed inside subgrid tracks |
ποΈ Practical Example β Blog Layout
A parent grid defines global structure, while subgrids align child sections such as posts.
Blog Example
.layout {
display: grid;
grid-template-columns: 250px 1fr 300px;
gap: 20px;
}
.post {
display: grid;
grid-template-columns: subgrid; /* align post content with main grid */
grid-column: 2; /* middle area */
}Every blog post aligns perfectly with the site layout, without redefining column widths.
π± Responsive Example
Responsive Subgrid
.wrapper {
display: grid;
grid-template-columns: 1fr;
}
.section {
display: grid;
grid-template-columns: subgrid;
}
@media (min-width: 900px) {
.wrapper {
grid-template-columns: 200px 1fr 200px;
}
}Subgrid adapts automatically when the parent changes track sizes.
β οΈ Common Mistakes
- Expecting subgrid to work without the parent having explicit track definitions
- Trying to use subgrid on non-grid containers (won't work)
- Forgetting that Safari requires enabling experimental features
- Expecting child elements to ignore parent gaps (they inherit them!)
Note
β track sizes
β gaps
β line positions
β not the parentβs auto-placement of items
π subgrid vs Nested Grid
| Feature | Nested Grid | Subgrid |
|---|---|---|
| Alignment with parent | Independent β misalignments possible | Perfectly aligned |
| Track duplication | Required | Not required |
| Complex UI | Harder to maintain | Easier & consistent |
π₯ Summary
subgrid is a powerful tool for creating consistent, scalable, and maintainable grid layouts. It shines in complex UIs where nested components must align perfectly across the page.
Learn more βMDN Docs π