CSS flex-wrap Property

The flex-wrap property controls whether flex items remain on a single line or can wrap onto multiple lines when there is insufficient space in a flex container.

By default, flex items are placed on one line. Setting flex-wrap: wrap allows additional lines to form, making it useful for responsive navigation, card groups, toolbars, and other flexible layouts. The property is applied to the flex container.

flex-wrap Syntax

The flex-wrap property accepts one of three keywords that determine whether flex items remain on a single line or are distributed across multiple lines.

Basic Syntax

selector {
  flex-wrap: wrap;
}

Available Values

.container-one {
  flex-wrap: nowrap;
}
.container-two {
  flex-wrap: wrap;
}
.container-three {
  flex-wrap: wrap-reverse;
}

CSS-Wide Keywords

.container {
  flex-wrap: inherit;
}
.container {
  flex-wrap: initial;
}
.container {
  flex-wrap: unset;
}
.container {
  flex-wrap: revert;
}
.container {
  flex-wrap: revert-layer;
}

The initial value is nowrap. The property has an effect when applied to a flex container created with display: flex or display: inline-flex.

Property Values

The three primary values determine how flex lines are created and arranged along the cross axis.

Value Description
nowrap Places all flex items on a single flex line. Items may shrink or overflow depending on their sizing constraints. This is the initial value.
wrap Allows flex items to form additional flex lines when necessary. Lines progress in the normal cross-axis direction.
wrap-reverse Allows multiple flex lines but reverses the cross-axis direction in which the lines are stacked.

CSS-Wide Values

Value Description
inherit Uses the computed flex-wrap value of the parent element.
initial Restores the initial value, nowrap.
unset Restores nowrap because the property is not inherited.
revert Reverts the property according to the CSS cascade origins.
revert-layer Reverts the property according to the CSS cascade layers.

Property Characteristics

Characteristic Value
Initial value nowrap
Inherited No
Applies to Flex containers
Computed value As specified
Animation type Discrete
Percentages Not applicable

Because the property is animated discretely, transitions between its keyword values switch between wrapping behaviors rather than producing a gradual intermediate layout.

How It Works

Flexbox places items along the main axis of a flex container. The flex-wrap property determines whether those items must remain on one line or can be divided into multiple flex lines.

Step 1: Create a Flex Container

.container {
  display: flex;
  width: 400px;
}

Step 2: Assign Item Sizes

.item {
  flex: 0 0 150px;
}

Each item has a flex basis of 150px and is prevented from growing or shrinking through flexible sizing.

Step 3: Enable Wrapping

.container {
  display: flex;
  flex-wrap: wrap;
  width: 400px;
  gap: 10px;
}

Two 150px items can fit on the first line with a 10px gap. A third item moves to another line because it would exceed the available width.

Step 4: Reverse the Line Direction

.container {
  display: flex;
  flex-wrap: wrap-reverse;
}

The items can still form multiple lines, but those lines are stacked in the reverse cross-axis direction.

Important Distinction

Wrapping does not change the order of items within each flex line. The wrap-reverse value reverses line stacking, not the main-axis ordering of items.

nowrap

The nowrap value places all flex items on a single flex line, even when the combined preferred sizes exceed the available space.

Syntax

.container {
  display: flex;
  flex-wrap: nowrap;
}

Example

.container {
  display: flex;
  flex-wrap: nowrap;
  width: 400px;
  gap: 10px;
}
.item {
  flex: 0 1 180px;
  min-width: 0;
}

How It Works

All items remain on one line. When their combined flex bases exceed the available width, the items can shrink because flex-shrink is set to 1.

Preventing Shrinking

.item {
  flex: 0 0 180px;
}

With shrinking disabled, the items retain their assigned main-axis sizes. If the container is too narrow, they may overflow.

When to Use nowrap

Use nowrap when a layout must remain on a single line, such as a compact toolbar, a horizontal control group, or a row intended for horizontal scrolling.

Important Consideration

The nowrap value does not guarantee that all content will fit. Flex items may shrink, overflow, or be constrained by their minimum sizes.

wrap

The wrap value allows flex items to move onto additional flex lines when they cannot all fit on the current line.

Syntax

.container {
  display: flex;
  flex-wrap: wrap;
}

Example

.container {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
}
.item {
  flex: 0 0 180px;
}

