Sub topic 16
Grid or Flexbox: choosing the right tool
This chapter assumes you can build a basic grid with grid-template-columns, gap, and grid-area, and that you know display: flex and the idea of main and cross axes. Grid and Flexbox are both modern layout systems. They overlap, so the useful question is which one fits the problem in front of you.
The core difference
Flexbox lays items out along one axis at a time. Grid lays out rows and columns together.
- Flexbox is content-out. Item sizes drive the layout. Items sit in a line and may wrap onto new lines, but each wrapped line is laid out independently.
- Grid is layout-out. You define tracks first, then place items into them. Items in different rows share the same column tracks, so they always line up.
Seeing the difference with wrapping
Wrapping is where the two diverge most. With Flexbox, the last row of a wrapped list stretches or sits differently from the rows above it, because each row is its own line.
.flex-list {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.flex-list > li {
flex: 1 1 200px;
}
.grid-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 1rem;
}If the last row has two items, .flex-list stretches them to fill the row because of flex: 1. .grid-list keeps them in the same column widths as the rows above. For card galleries and anything that should look like a table of cells, Grid gives the more predictable result.
When to choose each
| Situation | Better tool | Why |
|---|---|---|
| Page shell with header, sidebar, main, footer | Grid | You control both axes and can name areas. |
| Card gallery with aligned columns | Grid | Columns stay consistent across rows. |
| Navigation bar with a logo and links | Flexbox | One row, content-sized, easy spacing with `gap` or `margin-left: auto`. |
| Centering one item in a container | Either | `place-items: center` on Grid or `align-items` and `justify-content` on Flexbox. |
| Row of buttons that wrap unevenly | Flexbox | Each line sizes to its own content. |
| Items that must overlap or span rows and columns | Grid | Line-based placement and spans. |
Using both together
The two tools combine well. A common pattern is Grid for the page and Flexbox inside components.
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-areas:
"header header"
"sidebar main";
}
.page > header {
grid-area: header;
display: flex;
align-items: center;
justify-content: space-between;
}The header is a grid item in the page layout and a flex container for its own contents. Each element gets the system that suits its job.
Exercise
A product list shows cards that wrap onto several rows. The last row has two cards, and they must stay the same width as the cards above. Which layout system do you choose, and why?
Show answer
Grid, for example with grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)). Grid columns are shared by all rows, so the last row's cards keep the same width. A wrapping flex container sizes each line separately, so those two cards would stretch (with flex-grow) or sit unaligned.