CSS flex-wrap: nowrap, wrap, and wrap-reverse
flex-wrap controls whether Flexbox lays out items on one line or allows more lines. The default nowrap keeps one line. wrap starts a new line when items no longer fit, and wrap-reverse changes the direction in which those lines stack.
Syntax and the three values
.container {
display: flex;
flex-wrap: wrap;
}
Set the property on the parent with display: flex or inline-flex. With the default row and horizontal writing, the main size is the width. With column it is the height. Additional lines stack on the cross axis, perpendicular to the main axis.
| Value | How items are laid out |
|---|---|
nowrap |
Keeps one line. Items may shrink or overflow if their minimum sizes prevent it. |
wrap |
Starts another line when items no longer fit. |
wrap-reverse |
Wraps items and reverses the cross-axis stacking direction. |
The initial value is nowrap and the property does not inherit by default. initial restores that default and inherit takes the parent’s computed value explicitly. wrap-reverse keeps DOM order and the direction of items within each line. See CSS Flexbox · flex-wrap.
Try the example with five equal items
Save the code as flex-wrap.html and open it. No external files are needed. The five items use flex: 0 0 96px, so their 96px basis in the row neither grows nor shrinks. The gap between items or lines is 8px.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare the three flex-wrap values</title>
<style>
body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
* { box-sizing: border-box; }
h1 { font-size: 24px; } h2 { margin: 12px 0 4px; font-size: 18px; }
.box { display: flex; max-width: 360px; min-height: 140px; padding: 8px;
border: 1px solid #7795a3; gap: 8px; align-content: flex-start;
overflow-x: auto; background: #edf5f7; }
.item { flex: 0 0 96px; height: 40px; display: grid; place-items: center;
background: #176f80; color: white; border-radius: 4px; }
.nowrap { flex-wrap: nowrap; }
.wrap { flex-wrap: wrap; }
.reverse { flex-wrap: wrap-reverse; }
.box:focus-visible { outline: 3px solid #b75c00; outline-offset: 2px; }
</style>
</head>
<body>
<h1>Compare flex-wrap values</h1>
<h2>nowrap</h2>
<div class="box nowrap" tabindex="0" role="region" aria-label="nowrap example"><div class="item">A</div><div class="item">B</div><div class="item">C</div><div class="item">D</div><div class="item">E</div></div>
<h2>wrap</h2>
<div class="box wrap"><div class="item">A</div><div class="item">B</div><div class="item">C</div><div class="item">D</div><div class="item">E</div></div>
<h2>wrap-reverse</h2>
<div class="box reverse"><div class="item">A</div><div class="item">B</div><div class="item">C</div><div class="item">D</div><div class="item">E</div></div>
</body>
</html>
Three items need 96px × 3 + 8px × 2 = 304px of content width. A 360px box has 342px inside its border and padding, so wrap groups A, B, C and D, E. wrap-reverse puts the first group below the second. nowrap keeps all five on one line that requires 512px and scrolls inside the box.
If the outer box width drops below 322px, three items no longer fit. At a 320px viewport the body margins leave at most 280px, so the groups are two, two, and one. The boxes use min-height so their height can grow with the extra lines. Tab to the nowrap area and use the arrow keys to reach the overflow. See CSS Flexbox · Breaking into lines.
Separate wrapping from shrinking
Add the following rule at the end of style. The nowrap line stays intact but its items shrink. In the 360px box, four 8px gaps leave 310px to split between five items, or 62px each. That result assumes equal base sizes and minimum sizes that allow shrinking. Remove the rule to restore the 96px items.
.nowrap .item {
flex-shrink: 1;
}
Different base sizes can lead to different reductions even with the same shrink factor. If long text enforces a larger minimum size, allow min-width: 0 where appropriate and choose how that text can break. For three equal tracks with two 8px gaps, subtract the gaps first with calc((100% - 16px) / 3) or use Grid. See CSS Flexbox · Resolving flexible lengths.
Wrap into columns and use the flex-flow shorthand
With column, a shortage of vertical space starts a new column. Append these rules to change only the wrap box. Removing borders and padding from 150px leaves 132px, enough for two 40px items and one 8px gap. Three would need 136px. The columns are A, B, then C, D, then E.
.wrap {
flex-direction: column;
height: 150px;
}
.wrap .item {
flex: 0 0 40px;
width: 96px;
}
Set height: auto in the exercise and the box can grow tall enough to keep one column. Remove the added rules to go back to row wrapping. To set direction and wrapping together, write both values, for example flex-flow: column wrap. Writing only flex-flow: wrap resets the omitted direction to its initial value row. See CSS Flexbox · flex-flow.




