π What is a Nested Grid?
A nested grid is when a grid item itself becomes a grid container. This allows you to build complex layouts by placing grids inside other grids. Each nested grid behaves independently unless you choose to connect it using subgrid.
π― Why Use Nested Grids?
- To organize complex UI components (cards, dashboards, forms)
- To structure content inside a grid item (headers, sidebars, thumbnails)
- Create modular, reusable components with inner layouts
- Achieve precise control that flexbox or a single grid alone can't handle
π Basic Concept
1οΈβ£ Parent element β display: grid;
2οΈβ£ Child grid item β also becomes display: grid;
Basic Nested Grid
.parent {
display: grid;
grid-template-columns: 1fr 1fr;
}
.child {
display: grid;
grid-template-columns: repeat(2, 1fr);
}π Visual Structure

π§ͺ Example: Card Component With Nested Grid
Card Layout Using Nested Grid
.cards {
display: grid;
gap: 20px;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
}
.card {
display: grid;
grid-template-rows: 150px auto 50px;
}
.card-header {
background: #ccc;
}
.card-body {
padding: 15px;
}
.card-footer {
background: #eee;
}Each card is a nested grid with its own rows. The parent controls card placement; the card controls internal content structure.
π₯ Example: Dashboard With Multiple Nested Grids
Dashboard Layout
.dashboard {
display: grid;
grid-template-columns: 2fr 1fr;
gap: 20px;
}
.stats,
.activity {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.stat-box,
.activity-item {
background: #f1f1f1;
padding: 10px;
}Both .stats and .activity are nested grids within the dashboard layout.
π§± Nested Grid vs Subgrid
| Feature | Nested Grid | Subgrid |
|---|---|---|
| Control | Independent tracks inside each grid | Inherits parent grid tracks |
| Alignment | Can misalign with parent | Perfect alignment with parent |
| Use Case | Modular layout components | Consistent alignment across nested children |
Note
π± Responsive Nested Grid Example
Responsive Nested Grid
.profile {
display: grid;
grid-template-columns: 1fr;
}
.profile-info {
display: grid;
grid-template-columns: 1fr 2fr;
}
@media (max-width: 600px) {
.profile-info {
grid-template-columns: 1fr;
}
}Each grid controls its own responsive behavior while still living inside a parent grid.
β οΈ Common Mistakes
- Not defining display: grid on nested grid containers
- Expecting nested items to align with parent grid lines (use subgrid instead)
- Overusing nested grids for layouts that could be simpler
- Mixing grid and flexbox unnecessarily inside nested structures
Note
If you need structured placement β grid.
If you need directional flow β flexbox.
π₯ Summary
Nested grids allow you to build multi-layered, modular, and powerful layouts in CSS Grid. Use them to structure components internally while keeping the parent layout clean and flexible.
Learn more βMDN Docs π