How It Works

Items are placed along the main axis until the next item would exceed the available line space. That item begins another flex line.

Flexible Wrapped Items

.container {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
}
.item {
  flex: 1 1 180px;
  min-width: 0;
}

Items can grow to fill their flex lines and shrink when permitted. The 180px flex basis helps determine when additional lines are formed.

When to Use wrap

Use wrap for responsive card groups, navigation links, tag collections, button groups, and other layouts that should adapt naturally to the available width.

Important Consideration

Each flex line is sized independently. Items on the final line may become wider than items on earlier lines when positive free space is distributed.

wrap-reverse

The wrap-reverse value allows multiple flex lines while reversing the cross-axis direction in which those lines are stacked.

Syntax

.container {
  display: flex;
  flex-wrap: wrap-reverse;
}

Example

.container {
  display: flex;
  flex-wrap: wrap-reverse;
  width: 400px;
  gap: 10px;
}
.item {
  flex: 0 0 180px;
}

How It Works

Items form flex lines using the same line-breaking behavior as wrap. The difference is that the cross-start and cross-end directions are reversed.

Comparing wrap and wrap-reverse

In a typical left-to-right, horizontal writing mode with flex-direction: row, wrap stacks additional lines downward, while wrap-reverse stacks them upward.

When to Use wrap-reverse

Use wrap-reverse when the design requires flex lines to accumulate in the opposite cross-axis direction.

Important Consideration

The value does not reverse the source order of items. It changes line stacking, and the visual result also depends on flex-direction and the writing mode.

Relationship with flex-direction

The flex-direction property establishes the main axis, while flex-wrap determines whether items can form multiple lines along the cross axis.

Row with Wrapping

.container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

In a horizontal writing mode, items are arranged horizontally and additional lines are stacked vertically.

Column with Wrapping

.container {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: 300px;
}

Items are arranged vertically. When the available height cannot accommodate another item, a new column can form.

Direction and Wrapping Comparison

flex-direction flex-wrap Typical Result
row wrap Horizontal items with additional rows.
row-reverse wrap Reversed horizontal direction with additional rows.
column wrap Vertical items with additional columns.
column-reverse wrap-reverse Reversed vertical direction and reversed column stacking.

Important Consideration

For column wrapping, the container generally needs a constrained main-axis size, such as a specified height in a horizontal writing mode. Otherwise, the container may expand to accommodate the items without forming additional columns.

flex-flow Shorthand

The flex-flow shorthand combines flex-direction and flex-wrap in a single declaration.

Individual Properties

.container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

Equivalent Shorthand

.container {
  display: flex;
  flex-flow: row wrap;
}

Common Combinations

Declaration Meaning
flex-flow: row nowrap Horizontal main axis with a single flex line.
flex-flow: row wrap Horizontal main axis with wrapping enabled.
flex-flow: row wrap-reverse Horizontal main axis with reversed line stacking.
flex-flow: column wrap Vertical main axis with additional columns when necessary.
flex-flow: column-reverse wrap Reversed vertical main axis with wrapping enabled.

Important Consideration

The flex-flow shorthand applies to flex containers. It is different from the flex shorthand, which controls the growth, shrinking, and basis of individual flex items.

Wrapping and Flex Sizing

Wrapping works with flex-basis, flex-grow, and flex-shrink to determine how items are distributed across lines.

Using flex-basis

.container {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
}
.item {
  flex-basis: 200px;
}

The flex basis contributes to determining whether an item fits on the current line.

Using flex-grow

.item {
  flex: 1 1 200px;
}

Items can grow to fill available space on their respective flex lines. A line with fewer items may produce wider items.

Using flex-shrink

.item {
  flex: 0 1 200px;
  min-width: 0;
}

Items can shrink when necessary, but wrapping decisions are made before flexible shrinking distributes negative free space within each line.

Independent Flex Lines

Each line resolves its own flexible sizing. The items on one line do not share positive or negative free space with items on another line.

Important Consideration

Wrapping is not a substitute for setting appropriate item sizes. Flex bases, minimum sizes, and available container space all influence where line breaks occur.

Aligning Multiple Flex Lines

The align-content property controls how flex lines are distributed along the cross axis when there is extra cross-axis space.

Centering Flex Lines

.container {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  height: 300px;
}

