日本語

“CSS flex-wrap: How to Wrap Flex Children When the Width Is Not Enough”

​​CSS​Flexbox,Playwright

This article was translated from the Japanese original with AI. 日本語の原文を読む

Children laid out with display: flex stay on one line by default, even when they do not fit. Adding flex-wrap: wrap to the parent moves the children that do not fit onto the next line. Use it when you want a tag list, a row of buttons, or a navigation menu to wrap onto two or three lines on a narrow mobile screen.

css
.tags {
  display: flex;
  flex-wrap: wrap; /* Move children that do not fit to the next line */
  gap: 8px;
}
Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened a page in Playwright with a 400px-wide parent (no padding or border) containing 50px-high children, and measured the position and size of each child with getBoundingClientRect(). I did not test Firefox, Safari, right-to-left writing, or vertical writing.

Without wrapping, children shrink to fit on one line

The initial value of flex-wrap is nowrap (CSS Flexbox spec). With nowrap, the parent places all children on one line.

I put four children with width: 150px into a 400px parent. Together they are 600px, so they are 200px too wide. All four shrank to 100px and stayed on one line. According to the spec's description of flex, the initial value of flex is 0 1 auto, so flex-shrink is 1. The children therefore share the missing space between them and shrink.

When I added flex-shrink: 0 to stop the children from shrinking, the fourth child started at x=450 and overflowed past the right edge of the parent. The parent's scrollWidth was 600, larger than its clientWidth of 400. MDN's flex-wrap documentation also says that with nowrap, content can overflow the flex container.

Four stacked diagrams. In the first, with nowrap, four light blue boxes sit in a row inside a gray dotted parent, each shrunk to 100px wide. In the second, with flex-shrink: 0, four 150px boxes sit in a row and run past the right edge of the gray dotted parent, starting partway through the third box. In the third, with flex-wrap: wrap, the 150px boxes form two rows of two. In the fourth, with wrap and flex: 1 1 150px, five boxes form two rows of two, and the fifth box alone stretches across the full width of its row
Children in a 400px parent (gray dotted outline) with different flex-wrap and child settings. The labels in the image are in Japanese.

flex-wrap: wrap moves children that do not fit to the next line

With flex-wrap: wrap on the same parent, the children kept their 150px width and formed two rows of two.

css
.row {
  display: flex;
  flex-wrap: wrap;
  width: 400px;
}

.item {
  width: 150px;
}
Child x y Width
1 0 0 150
2 150 0 150
3 0 50 150
4 150 50 150

Three children side by side would take 450px, which does not fit in 400px, so the third child moved to a new line. The height of the parent grew from 50px for one line to 100px for two.

Where lines break depends on gap and margin

The spec's algorithm for splitting children into lines places children into a line in order, starting from the first, and starts a new line when the next child no longer fits within the inner width of the parent. When measuring, the child's size includes its margin, so the outer size is used.

Three 130px children side by side total 390px, so they fit on one line. Adding gap: 10px puts two 10px gaps between them, for 410px in total, and the third child wrapped to the next line. Adding margin-right: 10px to each child gives 140px per child including the margin, for 420px in total, and the third child wrapped as well.

Child width and added spacing Children per line
130px (nothing added) 3
130px + gap: 10px 2
130px + margin-right: 10px 2
120px + gap: 10px 3

gap also adds space between wrapped lines. With gap: 10px, the y of the second line was 60, so there was a 10px space between the lines. With margin-right: 10px, the y of the second line was 50, and there was no space between the lines.

To fill each line, combine wrap with flex: 1 1 150px

With wrap alone, each child keeps its own width, which leaves extra space at the right end of each line. To stretch the children to fill the full width of the line, give them flex: 1 1 150px. The 150px basis determines where lines break, and the leftover space is shared among the children on the same line.

css
.row {
  display: flex;
  flex-wrap: wrap;
}

.item {
  flex: 1 1 150px; /* Wrap based on 150px; extra space is shared and items grow */
}

With five children, the children on the first and second lines were each 200px wide, so two children filled each line. The one child left alone on the last line grew to the full 400px width of the line. The spec's algorithm for resolving flexible lengths works only on the children within a single line. Because flex-grow shares the extra space within each line, the child widths differ from line to line.

To stop the lone child on the last line from growing, add max-width to the children. With max-width: 200px, the fifth child stopped at 200px.

A child wider than the parent gets a line to itself

According to the spec, when the first child on a line does not fit on its own, that child is placed alone on a line. I placed a 500px child and a 100px child in a 400px parent with wrap.

The first child was placed alone on the first line and shrank to 400px. Because flex-shrink applies within that line after the break, it shrinks even with wrap. When I added flex-shrink: 0 to the first child, it stayed at 500px, the parent's scrollWidth became 500, and the child overflowed to the right. The second child went to the second line in both cases.

wrap-reverse stacks the lines in reverse

With flex-wrap: wrap-reverse, the wrapped lines stacked from bottom to top. Children 1 and 2 were on the lower line (y=50), and children 3 and 4 were on the upper line (y=0). The left-to-right order within each line was the same as with wrap. The spec says wrap-reverse creates multiple lines just as wrap does, and only reverses the direction in which the lines are stacked (the start and end of the cross axis).

Write both in one with flex-flow

You can write flex-direction and flex-wrap together with the shorthand flex-flow. flex-flow: row wrap produced the same layout as writing flex-direction: row and flex-wrap: wrap separately. When you leave out the direction, as in flex-flow: wrap, flex-direction takes its initial value, row.

css
.row {
  display: flex;
  flex-flow: row wrap;
}

With a height on the parent, the gap between lines can grow

When the parent has a set height and wraps, the lines share that height. When I placed two rows of 50px-high children in a 300px-high parent, the y of the second line was 150, not 50. When align-content is not set (the computed value in Chromium is normal), each of the two lines gets 150px of height, and the children sit at the top edge of their line. When the child has no height set, it stretched to the full 150px line height.

To pack the lines toward the top, add align-content: flex-start to the parent. The y of the second line became 50, and the gap between the lines disappeared.

css
.row {
  display: flex;
  flex-wrap: wrap;
  height: 300px;
  align-content: flex-start; /* Pack lines toward the top */
}

As the spec's description of align-content says, align-content affects only parents with multiple lines. When I added align-content: flex-start to a nowrap parent, children without a set height still stretched to the parent's 300px height.

To wrap in a column, the parent needs a height

Combining flex-direction: column with flex-wrap: wrap stacks children vertically and moves the ones that do not fit below into a new column on the right. However, wrapping does not happen unless the parent has a set height. When I placed four 80px-high children, the parent grew to 320px tall and all four stayed in one column.

With height: 200px on the parent, two children take 160px and three take 240px, so the third child moved to a second column. Because align-content was not set, the two columns shared the parent's 400px width, and the second column started at x=200.

​Posted by 86