Lesson 22 of 296
Grid
What you will learn
- What CSS Grid is for
- Defining columns and rows
- The fr unit, repeat() and minmax()
- Making items cover several cells
- Named areas
- Responsive grids with no media queries
- Flexbox or Grid: how to choose
On this page
What is Grid?
CSS Grid is a layout system for two directions at once: rows and columns. It is the best tool for whole-page layouts, galleries, dashboards and any design that lines up in both directions.
.gallery {
display: grid;
grid-template-columns: 200px 200px 200px;
gap: 16px;
}The children are placed into three columns of 200px. When a row is full, the next item starts a new row.
The fr unit
fr means "a fraction of the free space".
grid-template-columns: 1fr 1fr 1fr; three equal columns
grid-template-columns: 2fr 1fr; first column twice as wide
grid-template-columns: 250px 1fr; fixed sidebar, rest for contentrepeat()
grid-template-columns: repeat(4, 1fr);is the same as 1fr 1fr 1fr 1fr.
minmax()
grid-template-columns: repeat(3, minmax(200px, 1fr));Each column is at least 200px and at most 1fr.
A responsive grid with no media queries
This is one of the most useful lines in CSS:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1.5rem;
}It means: "fit as many columns as possible, each at least 250px wide, and share the extra space". On a phone you get one column; on a laptop three or four. No breakpoints needed.
auto-fill is similar, but keeps empty columns when there are few items. auto-fit stretches the items to fill the row. For most card layouts, auto-fit is what you want.
Rows
grid-template-rows: 80px 1fr 60px;If you do not set rows, the browser makes them as tall as their content. These are called implicit rows. grid-auto-rows: 200px sets the height of rows the browser creates automatically.
Placing items on the grid
Grid lines are numbered from 1, starting at the left edge (and the top edge for rows).
1 2 3 4
| A | B | C |.featured {
grid-column: 1 / 3; from line 1 to line 3, so 2 columns wide
grid-row: span 2; 2 rows tall
}
.full-width {
grid-column: 1 / -1; from the first line to the last line
}-1 means the last line. span 2 means "cover 2 tracks from wherever you start".
Named areas
You can draw the layout with words:
.page {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100dvh;
}
.page header { grid-area: header; }
.page aside { grid-area: sidebar; }
.page main { grid-area: main; }
.page footer { grid-area: footer; }To change the layout on phones, redraw the areas in a media query:
@media (max-width: 700px) {
.page {
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}
}Aligning in a grid
| Property | Aligns |
|---|---|
justify-items | Items horizontally inside their cells |
align-items | Items vertically inside their cells |
place-items: center | Both at once |
justify-self / align-self | One item only |
.box {
display: grid;
place-items: center;
}This is the shortest way to centre something in CSS.
Flexbox or Grid?
| Question | Choose |
|---|---|
| Lining items up in one direction (a row or a column)? | Flexbox |
| Rows and columns that must line up together? | Grid |
| Should the content decide the sizes? | Flexbox |
| Should the layout decide the sizes? | Grid |
| Navigation bar, button group, centring one thing | Flexbox |
| Whole page layout, gallery, dashboard, form with aligned labels | Grid |
They are not rivals. Most real pages use both: Grid for the page, Flexbox inside the parts.
Subgrid
grid-template-columns: subgrid lets a child grid use its parent's columns, so content in different cards lines up perfectly. It is supported in all modern browsers. Good to mention in a senior interview.
Common mistakes
- Using Flexbox with many width calculations for something that is clearly a grid.
- Forgetting that line numbers start at 1 and that 3 columns have 4 lines.
- Using
auto-fillwhen you wanted items to stretch (auto-fit). - Changing the visual order with grid placement so it no longer matches the HTML order for keyboard users.
How to say it in an interview
"Grid is two-dimensional, Flexbox is one-dimensional. I use Grid when rows and columns need to line up, like a page layout or a card gallery, and Flexbox for arranging items in a single row or column. The fr unit shares free space, and repeat with auto-fit and minmax creates a responsive grid without media queries. Named template areas make page layouts readable and easy to rearrange on small screens."
Key words
| Word | Meaning |
|---|---|
| Grid container | The parent with display: grid |
| Track | One row or one column |
| Grid line | The numbered lines between tracks |
| fr | A fraction of the free space |
| minmax() | A size between a minimum and a maximum |
| auto-fit | Fit as many columns as possible and stretch them |
| Template areas | Naming parts of the grid with words |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.