When multiple lines exist and extra cross-axis space is available, the lines are grouped toward the center of the container.

Spacing Flex Lines

.container {
  display: flex;
  flex-wrap: wrap;
  align-content: space-between;
  height: 300px;
}

The first and last lines are positioned toward opposite cross-axis edges, with remaining space distributed between the lines.

Stretching Flex Lines

.container {
  display: flex;
  flex-wrap: wrap;
  align-content: stretch;
  height: 300px;
}

With the default stretch behavior, flex lines can expand to use extra cross-axis space.

align-items vs. align-content

Property Purpose
align-items Controls the cross-axis alignment of items within their flex lines.
align-content Controls the distribution of flex lines within the container.

Important Consideration

In Flexbox, align-content has no effect on a single-line flex container. Multiple lines and available cross-axis space are generally needed for its distribution behavior to be visible.

Spacing Between Wrapped Items

The gap property provides spacing between flex items and between flex lines without requiring margins on individual items.

Uniform Spacing

.container {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
}

The items and lines receive consistent spacing.

Different Row and Column Gaps

.container {
  display: flex;
  flex-wrap: wrap;
  row-gap: 20px;
  column-gap: 10px;
}

For a typical row-oriented layout, row-gap separates flex lines vertically, while column-gap separates items horizontally.

Using the gap Shorthand

.container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px 10px;
}

The first value sets the row gap, and the second sets the column gap.

Accounting for Gaps

Gaps consume available space. A group of items that fits without gaps may require additional flex lines after spacing is introduced.

Important Consideration

The physical direction of the gaps depends on the flex direction. In column-oriented layouts, the relationship between gaps, items, and flex lines differs from a typical row layout.

Responsive Layouts

Wrapping allows flex layouts to adapt to different container widths without requiring a separate media query for every possible number of items per row.

Responsive Card Group

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}
.card {
  flex: 1 1 220px;
  min-width: 0;
}

Cards can grow within each line and move onto additional lines as the available width decreases.

Wrapping Navigation

.navigation {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 20px;
}
.navigation a {
  flex: 0 0 auto;
}

Navigation links remain at their content-based widths and wrap when there is insufficient room.

Responsive Button Group

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.actions button {
  flex: 1 1 140px;
}

Buttons can expand to use available space and move onto additional lines when necessary.

Using a Media Query

@media (max-width: 600px) {
  .cards {
    flex-direction: column;
  }
  .card {
    flex: 1 1 auto;
  }
}

At narrower viewport widths, the cards can switch from a wrapping row layout to a single vertical column.

Important Consideration

Flexbox wraps items into independent lines rather than creating shared columns across all rows. For layouts requiring consistent column tracks, CSS Grid may be more appropriate.

flex-wrap Example

This example compares nowrap, wrap, and wrap-reverse. Each container contains five numbered items with the same flex basis.

HTML Example

<div class="wrap-demo">
  <h3>nowrap</h3>
  <div class="wrap-nowrap">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
  </div>
  <h3>wrap</h3>
  <div class="wrap-normal">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
  </div>
  <h3>wrap-reverse</h3>
  <div class="wrap-reversed">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
  </div>
</div>

CSS Example

.wrap-demo > div {
  display: flex;
  box-sizing: border-box;
  gap: 8px;
  width: 100%;
  max-width: 340px;
  min-height: 150px;
  padding: 10px;
  margin-bottom: 20px;
  background-color: #f1f5f9;
  align-content: flex-start;
}
.wrap-demo > div > div {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 100px;
  height: 45px;
  background-color: #336699;
  color: #fff;
  font-weight: bold;
}
.wrap-demo .wrap-nowrap {
  flex-wrap: nowrap;
  overflow-x: auto;
}
.wrap-demo .wrap-normal {
  flex-wrap: wrap;
}
.wrap-demo .wrap-reversed {
  flex-wrap: wrap-reverse;
  align-content: flex-start;
}
Browser Result

nowrap

1
2
3
4
5

wrap

1
2
3
4
5

wrap-reverse

1
2
3
4
5

The nowrap container keeps all five items on one line and provides horizontal scrolling for the overflow. The wrap container creates additional lines below the first line. The wrap-reverse container reverses the cross-axis stacking direction, placing subsequent lines above earlier lines in this horizontal writing-mode example.

