Loading

How do you wrap a flex?

The quickest way to wrap flex items is to apply the CSS property flex-wrap: wrap on the flex container. This lets items move onto additional lines as space runs out; flex-wrap: wrap-reverse can wrap in the opposite direction, and flex-flow provides a shorthand for common combinations.


What wrapping means in Flexbox


In a flex container, items are laid out along a main axis (row or column). Wrapping enables these items to spill onto new lines or columns when there isn’t enough space, producing multi-line layouts that adapt to different screen sizes. Without wrapping, items stay on a single line and may overflow or shrink excessively. Understanding wrapping is essential for responsive navigation bars, card grids, and fluid galleries.


Enable wrapping: the flex-wrap property


To enable wrapping and control how items wrap, start with the basics and then refine with shorthand and direction tricks.



  • Set the container to display: flex; to activate Flexbox on the parent element.

  • Apply flex-wrap: wrap; to allow items to wrap onto multiple lines as space becomes available.

  • Alternatively, use the shorthand flex-flow: row wrap; (or column wrap) to combine direction and wrapping in one declaration.

  • For wrapping in the opposite direction, use flex-wrap: wrap-reverse; which reverses the cross-line order.

  • Tune when wrapping occurs by sizing items with flex-basis, flex-grow, and min-width (for example, flex: 1 1 200px; lets items shrink or grow and wrap when they reach about 200px).


Wrapping behavior is most predictable when you pair a flexible item size with a wrapping container, and test across breakpoints to ensure lines form as intended.


Shorthand usage


Combining properties with a single declaration can simplify your CSS. Example: display: flex; flex-flow: row wrap; This sets a horizontal flow and allows wrapping onto new lines. You can switch to row wrap-reverse or column wrap as your layout demands.


Controlling alignment and distribution of wrapped lines


Once items are allowed to wrap, you’ll often want to control how lines are spaced and how items align within and across lines. The following properties determine the visual balance of your multi-line layout.


Before listing the controlling properties, keep in mind that alignment works differently for the main axis (the line direction) and the cross axis (perpendicular to the line). When there are multiple lines, extra space can exist along the cross axis, which these properties manage.



  • align-content: Distributes the wrapped lines along the cross axis when there is extra space. It can push lines to the start, end, center, or evenly space them.

  • justify-content: Aligns items within each line along the main axis (left, right, center, space-between, space-around, etc.).

  • align-items: Aligns items along the cross axis within their line (stretch by default, or align to start, center, end).

  • gap: Introduces consistent spacing between items, both row-wise and column-wise, which helps maintain even wrapping.

  • flex-direction: Determines the direction of item flow (row, row-reverse, column, column-reverse) and thus how wrapping is perceived.


Note that align-content only applies when there are multiple lines; if there is only a single line, its effect may be minimal.


Practical patterns and examples


Real-world layouts often rely on wrapping to stay legible and balanced across devices. Here are common patterns and how to implement them with wrapping.



  1. Responsive navigation bar: Use display: flex; flex-wrap: wrap; and set horizontal gaps. Allow the logo to stay on the first line while menu items wrap to a second line on narrow screens.

  2. Wrapping card grid: Use flex-wrap: wrap; and a flexible basis like flex: 1 1 240px; so cards reflow into additional rows as space permits.

  3. Fluid image gallery: Give items a flexible width and a consistent gap so images reflow gracefully into new rows while maintaining aspect ratios and spacing.


These patterns help maintain readability and visual balance as the viewport changes, without resorting to complex JavaScript calculations.


Browser compatibility and tips


Flexbox wrapping is widely supported across modern browsers, including Chrome, Firefox, Safari, and Edge. For very old browsers, especially legacy Internet Explorer versions, you may encounter inconsistencies or need alternative fallbacks. When supporting older environments, consider progressive enhancement and test with real devices.



  • In older IE versions (IE 10/11), you may need the -ms-flexbox properties or different syntax in addition to the standard API. Use fallbacks if you must support these browsers.

  • Test responsive behavior at multiple breakpoints to ensure wrapping occurs as intended and that content remains accessible.


