Flex-wrap in CSS: A Practical Guide for Developers

flex-wrap controls whether items in a flex container stay on one line or wrap onto new lines when they run out of room. You set it on the flex container, and the default value is nowrap.

.container {
  display: flex;
  flex-wrap: wrap;
}

If flex containers and flex items are new to you, start with the complete Flexbox guide. This post focuses on wrapping: the four values, a live demo, the space between wrapped lines, and why items sometimes refuse to wrap.

The Values of flex-wrap

There are three long-standing values, plus one new one:

Value What it does Browser support
nowrap The default. All items stay on one line, even if they overflow the container. All browsers
wrap Items that don’t fit move to a new line below. All browsers
wrap-reverse Like wrap, but new lines stack above the first one. All browsers
balance Wraps like wrap, but spreads the items so the lines are as even as possible. Chrome 150+ (experimental)

MDN describes the new value like this:

“Specifies that flex items broken across multiple lines are distributed so that the line lengths are as similar as possible.” (MDN, flex-wrap)

flex-wrap isn’t inherited. A flex container nested inside another one starts with nowrap again.

In RTL layouts such as Hebrew, items fill each line from the right rather than the left. The lines still stack from top to bottom (or bottom to top with wrap-reverse).

flex-wrap in Action

Pick a value, then drag the slider to make the items wider. The readout shows how many items end up on each line and whether anything sticks out of the container.

Demo 1: flex-wrap playground
100px
1
2
3
4
5
6
7
Lines: 1 line (7)Overflow: no

With nowrap, the items stay on one line. Once they’re wider than the container, they spill out. Switch to wrap, and those same items flow onto new lines instead.

The demo items use flex: none, which keeps their width intact. Without it, nowrap items shrink before they overflow. In my test, six 150px items in a 400px container squeezed down to 67px each.

The Last-Row Problem and flex-wrap: balance

With wrap, the browser fills each line as much as it can, then moves the rest down. That can leave one lonely item sitting on the last line.

Seven 100px items in a 320px container (no gap) wrap as 3 / 3 / 1. With balance, those same items split 3 / 2 / 2. Try both values in the demo above and watch the line readout.

.container {
  display: flex;
  flex-wrap: wrap;    /* fallback for browsers without balance */
  flex-wrap: balance;
}
balance is new and marked experimental. Chrome supports it from version 150; Firefox and Safari don’t yet. Browsers that don’t know the value drop that line and keep wrap, which is why the fallback comes first.

If you need even lines in every browser today, there are two other fixes:

  • Use gap instead of justify-content: space-between. With space-between, two items on the last line get pushed to the edges with a hole between them.
  • Switch to CSS Grid when every item must line up in the same columns.

Spacing Between Wrapped Items with gap

To add space between items and between wrapped lines, set gap on the container. Unlike margins, it never adds space on the outer edges.

.container {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 16px; /* 12px between lines, 16px between items */
}

Percentages, browser support and the gap versus margin question each have their own quirks. I cover them in the guide to CSS gap in Flexbox.

Aligning Wrapped Lines with align-content

Once items wrap, the container has more than one line. align-content decides how those lines use the container’s extra height.

“This property has no effect on single line flex containers (i.e., ones with flex-wrap: nowrap).” (MDN, align-content)

So it works with wrap, wrap-reverse and balance, even when all the items fit on one line. With nowrap, the single line always fills the container, so there’s nothing to align.

Demo 2: align-content on wrapped lines
1
2
3
4
5
6
7
8
9
10
Lines: 2 lines

Switch to nowrap: the items squeeze onto one line and the align-content buttons stop doing anything.

Responsive Cards Without Media Queries

flex-wrap plus a flexible basis gives you a row of cards that reflows on its own:

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.cards > * {
  flex: 1 1 200px;
}

Each card starts at 200px. When there’s room, the cards grow to fill the line. When there isn’t, the last card drops to the next line, no breakpoints needed.

The catch is the last line. Those cards grow too, so they can end up wider than the cards above. In my test, five cards in a 700px container became three 223px cards and two 342px cards.

If every card needs to be the same width, use a Grid layout with auto-fit and minmax() instead.

Why Isn’t flex-wrap Working?

When items refuse to wrap, it’s usually one of these:

  • flex-wrap is on the items instead of the container. It only works on the element with display: flex.
  • You never set it. The default is nowrap, so items shrink below their width to stay on one line.
  • The items use flex: 1. That shorthand sets flex-basis to 0%, so the line never fills and nothing wraps, even when the items have a width.
  • It’s a column container with no height. With flex-direction: column, items only wrap into a new column when the container has a height or max-height.

The flex: 1 case catches plenty of people. In my test, six items with flex: 1 and width: 150px stayed on one line at 67px each. With flex: 1 1 150px, they wrapped into three lines of two.

/* Never wraps: the basis is 0% */
.item {
  flex: 1;
  width: 150px;
}

/* Wraps: the basis is a real size */
.item {
  flex: 1 1 150px;
}

For the column case, give the container a height:

.columns {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: 300px;
}

The flex-flow Shorthand

flex-flow sets flex-direction and flex-wrap in one declaration. flex-direction decides whether items run in a row or a column: row (the default), row-reverse, column or column-reverse.

So these two declarations:

.flex-container {
  flex-direction: row-reverse;
  flex-wrap: wrap-reverse;
}

can be written as one:

.flex-container {
  flex-flow: row-reverse wrap-reverse;
}

FAQs

A few common questions about flex-wrap:

What does flex-wrap do in CSS?
It controls whether flex items stay on a single line (nowrap, the default) or break onto new lines when they run out of space (wrap, wrap-reverse or balance). You set it on the flex container.
What is the difference between flex-wrap: wrap and nowrap?
With nowrap, all items share one line, so they shrink or overflow the container. With wrap, items that don't fit move to a new line.
Is flex-wrap inherited?
No. It applies only to the flex container you set it on. A nested flex container starts at nowrap unless you set it again.
How do I add space between wrapped rows?
Use row-gap, or the gap shorthand, on the container. gap: 12px 16px puts 12px between lines and 16px between items.
What is flex-wrap: balance?
A new value that wraps items so the lines are as close in length as possible, which avoids a single item alone on the last line. Only Chrome 150 and later support it so far, so declare flex-wrap: wrap first as a fallback.
Why are my flex items not wrapping?
The usual causes: flex-wrap is set on the items instead of the container, the items use flex: 1 (a 0% basis), or a column container has no height.
Should I use flex-wrap or CSS Grid?
Use flex-wrap when items have their own sizes and can flow, like tags, buttons or chips. Use Grid when every item must line up in the same columns.

Summary

flex-wrap turns a single-line flex container into a multi-line one. Use wrap for most layouts, add balance if you want even lines in Chrome, and use gap and align-content to control the space around those lines.

If items still refuse to wrap, check for flex: 1 and column containers without a height.

Discussion
[  0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!