Flex Wrap

Control how flex items flow onto multiple lines with CSS flexbox wrapping. Build responsive layouts that adapt gracefully to any screen size.

Understanding the flex-wrap Property

Flexbox revolutionized CSS layouts by providing a powerful one-dimensional system for arranging elements. But what happens when your flex container has more items than can fit on a single line? This is where the flex-wrap property becomes essential.

The flexbox model was designed as a single-dimensional layout system -- it controls content along either a row OR a column, but not both simultaneously. By default, flex items are forced onto a single line using flex-wrap: nowrap, which can cause content to overflow the container when space runs out.

The flex-wrap property gives developers fine-grained control over how flex items behave when they exceed their container's available space. When set to wrap or wrap-reverse, items flow onto new lines, creating flexible, responsive layouts that adapt gracefully to different screen sizes and content volumes.

Understanding how flex-wrap interacts with the flex property is crucial for creating sophisticated layouts. While flex controls individual item behavior (grow, shrink, basis), flex-wrap controls the overall container behavior when items overflow.

This behavior is fundamental to building modern responsive web designs that provide optimal experiences across desktops, tablets, and mobile devices without relying solely on media queries for every layout adjustment.

The Three Values

The flex-wrap property accepts three values that determine exactly how flex items behave when they overflow their container.

nowrap (Default)

The default behavior where flex items are forced onto a single line regardless of available space. This causes items to shrink below their ideal size and can ultimately result in content overflowing the container, making it essential to understand this default when building responsive layouts.

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

wrap

When the container runs out of horizontal space (in row direction) or vertical space (in column direction), items wrap onto new lines. For flex-direction: row, items flow from left to right, wrapping into new rows below. This creates a natural top-to-bottom stacking of content that maintains readability and prevents horizontal scrolling.

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

wrap-reverse

Similar to wrap, but items flow in the opposite direction. For rows, content starts from the bottom and wraps upward. For columns, items flow from right to left. This value is useful for specific design patterns where bottom-aligned content or reverse ordering is required.

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

For smooth transitions between wrapping states, consider combining flex-wrap with transition properties for animated layout changes. MDN Web Docs provides comprehensive documentation on each value with interactive examples demonstrating the wrapping behavior.

Basic Flex Wrap Example
1.container {2 display: flex;3 flex-wrap: wrap;4 gap: 1rem;5}6 7.item {8 flex: 1 1 200px;9}

How Wrapping Works with flex-direction

The interaction between flex-wrap and flex-direction determines exactly how items wrap and stack within your layout. The flex-wrap property sets the direction in which lines are stacked -- either horizontally or vertically based on the current flex direction.

  • row + wrap: Items flow horizontally from left to right, wrapping into new rows below the existing content. This is the most common combination for building responsive grid-like layouts.

  • row-reverse + wrap: Items start from the right edge of the container and flow leftward, wrapping into new rows below when space runs out.

  • column + wrap: Items flow vertically from top to bottom, wrapping into new columns to the right when vertical space is exhausted.

  • columnreverse + wrap: Items start from the bottom of the container and flow upward, wrapping into new columns to the right.

Single-Dimensional vs Two-Dimensional Layouts

When flex items wrap, each flex line (row or column) acts as an independent flex container. This means alignment properties like justify-content and align-items only apply within each individual line, not across all lines simultaneously. MDN's guide on wrapping flex items explains this behavior in detail.

For layouts requiring strict control in both dimensions -- where you need precise alignment across rows AND columns -- CSS Grid is the appropriate tool. Flexbox with flex-wrap excels at one-dimensional content that flows naturally, while Grid provides two-dimensional control where both axes are managed simultaneously.

You can also create engaging interactive experiences by combining flex-wrap with hover effects to highlight wrapped items or create dynamic reordering animations.

The key distinction: use flex-wrap when your content has a natural flow and order; use Grid when you need strict, aligned positioning in both directions.

Common Use Cases

Responsive Card Grids

Flex-wrap creates fluid galleries that adjust column count based on viewport width. Combined with flex-basis and flex-grow, cards resize proportionally before wrapping, maintaining visual harmony without media queries.

.card-grid {
 display: flex;
 flex-wrap: wrap;
 gap: 20px;
 padding: 20px;
}

.card {
 flex: 0 1 calc(25% - 20px);
 min-width: 250px;
}

Navigation Menus

When used with flex-wrap: wrap, navigation menus wrap gracefully on smaller screens. Links stack vertically while maintaining logical grouping and touch-friendly targets. Combined with transition, you can animate between wrapped and unwrapped states for smooth user experiences.

.nav-menu {
 display: flex;
 flex-wrap: wrap;
 gap: 1rem;
}

Product Listings

E-commerce product grids benefit from flex-wrap, allowing products to flow naturally across screen sizes. Combined with the gap property, products maintain consistent spacing regardless of how many items appear per row.

Dashboard Widgets

Dashboard layouts with varying widget sizes use flex-wrap for organic arrangements that respond to available space. Widgets flow into natural positions without requiring rigid grid specifications.

Preventing Unwanted Wrapping

For navigation toolbars or horizontal scroll areas, use flex-wrap: nowrap combined with overflow-x: auto to create horizontal scrolling instead of wrapping.

.toolbar {
 display: flex;
 flex-wrap: nowrap;
 overflow-x: auto;
}

These patterns demonstrate why flex-wrap is essential for modern responsive web development, enabling fluid layouts that adapt naturally to any device.

Browser Support & Best Practices

Everything you need to know for production use

Universal Browser Support

The flex-wrap property is Baseline: Widely available since September 2015. Works in all modern browsers including Chrome, Firefox, Safari, and Edge without requiring vendor prefixes.

Use the Gap Property

Replace margins with gap for consistent spacing between wrapped items. This eliminates the need for complex margin calculations and works seamlessly with wrapping behavior.

Combine with Flex Shorthand

Use `flex: 1 1 auto` or `flex: 0 1 [value]` for predictable item sizing before wrapping occurs. The flex shorthand controls grow, shrink, and basis properties together.

Test at Breakpoints

Verify wrapping behavior at typical device widths including mobile (375px), tablet (768px), and desktop (1024px+) to ensure content remains readable and accessible.

Frequently Asked Questions

Build Modern Responsive Layouts

Our team of expert developers creates flexible, performant web interfaces using the latest CSS layout techniques including flexbox, Grid, and responsive design patterns.

Sources

  1. MDN Web Docs: Wrapping Flex Items - Comprehensive guide explaining flexbox wrapping behavior, when to use flexbox vs CSS Grid, and practical examples with code demonstrations

  2. MDN Web Docs: flex-wrap Property Reference - Official CSS property reference with formal definitions, all three values, and browser compatibility information

Flex Wrap CSS Property Guide | Digital Thrive | Digital Thrive Canada