Sub topic 6
Wrapping items onto multiple lines
This chapter covers flex-wrap, flex lines, and how wrapping interacts with sizing and spacing. It assumes you know what a flex container is, how the main and cross axes work, and how justify-content and align-items behave on a single line. You should also be comfortable with the box model and rem and px units.
The default: one line that never wraps
A flex container lays its items out on a single line by default. The initial value of flex-wrap is nowrap. If the items are wider than the container, they shrink (see flex-shrink) until they hit their minimum size. If they still don't fit, they overflow the container.
.row {
display: flex;
flex-wrap: nowrap; /* the default */
}Wrapping changes this. Once the items on a line no longer fit, the extra items move to a new line instead of shrinking or overflowing.
The three values of flex-wrap
| Value | Behavior |
|---|---|
| `nowrap` | All items stay on one line. They may shrink or overflow. |
| `wrap` | Items break onto new lines. New lines are added in the cross-axis direction. |
| `wrap-reverse` | Items break onto new lines, but new lines are added in the opposite cross-axis direction. |
With flex-direction: row in a left-to-right language, wrap stacks new lines downward. wrap-reverse stacks them upward, so the first line is at the bottom. The order of items within each line is unchanged.
.cards {
display: flex;
flex-wrap: wrap;
}What a flex line is
When wrapping is on, the container holds one or more flex lines. Each line is its own small flex layout. Items are assigned to lines first, then each line distributes its own free space.
The browser assigns items to lines by walking through them in order. It adds each item to the current line while the sum of the items' hypothetical sizes (their flex-basis clamped by min and max sizes, plus margins, borders and padding) still fits in the container's main size. When the next item would not fit, a new line starts. Only after this step do flex-grow and flex-shrink run, and they run per line.
Two consequences follow from this order:
- Line breaking depends on
flex-basis, not on the final size. An item withflex: 1 1 200pxcounts as 200px when lines are formed, even though it may grow later. - A line is sized independently. A line with two items grows each of them to fill the line, and the last line with one item grows that item to fill its line.
Cards that wrap and grow
The most common use of wrapping is a row of cards that reflows as the container narrows.
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 200px;
}Each card has a base size of 200px, may grow, and may shrink. Suppose the container is 700px wide and the gap is 16px. Three cards need 600px plus two gaps of 16px, which is 632px, so three fit on the first line. A fourth card would need 848px, so it wraps. The leftover 68px on the first line is shared among the three cards by flex-grow, so each card is about 222.7px wide.
A single card on the last line stretches to the full container width. That surprises many people. If you want leftover cards to keep the same width as the others, you have two options: set flex-grow: 0 and give a fixed or maximum width, or use Grid, which has a real column structure (see the last chapter).
.card {
flex: 0 1 200px; /* never grows, so the last line stays tidy */
}With flex-grow: 0, lines are left-aligned (or follow justify-content) and the extra space remains empty on each line.
Wrapping with a column direction
Wrapping works along whichever axis is the main axis. With flex-direction: column, items wrap into new columns, but only if the container has a constrained height. A column container with an automatic height grows to fit its items, so nothing ever needs to wrap.
.columns {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 300px;
gap: 0.5rem;
}Here, items fill a column until 300px is reached, then continue in a new column to the right. If you remove the height, you will see a single tall column.
Wrapping and gap
The gap property inserts space only between items and between lines. It adds nothing at the outer edges. In a wrapping container, gap: 1rem sets both row-gap (between lines) and column-gap (between items on a line). You can set them separately.
.tags {
display: flex;
flex-wrap: wrap;
gap: 0.5rem 1rem; /* row-gap column-gap */
}Gaps count toward line breaking. The browser includes them when it checks whether the next item fits, which is why the 700px example above needed 632px for three cards. Before gap worked in Flexbox, authors used margins on every item and negative margins on the container. You can still see that pattern in older code. Prefer gap in new work.
Common errors
- Forgetting that wrapping needs a size limit. In a row container, the container must be narrower than the total item size for wrapping to happen. In a column container, it needs a set height.
- Expecting columns to line up across lines. Each line is laid out independently, so a card on line two does not align with a card above it unless the widths happen to match.
- Using
widthandflex-basistogether without knowing which wins. Whenflex-basisis notauto, it overrideswidthfor the main size.max-widthandmin-widthstill clamp the result. - Wide items that never wrap. An item that is wider than the container goes on its own line and overflows it. Wrapping does not shrink an item below its automatic minimum size. Chapter material on
min-width: 0explains how to relax that. - Using
align-itemsto control space between lines.align-itemspositions items inside their line. The space between lines is controlled byalign-content, which is covered in a later chapter.
Reading order and wrap-reverse
wrap-reverse flips the order of lines visually but not the order of items in the DOM. Screen readers and keyboard focus follow the source order, so a visual layout that differs from it can confuse users. Use wrap-reverse sparingly and check keyboard tabbing after you apply it.
Putting it together
The following page fragment shows a wrapping tag list with a gap. It runs as shown in any modern browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Wrapping tags</title>
<style>
.tags {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
max-width: 20rem;
padding: 0;
list-style: none;
}
.tags li {
padding: 0.25rem 0.75rem;
background: #e0e7ff;
border-radius: 999px;
}
</style>
</head>
<body>
<ul class="tags">
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
<li>Accessibility</li>
<li>Performance</li>
<li>Flexbox</li>
<li>Grid</li>
</ul>
</body>
</html>The container is 20rem wide, so the tags fill each line and the rest move down. Resize the container's max-width and watch how the lines re-form.
Exercise
A container is 700px wide with display: flex; flex-wrap: wrap; gap: 1rem (assume 1rem = 16px). Each item has flex: 0 0 200px. How many items fit on the first line, and how wide is the leftover space on that line?
Show answer
Three items fit. Three items take 600px and two gaps take 32px, which is 632px. A fourth item would need 848px. The leftover space is 700px - 632px = 68px, and it stays empty because flex-grow is 0.
Exercise
You have a column-direction flex container with flex-wrap: wrap, and the items never move into a second column. What is the most likely cause, and what is the fix?
Show answer
The container has an automatic height, so it grows to hold every item and nothing needs to wrap. Give the container a fixed or constrained height, such as height: 300px or max-height: 300px, and the items will continue in a new column once the first one is full.