Practical testing and careful sizing reduce the risk of awkward gaps or overflow in wrapped layouts.


Common pitfalls and debugging tips


Wrapping can introduce surprises when item sizing, margins, and padding interact with available space. Here are tips to avoid common issues.



  • Ensure min-width on items isn’t set too high; otherwise items may never wrap when you expect them to.

  • Use box-sizing: border-box so padding and borders are included in width calculations.

  • Be mindful of horizontal margins on flex items; large margins can prevent wrapping or create uneven lines.

  • Leverage the gap property for clean spacing; if the browser you target lacks gap support for flexbox, use margins as a fallback.

  • Use media queries to adjust flex-basis, min-width, or wrap behavior at key breakpoints for better control.


With these checks, you can diagnose why wrapping isn’t behaving as expected and adjust your layout strategy accordingly.


Summary


Wrapping is a fundamental capability of CSS Flexbox that enables responsive, multi-line layouts. By enabling wrapping with flex-wrap, sizing items with flex-basis and min-width, and fine-tuning alignment and spacing with align-content, justify-content, align-items, and gap, you can build flexible layouts that adapt from mobile to desktop. Practice across real-world patterns, test across devices, and consider progressive enhancement for environments without full Flexbox support.

How do I wrap flex items?


To make them wrap once they are too wide, add the flex-wrap property with a value of wrap , or use the shorthand flex-flow with values of row wrap or column wrap . Items will then wrap onto new lines when they overflow their container.



How to align flex items to center?


Using alignment in flexbox
To center an element, we use the align-items property to align the item on the cross axis, which in this case is the block axis running vertically. We use justify-content to align the item on the main axis, which in this case is the inline axis running horizontally.



What is meant by flex wrap?


flex-wrap is a CSS property that controls whether flex items are forced onto a single line or allowed to wrap onto multiple lines in a flex container. The default value, nowrap, keeps all items on a single line, shrinking them as needed until they reach their minimum size. Setting flex-wrap: wrap allows items to move to a new line when there isn't enough space, which is essential for creating responsive layouts. 
This video demonstrates the difference between flex-wrap: nowrap and flex-wrap: wrap: 1mScrimbaYouTube · Mar 16, 2022
How it works

  • flex-wrap: nowrap; (default): All flex items are forced onto a single line. If there isn't enough space, items will shrink to fit or overflow the container. 
  • flex-wrap: wrap;: Items are allowed to wrap to the next line if they don't fit on the current one. The direction the new lines are stacked is determined by the flex-direction property. 
  • flex-wrap: wrap-reverse;: Items wrap to new lines, but the lines are stacked in the reverse order. For example, the second line would appear above the first line. 

You can watch this video to learn about the flex-wrap: wrap-reverse property: 30sThe Code CreativeYouTube · Jul 8, 2020
Common use case
  • Responsive design: flex-wrap is crucial for making websites look good on different screen sizes. On large screens, items can be displayed side-by-side. As the screen shrinks, items wrap to new lines, preventing horizontal scrolling and maintaining a clean layout. 



How to make Flex go to next line?


The CSS flex-flow property
The value of the flex-direction property specifies the direction of the flex items and the value of the flex-wrap property allows flex items to move to the next line instead of shrinking to fit inside the flex container. The flex-flow property should be declared on the flex container.


Kevin's Auto

Kevin Bennett

Company Owner

Kevin Bennet is the founder and owner of Kevin's Autos, a leading automotive service provider in Australia. With a deep commitment to customer satisfaction and years of industry expertise, Kevin uses his blog to answer the most common questions posed by his customers. From maintenance tips to troubleshooting advice, Kevin's articles are designed to empower drivers with the knowledge they need to keep their vehicles running smoothly and safely.