flex-wrap: Controlling Line Breaking
You'll learn how to allow flex items to wrap onto multiple lines when they can't all fit in one row or column.
What Is Line Breaking in Flexbox?
By default, flex items try to squeeze onto a single line, even if it means shrinking or overflowing the container. The flex-wrap property changes this behavior by allowing items to "wrap" onto additional lines when the container runs out of space—just like words wrap to a new line when they reach the edge of a page.
The flex-wrap Values
The flex-wrap property accepts three values:
nowrap(default) — All items stay on one line, no matter whatwrap— Items wrap onto new lines if needed, moving downward (or rightward in column layouts)wrap-reverse— Items wrap, but new lines appear in the opposite direction
How It Works
.container {
display: flex;
flex-wrap: wrap;
}
With flex-wrap: wrap, when flex items exceed the container's width (or height in column layouts), they automatically break onto a new line instead of overflowing or shrinking uncomfortably.
Think of it like packing boxes on a shelf: with nowrap, you'd try to cram all boxes on one shelf. With wrap, you'd add a second shelf below when the first is full.
When to Use flex-wrap
Use wrap when you have a variable number of items that should flow naturally into multiple rows or columns. Common examples include image galleries, tag lists, or navigation items that should adapt to different screen sizes.
Key Takeaway: flex-wrap: wrap allows flex items to break onto multiple lines when they exceed the container's space, creating flexible, responsive layouts that adapt to content and screen size.