Performance: Flex wrap
Flex wrap affects how items flow and wrap in a flex container, impacting layout calculation and paint times.
Jump into concepts and practice - no test required
display: flex; flex-wrap: wrap;
display: flex; flex-wrap: nowrap;
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| flex-wrap: nowrap with many items causing overflow | Low | 1+ (due to overflow) | Medium (scrollbar repaint) | [X] Bad |
| flex-wrap: wrap with batch DOM updates | Low | 1 | Low | [OK] Good |
| flex-wrap: wrap adding items one by one | High | N (for N items) | High | [X] Bad |
flex-wrap: wrap; do in a flex container?flex-wrapflex-wrap property controls whether flex items stay in one line or wrap to new lines.wrap valuewrap allows items to move to the next line if they don't fit in one row, preventing overflow.flex-wrapnowrap, wrap, and wrap-reverse.flex-wrap: wrap;. Other options are invalid or incorrect.div.container {
display: flex;
flex-wrap: wrap;
width: 300px;
}
span.item {
width: 140px;
flex-shrink: 0;
height: 50px;
background: lightblue;
margin: 5px;
}<div class='container'>
<span class='item'>1</span>
<span class='item'>2</span>
<span class='item'>3</span>
</div>flex-wrap: wrap;div.box {
display: flex;
flex-wrap: wrap;
width: 200px;
}
span.box-item {
width: 150px;
height: 40px;
background-color: coral;
}wrap-reverse behaviorwrap-reverse makes wrapped lines stack in reverse order, with new lines above the first.reverse-wrap is invalid. Using flex-direction: row-reverse reverses item order horizontally but not line stacking. nowrap disables wrapping.