How can I make flex items wrap onto another line?
Quick answer
Set flex-wrap: wrap on the flex container. By default, flex items stay on one line, where they may shrink or overflow if they cannot shrink enough. With wrapping enabled, items that do not fit move to another line; in a row layout, those new lines are rows. [S1]
The question, the details, the next step.
A practical checklist
Identify the element that directly contains the items to arrange.
Set that container to
display: flex.Add
flex-wrap: wrapto allow items to move to additional lines.Test the layout at a roomy width and a narrower width using your real content.
Choose wrapping if multiple rows are wanted; use one-line behavior only if shrinking or possible overflow is acceptable.
A few more useful answers
What is the default value of `flex-wrap`?
It defaults to nowrap, so flex items stay on one line. They may shrink or overflow if they cannot shrink enough. [S1]
Does wrapping create rows or columns?
With flex-direction: row, new lines are rows. With flex-direction: column, they are columns. [S1]
Can I know exactly how many items will fit on each row?
Not from the wrapping rule alone. Test your actual items and available container width; the number of items per row depends on the layout.
Sources
References used for this guide.
- [S1] Basic concepts of flexbox - CSS | MDN developer.mozilla.org · Checked
