Flexbox lays things out in one direction. Grid controls rows and columns together, which makes page layouts and card grids much simpler: you describe the tracks on the container, and items fall into place.
The basics
.grid {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: auto;
gap: 16px;
}
This creates three columns: a fixed 200px column and two columns sharing the remaining space equally. Items fill cells left to right, top to bottom, creating new rows as needed.
The fr unit
fr is a fraction of the free space after fixed sizes and gaps are subtracted:
grid-template-columns: 1fr 2fr; /* one third and two thirds */
grid-template-columns: 240px 1fr; /* fixed sidebar, flexible content */
grid-template-columns: repeat(4, 1fr); /* four equal columns */
A 1fr track has an automatic minimum of its content size, so very long content can still make it wider. Use minmax(0, 1fr) to force truly equal columns.
repeat and minmax
grid-template-columns: repeat(3, minmax(0, 1fr));
grid-template-rows: repeat(2, minmax(120px, auto));
minmax(min, max) sets a size range: rows here are at least 120px and grow with their content.
Responsive cards without media queries
The most useful grid pattern:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
gap: 24px;
}
The browser fits as many columns of at least 240px as the container allows, then stretches them to fill the row. On a phone that's one column, on a desktop four or five. min(100%, 240px) prevents overflow on screens narrower than 240px.
auto-fit vs auto-fill
With many items, both behave the same. With only two items in a wide container:
auto-fitcollapses the empty tracks, so the two cards stretch across the full width.auto-fillkeeps empty tracks, so the two cards stay at their minimum width on the left.
Use auto-fit when items should fill the row, auto-fill when they should keep a consistent size.
Spanning cells
Items can span several columns or rows:
.featured {
grid-column: span 2;
grid-row: span 2;
}
Or be placed by line numbers. Lines are numbered from 1, and negative numbers count from the end:
.header { grid-column: 1 / -1; } /* from the first line to the last: full width */
.aside { grid-column: 1 / 2; grid-row: 2 / 4; }
When spanning items leave holes, grid-auto-flow: dense fills them with later items, which suits galleries where order doesn't matter.
Named areas
For page layouts, named areas make the CSS read like a picture of the layout:
.page {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
}
.page > header { grid-area: header; }
.page > nav { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }
@media (max-width: 720px) {
.page {
grid-template-columns: 1fr;
grid-template-areas: "header" "main" "sidebar" "footer";
}
}
Changing the layout on small screens only changes the template; the items keep their area names. A . in the template leaves a cell empty.
Alignment
| Property | Aligns | Axis |
|---|---|---|
justify-items |
Items inside their cells | Inline (row) |
align-items |
Items inside their cells | Block (column) |
justify-content |
The whole grid inside the container | Inline |
align-content |
The whole grid inside the container | Block |
justify-self, align-self |
One item | Either |
place-items |
Shorthand for align-items and justify-items |
Both |
.center-everything { display: grid; place-items: center; }
Subgrid
Cards in a row often need their titles, text and buttons aligned across cards, even with different content lengths. subgrid, supported in all current browsers, lets a card use the parent's row tracks:
.cards { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: auto auto 1fr auto; gap: 16px; }
.card { display: grid; grid-row: span 4; grid-template-rows: subgrid; }
Common mistakes
- Using
1frfor equal columns with long content. Useminmax(0, 1fr). - Setting widths on items instead of defining tracks on the container.
- Forgetting
min-width: 0on items containing<pre>or wide tables; like flex items, grid items don't shrink below their content by default. - Reordering content visually with placement in a way that confuses keyboard and screen reader users.
For one-dimensional rows like toolbars, flexbox is still the better tool; see the flexbox cheat sheet.