Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Flexbox

The flex-wrap property

Reference for the CSS flex-wrap property: single-line versus multi-line flex containers, wrap and wrap-reverse, and how wrapped lines are aligned.

The flex-wrap property decides whether a flex container keeps all its items on one line or lets them break onto multiple lines when they do not fit. By default (nowrap) the items stay on one line and, if necessary, shrink or overflow.

With wrap, an item that does not fit moves to the next line. Each line then behaves as its own small flex container: free space is shared per line, not across the whole container.

CSS
.gallery { display: flex; flex-wrap: wrap; gap: 8px; }

Try it

nowrap, wrap and wrap-reverse
A wrapping tag list

Syntax

CSS
flex-wrap: nowrap | wrap | wrap-reverse;
  • Initial value: nowrap
  • Applies to: flex containers
  • Inherited: no
  • Animatable: no (discrete)
  • Computed value: as specified

Values

Value Description
nowrap Single-line container. Items never move to a new line; they shrink, then overflow.
wrap Multi-line container. New lines are added in the cross direction (below for rows, to the right for columns in left-to-right text).
wrap-reverse Multi-line, but the cross-start and cross-end are swapped, so new lines are added above (rows) or to the left (columns). Items within a line keep their order.

Usage notes

  • Lines do not align as a grid. Each line distributes its own free space, so wrapped items in different lines can end up with different widths and will not line up in columns. If you need a grid, use display: grid.
  • Use gap, not margins. gap puts space between items and lines without extra space at the edges.
  • flex-grow fills each line. With flex: 1 1 200px and wrap, items fill each line, and the leftover space on every line is shared out. This is a common "responsive without media queries" trick.
  • Aligning the lines. Spare cross-axis space between lines is handled by align-content, not by align-items, which aligns items inside their own line.
  • Columns need a height. flex-direction: column with wrap only wraps when the container has a limited height.
  • wrap-reverse is visual. It does not change the HTML or focus order.
  • Combine with flex-direction through flex-flow.

Browser support

Supported in every browser.