Course contentsShow
← Web Development
Lesson 260 of 2,9825. FlexboxFree lesson

flex-wrap: Allowing Items to Wrap

Controlling whether flex items stay on one line or wrap to multiple lines with wrap and wrap-reverse.

flex-wrap: Allowing Items to Wrap

What you'll learn: How to control whether flex items stay squeezed on one line or wrap onto multiple lines when space runs out.

By default, flex containers try to fit all items on a single line along the main axis, even if it means shrinking them or causing overflow. The flex-wrap property gives you control over this behavior, letting items wrap to new lines when they don't fit comfortably.

The Three flex-wrap Values

flex-wrap: nowrap (default)
All flex items stay on one line, no matter what. Items will shrink or overflow rather than wrap.

flex-wrap: wrap
Items wrap to multiple lines if needed, creating additional rows (when flex-direction: row) or columns (when flex-direction: column). New lines appear below existing ones for rows, or to the right for columns.

flex-wrap: wrap-reverse
Items wrap just like wrap, but new lines appear in the opposite direction—above for rows, or to the left for columns.

When to Use Wrapping

Think of a bookshelf: with nowrap, you're cramming all books onto one shelf even if they don't fit. With wrap, you add new shelves as needed. This is perfect for responsive layouts like image galleries, tag lists, or navigation menus that need to adapt to different screen sizes.

.container {
  display: flex;
  flex-wrap: wrap; /* Items can flow to multiple lines */
}

When items wrap, each line becomes its own mini flex container. justify-content applies within each line, while align-items affects items in each line individually.

Key Takeaway: Use flex-wrap: wrap to let flex items break onto multiple lines when they don't fit, creating flexible, responsive layouts that adapt to container width rather than forcing everything onto one line.