- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Subgrid
CSS Grid
CSS Subgrid
Align nested content to tracks defined by an ancestor grid without copying track sizes. It demonstrates sharing ancestor row or column tracks so nested cards, forms, and aligned content follow one grid definition.
Three cards side by side, each with a title, some text and a button. Their titles are different lengths, so the buttons end up at different heights. Subgrid fixes that: the cards share the parent grid's rows, so every title, text and button lines up across all of them.
A normal grid inside a grid sizes its own tracks and knows nothing about its neighbors. With subgrid, the inner grid borrows the tracks of the outer one. It builds on CSS Grid Container and Grid Placement.
Cards that line up
Example
css
.cards {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 1rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}Each card spans three rows of the outer grid, then says grid-template-rows: subgrid, which means "use those three rows as mine". The title row becomes as tall as the tallest title in that row of cards, and the same for the text and the button. Everything lines up, whatever the content.
One axis or both
Example
css
.nested {
display: grid;
grid-template-columns: subgrid;
grid-template-rows: auto 1fr auto;
}Subgrid can apply to columns, rows or both. Here the columns come from the parent, while the rows are the nested grid's own. The subgrid also inherits the parent's line names, and it can set its own gap.
Subgrid Syntax
| Declaration | Purpose | Example |
|---|---|---|
| grid-template-columns: subgrid | Uses the parent grid's column tracks. | grid-template-columns: subgrid |
| grid-template-rows: subgrid | Uses the parent grid's row tracks. | grid-template-rows: subgrid |
| gap | Overrides the inherited subgrid gap when needed. | gap: 0.5rem |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS Subgrid example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
.cards {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 1rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
}
.nested {
display: grid;
grid-template-columns: subgrid;
grid-template-rows: auto 1fr auto;
}
</style>
</head>
<body>
<main class="cards"><article class="card"><h2>Short title</h2><p>A longer summary that takes more room.</p><button>Open</button></article><article class="card"><h2>A title that wraps over two lines</h2><p>Short summary.</p><button>Open</button></article><article class="card"><h2>Third title</h2><p>Another summary.</p><button>Open</button></article></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| subgrid | 117 | 117 | 71 | 16 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
When you need it
- Rows of cards whose parts should align.
- Forms where labels and fields in different groups should share one column width.
- Page layouts where a nested section must stick to the main content column.
If the inner layout does not need to line up with anything outside it, a normal nested grid is simpler.
CSS Subgrid exercises
A grid in a grid
Make the card a grid too.
The HTML your code works on
<style>
.grid { display: grid; grid-template-columns: 100px 200px 300px; grid-template-rows: 50px 60px; }
</style>
<div class="grid">
<div class="card">
<span>A</span>
<span>B</span>
<span>C</span>
</div>
</div>Result
