Sub topic 6
Wrapping items onto multiple lines
This chapter assumes you know how to create a flex container, that you can tell the main axis from the cross axis, and that you have used justify-content and gap. Here you will learn how flex-wrap moves items onto new lines instead of squeezing them.
The default: one line
A flex container is single-line by default (flex-wrap: nowrap). If the items are wider than the container, they shrink as far as their minimum size allows. After that they overflow the container. For a row of cards, this is rarely what you want.
Turning on wrapping
flex-wrap accepts three values.
| Value | Behavior |
|---|---|
| `nowrap` | All items stay on one line. This is the default. |
| `wrap` | Items break onto new lines in the cross-axis direction. |
| `wrap-reverse` | Same as `wrap`, but new lines stack in the opposite cross-axis direction. |
With wrap, the browser fills a line with items until the next one would not fit. It then starts a new line. Each line is called a flex line, and each line runs its own layout. Free space is shared only among the items on that line.
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 200px;
}Each card has a starting width (flex-basis) of 200px. The browser decides where to break lines using those starting sizes. Once the lines are formed, flex-grow: 1 lets the cards on each line grow to share the leftover space. Because gap is counted when the browser decides what fits, the spacing never causes an unexpected overflow.
Wrapping and the last line
Because each line shares its own free space, a last line with fewer items may look different from the lines above it. With flex: 1 1 200px, two leftover cards stretch wider than the cards in the full rows. If you want every card to keep the same width, use flex: 0 0 200px. You can also use flex-grow: 0 and let justify-content position the leftovers.
Short form
flex-flow combines flex-direction and flex-wrap in one declaration:
.cards {
display: flex;
flex-flow: row wrap;
}Common mistakes
- Setting
flex-wrap: wrapand then usingflex-shrinkto control the layout. A wrapping container moves an item to a new line before it needs to shrink, so shrinking rarely happens. - Expecting columns to line up between lines. Lines are independent, so items in different lines do not align. If you need aligned columns, that is a job for Grid.
- Using
wrapin a column container without a fixed height. A column only wraps when the container has a limited height. Without one, the container grows to hold all items on one line.
Exercise
A container has display: flex; flex-wrap: wrap; width: 650px; gap: 10px. Each item has flex: 0 0 200px. How many items fit on the first line, and how much free space is left on that line?
Show answer
Three items fit. Three items take 600px, and the two gaps take 20px, so the line uses 620px and 30px is left. A fourth item would need another 210px, so it moves to the next line. Because flex-grow is 0, the 30px stays unused.
Exercise
You use flex: 1 1 200px on cards and notice that the two cards on the last line are much wider than the cards above. Name one change that keeps every card the same width.
Show answer
Change the declaration to flex: 0 0 200px, so the cards do not grow. Alternatively, keep flex-grow: 1 but set a max-width on the cards.