The item order within each line remains unchanged. Only the wrapping behavior and line stacking differ.

Common Mistakes

Several common misunderstandings can lead to unexpected wrapping behavior.

Applying flex-wrap to Flex Items

.container {
  display: flex;
  flex-wrap: wrap;
}

Apply flex-wrap to the flex container, not to the individual items you want to wrap.

Expecting nowrap to Prevent Overflow

The nowrap value keeps items on one line. It does not guarantee that they fit within the container.

Confusing wrap-reverse with row-reverse

wrap-reverse reverses the cross-axis line stacking direction. row-reverse reverses the main-axis direction.

Expecting Equal Columns Across Lines

Each flex line is sized independently. Flexbox does not automatically align items into shared column tracks across multiple lines.

Ignoring Gaps

Gaps consume available space and can cause items to wrap sooner than expected.

Using Column Wrapping Without a Height Constraint

A column flex container may continue expanding vertically instead of creating new columns when its main-axis size is unconstrained.

Expecting align-content to Affect a Single Line

In Flexbox, align-content has no effect on a single-line container. It distributes flex lines when multiple lines and suitable cross-axis space exist.

Important Considerations

Keep the following points in mind when using flex-wrap:

  • Purpose: Controls whether flex items remain on one line or can form multiple lines.
  • Initial value: nowrap.
  • Inheritance: The property is not inherited.
  • Applies to: Flex containers.
  • Accepted values: nowrap, wrap, and wrap-reverse.
  • nowrap: Keeps all flex items on a single line.
  • wrap: Allows additional flex lines to form.
  • wrap-reverse: Reverses the cross-axis direction of line stacking.
  • Main axis: Established by flex-direction.
  • Cross axis: Determines the direction in which additional lines are stacked.
  • flex-flow: Combines flex-direction and flex-wrap.
  • flex-basis: Influences line-breaking decisions.
  • flex-grow: Distributes positive free space separately within each flex line.
  • flex-shrink: Controls shrinking within each flex line.
  • align-content: Distributes multiple flex lines along the cross axis.
  • align-items: Controls cross-axis alignment of items within their lines.
  • gap: Provides spacing between items and lines.
  • Minimum sizes: Can affect how items fit and wrap.
  • Column wrapping: Generally requires a constrained main-axis size.
  • Independent lines: Each flex line resolves its own flexible sizing.
  • Source order: Wrapping does not change the document order.
  • Animation: Keyword changes are discrete.
  • Responsive layouts: Wrapping helps items adapt to changing container sizes.

Use flex-wrap: wrap when flex items should move onto additional lines instead of remaining in one continuous row or column. Combine it with appropriate flex bases, gaps, and alignment properties for predictable responsive layouts.

Browser Support

The CSS flex-wrap property is Widely available and supported by modern browsers, including Chrome, Edge, Firefox, and Safari.

Standard, unprefixed Flexbox wrapping has broad support. Older browsers may have used prefixed or earlier Flexbox implementations with different capabilities.

Supported Example

.container {
  display: flex;
  flex-wrap: wrap;
}

Feature Detection

@supports (display: flex) and (flex-wrap: wrap) {
  .container {
    display: flex;
    flex-wrap: wrap;
  }
}

Fallback Example

.container > div {
  display: block;
}
@supports (display: flex) and (flex-wrap: wrap) {
  .container {
    display: flex;
    flex-wrap: wrap;
  }
  .container > div {
    display: block;
  }
}

Without Flexbox support, the items remain in normal block layout. Supporting browsers arrange the items using Flexbox and allow additional lines to form.

Compatibility Considerations

Although support is broad, test complex layouts involving nested flex containers, column wrapping, intrinsic item sizes, and cross-axis alignment when precise behavior is important.

For current compatibility information, visit Can I Use? .

Specifications

The flex-wrap property is defined in the CSS Flexible Box Layout Module Level 1. The specification establishes the available wrapping modes and how flex lines are stacked along the cross axis.

CSS Flexible Box Layout Module Level 1: flex-wrap Property

The specification also defines how items are collected into flex lines before flexible lengths are resolved.

CSS Flexible Box Layout Module Level 1: Collect Flex Items into Flex Lines

These rules explain the relationship between flex wrapping, item sizing, available main-axis space, and the creation of additional flex lines.