CSS Grid Layouts
Build two-dimensional layouts with CSS Grid using columns, rows, the fr unit, gaps, repeat and minmax, and named template areas for whole pages.
Welcome back!
Step 1 of 16Learn
Rows and columns at once
Flexbox lines things up in one direction. But a page layout, with a header across the top, a sidebar and main content side by side, and a footer below, is two-dimensional: it has rows and columns. That's what CSS Grid is for.
You turn a container into a grid and define its columns:
.gallery {
display: grid;
grid-template-columns: 200px 200px 200px;
gap: 16px;
}Now the children fill a three-column grid automatically, row after row. You only define the columns; new rows are created as needed. gap sets the space between rows and columns (you can also use row-gap and column-gap separately).
Fixed pixel columns aren't flexible, though. Grid introduces a new unit, fr, meaning a fraction of the free space:
grid-template-columns: 1fr 1fr 1fr; /* three equal columns */
grid-template-columns: 2fr 1fr; /* main content twice as wide as sidebar */
grid-template-columns: 250px 1fr; /* fixed sidebar, flexible content */The browser first subtracts fixed sizes and gaps from the container width, then divides what's left according to the fr values. Because fr works with the free space after gaps, you don't have to do percentage math that adds up to exactly 100%.
Step 2 of 16Try the calculator
fr column calculator
Enter a grid container's width, the gap and three fr values to see how wide each column becomes.
- Free space after gaps
- 960 px
- Column 1 width
- 480 px
- Column 2 width
- 240 px
- Column 3 width
- 240 px
Step 3 of 16Practice · Work it out
A grid is 920 px wide with grid-template-columns: 1fr 1fr 2fr; and a 20 px gap. How wide is the 2fr column, in pixels?
Step 4 of 16Practice · Chart
Column widths: 1fr 1fr 2fr in a 920 px grid with 20 px gaps
Tap the column that gets twice the space of the others.
Tap the bar that answers the question, then press Check.
Step 5 of 16Learn
repeat, minmax and auto-fit
Writing 1fr 1fr 1fr 1fr gets repetitive, so Grid has repeat():
grid-template-columns: repeat(4, 1fr); /* four equal columns */minmax(min, max) sets a size range for a track: minmax(200px, 1fr) means "at least 200 px, and up to an equal share of the space."
Combine them with auto-fit for one of the most useful lines in all of CSS:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 1.5rem;
}This says: "fit as many columns as you can, each at least 240 px wide, and stretch them to share the leftover space." On a 1,200 px screen you get four columns, on a tablet two or three, on a phone one. A fully responsive card grid, with no media queries.
Rows can be sized too: grid-template-rows: auto 1fr auto; gives a header row sized to its content, a middle row that takes all remaining height and a footer row sized to its content, a classic full-page structure when the container has min-height: 100vh.
Items can also span several tracks: grid-column: span 2; makes a featured card two columns wide, and grid-column: 1 / -1; stretches an item across every column, from the first line to the last.
Step 6 of 16Note
Step 7 of 16Practice · Choose one
What does grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); do on a narrow 360 px phone screen?
Step 8 of 16Learn
Named areas: drawing your layout
For whole-page layouts, Grid lets you draw the layout with names:
.page {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 1rem;
}
header { grid-area: header; }
aside { grid-area: sidebar; }
main { grid-area: main; }
footer { grid-area: footer; }Each quoted string is a row, and each word is a cell. Repeating a name across cells makes that area span them: "header header" makes the header stretch across both columns. A dot (.) leaves a cell empty. The areas must form rectangles.
The beauty of this approach shows on small screens. To stack everything in one column on phones, you only redraw the map:
grid-template-columns: 1fr;
grid-template-areas: "header" "main" "sidebar" "footer";The HTML doesn't change at all. This is the separation of structure and presentation at its best.
Grid or Flexbox? Use Grid when you're laying out in two dimensions, or when you want the layout to control the sizes (a page, a gallery, a form with aligned labels). Use Flexbox when items flow in one direction and their content should drive their sizes (a nav bar, a toolbar, a row of tags). They combine freely: a grid page whose header is a flex container is completely normal.
Step 9 of 16Note
Step 10 of 16Practice · Spot the mistake
This grid should have a header across the top, a sidebar and main content in the middle, and a footer across the bottom. Tap the row of the template that is invalid.
Step 11 of 16Practice · Sort into groups
Would Flexbox or Grid be the more natural choice?
Tap an item, then tap its group (or drag it there).
Step 12 of 16Compare
Flexbox or Grid?
| Aspect | Flexbox | Grid |
|---|---|---|
| Dimensions | One: a row or a column | Two: rows and columns together |
| Sizing starts from | The content of the items | The layout you define on the container |
| Great for | Nav bars, button groups, centering, a row of tags | Page layouts, card galleries, forms with aligned labels |
| Can combine? | Yes: flex items inside grid cells are common | Yes: a grid inside a flex item works too |
Step 13 of 16Note
Step 14 of 16Note
Step 15 of 16Practice · True or false
In CSS Grid, the fr unit divides the container's width before gaps are subtracted.
Step 16 of 16Practice · Choose one
A designer wants a featured product card to stretch across two columns of a four-column product grid. Which declaration on the card does this?
Lesson complete!