Flex Wrap and Flex Flow – Managing Multiple Rows
Quick Answer: Use flex-wrap on a flex container to decide whether items stay on one line or move onto new lines when they run out of space. The default is nowrap. Use wrap to allow multiple lines, or wrap-reverse to allow multiple lines stacked in the opposite cross direction. The flex-flow shorthand sets flex-direction and flex-wrap together, for example flex-flow: row wrap.
S1. Why Do We Need Flex Wrap?
Imagine you are building a course catalog page. It has six course cards: HTML, CSS, JavaScript, React, Node.js and SQL. You want them to sit in rows, and you want a new row to start when the screen runs out of space.
In the previous article, we learned how flex-direction sets the direction of a flex container. But a direction alone does not say what should happen when there are more items than the line can hold.
Would you split the cards into separate row containers by hand? That works for six cards on one screen size, but it breaks as soon as the screen changes.
CSS Flexbox solves this with one property on the parent. Let's first see what happens without it.
S2. The Problem: A Flex Container That Never Wraps
Flex line = one row (or column) of flex items inside a flex container. By default, a flex container has exactly one line.
Imagine the six course cards sit inside a 600px container. The developer makes it a flex container and gives each card a width of 180px. No wrapping rule is applied.
Example 1: Course cards without wrapping
Assumption: Exact sizes depend on the font and browser, so the output below is approximate.
<!-- HTML -->
<div class="courses">
<div class="course">HTML</div>
<div class="course">CSS</div>
<div class="course">JavaScript</div>
<div class="course">React</div>
<div class="course">Node.js</div>
<div class="course">SQL</div>
</div>
/* CSS */
.courses {
display: flex;
width: 600px;
gap: 16px;
}
.course {
width: 180px;
padding: 16px;
box-sizing: border-box;
background: #E2E8F0;
}
Expected output
Container (600px wide): [HTML][CSS][JavaScript][React][Node.js][SQL] All six cards stay on one line. The cards shrink as far as their content allows, and the row can still spill past the 600px edge.
The page renders, but the cards are not 180px wide any more. A flex container keeps all its items on one line unless we say otherwise.
What goes wrong?
- Squeezed items: Six cards are forced into the space of about three.
- Overflow: Cards cannot shrink below the size of their content, so the row can spill outside its container.
- Manual rows: Developers split the cards into separate row containers in the HTML.
- Risky changes: Adding a seventh card means rearranging the markup again.
- Poor clarity: The CSS does not say that extra items may move to a new line.
Now think about many card grids, tag lists and galleries on one website. Splitting rows by hand for each one becomes hard to maintain.
Remember: The container is not broken. It is using its default nowrap behaviour. The real problem is that nobody told it that items may move onto a new line.
Animation 1: How a single line squeezes the items
Many cards, one line
The red highlights show items forced to share a single line.
The animation highlights the container with no wrapping and the items it squeezes onto one line.
S3. The Solution: Allow Wrapping with flex-wrap
Flexbox gives us a simpler design.
flex-wrap = a property on the flex container that decides whether items stay on one line or move onto new lines.
Instead of splitting rows by hand, tell the container that wrapping is allowed. It accepts three values.
nowrapkeeps all items on one line. This is the default.wrapmoves items onto new lines when they run out of space. New lines are added in the cross-axis direction, so in a row they go downwards.wrap-reversealso wraps, but new lines are added in the opposite cross direction, so in a row they go upwards.
Now the same six cards flow into rows.
Example 2: Course cards that wrap
Assumption: This example uses standard CSS supported by all modern browsers, and assumes a left-to-right language.
.courses {
display: flex;
flex-wrap: wrap;
width: 600px;
gap: 16px;
}
.course {
width: 180px;
padding: 16px;
box-sizing: border-box;
background: #E2E8F0;
}
Expected output
[ HTML ] [ CSS ] [ JavaScript ] [ React ] [ Node.js ] [ SQL ] Three cards fit on a line (3 x 180px + 2 x 16px = 572px). A fourth would need 768px, so it moves to a new line. Every card keeps its 180px width.
Notice the difference. The HTML stays the same, and one property on the parent lets the cards flow into rows.
Animation 2: The flex-wrap design
One property, many lines
The container creates a new line whenever the current one is full.
The animation highlights each flex line and the container rules that create it.
Compare both approaches
| Without wrapping (nowrap) | With flex-wrap: wrap |
|---|---|
| All items stay on one line. | Items move to a new line when they run out of space. |
| Items shrink, and the row may overflow. | Items keep their intended size. |
| Extra rows need separate containers in the HTML. | One container holds every item. |
| Adding an item may break the layout. | A new item flows into the next free place. |
| Layout intent is hidden. | Layout intent is clear. |
The idea to remember
- flex-wrap = a property set on the flex container, not on the items.
- Default =
nowrap, which keeps one flex line. - Flex line = one row or column of items.
wrapallows more than one. - wrap-reverse = new lines are added in the opposite cross direction.
- flex-flow = a shorthand for
flex-directionandflex-wrap.
S4. Step-by-Step Code Breakdown
1. Start with a flex container
flex-wrap only works on a flex container. Add display: flex or display: inline-flex to the parent first. Without it, the property has no effect.
2. Know the three values
Each value decides whether lines are created and in which direction they are stacked. The table assumes a row direction in a left-to-right language.
| Value | Behaviour | New lines go | Typical use |
|---|---|---|---|
nowrap |
One line only. Items may shrink or overflow. | No new lines | A toolbar that must stay on one line |
wrap |
Items move to new lines when needed. | Downwards | Card grids and tag lists |
wrap-reverse |
Items wrap, and the lines are stacked in reverse. | Upwards | Special visual layouts |
3. Compare all three values
The same five items behave differently under each value. Each container below has the same children. Only flex-wrap changes.
Example 3: nowrap, wrap and wrap-reverse
Assumption: In the nowrap case, the items shrink equally. Exact sizes depend on padding and font.
<!-- HTML (three containers with the same children) -->
<div class="demo nowrap"><span>A</span><span>B</span><span>C</span><span>D</span><span>E</span></div>
<div class="demo wrap"><span>A</span><span>B</span><span>C</span><span>D</span><span>E</span></div>
<div class="demo wrap-reverse"><span>A</span><span>B</span><span>C</span><span>D</span><span>E</span></div>
/* CSS */
.demo {
display: flex;
width: 300px;
}
.demo span {
width: 120px;
padding: 10px;
box-sizing: border-box;
}
.nowrap { flex-wrap: nowrap; }
.wrap { flex-wrap: wrap; }
.wrap-reverse { flex-wrap: wrap-reverse; }
Expected output
nowrap: [A][B][C][D][E] one line, each item about 60px wide wrap: [ A ][ B ] [ C ][ D ] [ E ] wrap-reverse: [ E ] [ C ][ D ] [ A ][ B ]
In wrap-reverse, the first line (A and B) is placed at the bottom, and later lines are added above it. The order of the items inside each line does not change. Only the order of the lines is reversed.
4. Add spacing with gap
Use gap on the container to space the items and the lines. It accepts one or two values. With two values, the first is the space between rows and the second is the space between columns. The gap appears only between items and lines, not around the outside edge.
Example 4: Different row and column gaps
.courses {
display: flex;
flex-wrap: wrap;
gap: 24px 12px; /* row gap, then column gap */
}
Expected output
[ HTML ] [ CSS ] [ JavaScript ]
← 24px between lines
[ React ] [ Node.js ] [ SQL ]
12px between items on the same line.
24px between one line and the next.
5. Use the flex-flow shorthand
flex-flow sets flex-direction and flex-wrap in one declaration. The two values can be written in either order. If you leave one out, it is set to its initial value.
Example 5: flex-flow compared with the longhand properties
.a { flex-flow: row wrap; }
.b {
flex-direction: row;
flex-wrap: wrap; /* same result as .a */
}
.c { flex-flow: wrap; } /* direction omitted: row */
.d { flex-flow: column; } /* wrap omitted: nowrap */
.e { flex-flow: column wrap; }
Expected output
.a direction: row wrap: wrap .b direction: row wrap: wrap .c direction: row wrap: wrap (row is the initial value) .d direction: column wrap: nowrap (nowrap is the initial value) .e direction: column wrap: wrap
6. Wrap items in a column
Wrapping also works with column. In that case, a new line is a new column. But a column wraps only when the container has a limited height. With automatic height, the container just grows taller and never needs a second column.
Example 6: A column that wraps into two columns
<!-- HTML -->
<div class="tags">
<span>Tag 1</span> <span>Tag 2</span> <span>Tag 3</span>
<span>Tag 4</span> <span>Tag 5</span> <span>Tag 6</span>
</div>
/* CSS */
.tags {
display: flex;
flex-flow: column wrap;
height: 200px;
gap: 10px;
}
.tags span {
height: 50px;
padding: 0 16px;
background: #E2E8F0;
}
Expected output
[ Tag 1 ] [ Tag 4 ] [ Tag 2 ] [ Tag 5 ] [ Tag 3 ] [ Tag 6 ] Three tags fit in 200px (3 x 50px + 2 x 10px = 170px). A fourth would need 230px, so it starts a new column.
S5. Architecture and Real-World Trade-Offs
The wrap rule is set per container, so each part of a page can decide how its items flow onto new lines. This is useful in card grids, tag lists, galleries and filter bars.
How the design fits a website
Imagine a website with a course grid, a tag list and a long category list. Each one has its own parent with its own wrapping rule.
A focused wrapping flow
Each website area chooses the wrapping that suits its content.
The animation traces each website area to its own wrapping rule.
Benefits for larger websites
- Maintainability: The wrapping rule lives on the parent, so changes happen in one place.
- Adaptive layouts: With fixed item widths, the number of items per line adapts to the container width.
- Less markup: One container holds every item. You do not need a wrapper for each row.
- Clean spacing:
gapspaces both the items and the lines. - Short code:
flex-flowsets direction and wrapping in one line.
Important trade-offs
- Each flex line is laid out on its own, so items in different lines do not line up in columns. Use CSS Grid when you need strict rows and columns.
- The last line may hold fewer items and leave empty space at its end.
wrap-reversereverses the order of the lines visually, but keyboard focus and screen readers still follow the HTML order.- A column container wraps only when its height is limited.
- Wrapping happens only when the items cannot fit on the line, so items need sensible sizes.
Important: flex-direction decides which way items flow, and flex-wrap decides whether they may continue on a new line. Choose both, and let flex-flow write them in one declaration.
S6. Common Mistakes and Best Practices
Mistake 1: Forgetting to enable wrapping
Wrong: Give each card a fixed width in a flex container and expect extra cards to move to a new line.
Why it fails: The default is nowrap, so all items stay on one line, shrink and may overflow.
Better: Add flex-wrap: wrap to the container.
Rule: If items may need more than one line, say so on the container.
Mistake 2: Expecting a column container with automatic height to wrap
Wrong: Use flex-flow: column wrap and leave the height automatic.
Why it fails: The container grows to fit all items, so there is never a reason to start a second column.
Better: Give the container a height or max-height.
Rule: A column needs a limit before it can wrap.
Mistake 3: Writing flex-flow: wrap and expecting the direction to stay
Wrong: Set flex-direction: column, then later write flex-flow: wrap in the same rule.
Why it fails: The shorthand sets every value it covers. The omitted direction goes back to row.
Better: Write flex-flow: column wrap, or use the two separate properties.
Rule: A shorthand overwrites the values you leave out.
Mistake 4: Spacing wrapped items with margins
Wrong: Add margin-right to every card to space them.
Why it fails: The last card in each line also gets the margin, which adds extra space at the end of the line and can cause earlier wrapping.
Better: Use gap on the container.
Rule: Space between items belongs to the container.
Mistake 5: Expecting wrapped lines to behave like a grid
Wrong: Expect the cards in the last line to line up under the cards above, or to stretch across the full width.
Why it fails: Each flex line is laid out independently, and items stay at the start of a shorter last line.
Better: Use CSS Grid when you need strict rows and columns.
Rule: Flexbox wraps in one dimension at a time. Choose the tool that matches the layout.
S7. How This Comes Up in Interviews
Question 1: What is the default value of flex-wrap?
What the interviewer wants: They want to know whether you understand what happens when items do not fit.
Strong answer:
"The default is nowrap. All items stay on one line. They can shrink, and if they cannot shrink enough, they overflow the container."
Question 2: What is the difference between wrap and wrap-reverse?
Strong answer:
"Both allow items to move onto new lines. With wrap, new lines are added in the cross-axis direction, which is downwards for a row. With wrap-reverse, the cross start and end are swapped, so new lines are added upwards. The order of items inside each line stays the same."
Question 3: What does flex-flow do?
Strong answer:
"It is a shorthand for flex-direction and flex-wrap. The values can be in either order. If I leave one out, it is set to its initial value, so flex-flow: wrap means row wrap."
Question 4: Why does my column container not wrap?
Strong answer:
"A column wraps only when the container has a limited height. With automatic height, the container simply grows to fit its items. I set a height or max-height so a new column can start."
What should different experience levels explain?
- Junior developer: Explain
flex-wrap: wrapand show a card row that moves to a second line. - Senior developer: Explain flex lines, the three values, the
flex-flowshorthand and why a column needs a limited height. - Technical architect: Explain when to choose wrapping Flexbox and when to choose CSS Grid for a design system.
S8. Hands-On Practice
Beginner: Make a row of cards wrap
Create a flex container that is 600px wide with six cards, each 180px wide. First leave the wrapping alone, then add flex-wrap: wrap and observe the result.
Expected result: The cards first sit on one line, then flow into two lines of three.
Intermediate: Compare all three values
Create three containers with the same five children labelled A to E. Give each one a different flex-wrap value. Then add gap: 24px 12px to the wrapping ones.
Hint: Watch where item A appears in each container.
Advanced: Build a responsive tag list and a column list
Build a tag list with flex-flow: row wrap and resize the browser to see tags move to new lines. Then build a category list with flex-flow: column wrap and a fixed height. Finally, try wrap-reverse on one list and press the Tab key through any links inside it.
Expected result: The tag lines adapt to the container width. The category list creates a new column when its height is full. Tab order follows the HTML order, even when the lines are reversed.
S9. Next Steps and Community
Previous article: Flex Direction – Row, Column and Reverse.
Next article: Justify Content – Mastering Main Axis Alignment.
Continue the series by learning how justify-content positions and spaces items along the main axis, including the items inside each wrapped line.
Try the examples in your own HTML and CSS project. Change the container width, switch between the three wrap values, try flex-flow combinations, and share your layouts with the Upgradix community. Help other developers understand Flexbox through practical examples.
Let the items wrap. Then the layout follows.
Upgradix by Shrash Studio
Learn Technology. Build Your Future.
Quality Technology Education. Completely Free.