EthicsComputerCourses
Join training +91 90532 10052

Flex Wrap

CSS · Lesson 62 of 240 Beginner 10 min read Updated

By the end of this lesson you will be able to decide when flex items should stay on one line and when they should wrap, predict where new lines appear, space wrapped items with gap, and spot the "lonely last row" problem before your users do. You will also know when to stop fighting Flexbox and switch to Grid.

flex-wrap was introduced in CSS Flexbox Introduction, and Align Items & Align Content showed how wrapped lines are spaced. This lesson goes deeper, using the product row and filter chips of TazaTokri, our imaginary grocery app from Pune.

What nowrap really does

The default is flex-wrap: nowrap: all items stay on a single line, whatever happens. When the line gets too narrow, the browser does two things in order:

  1. It shrinks the items. Every item has flex-shrink: 1 by default, so each gives up some width.
  2. When an item cannot shrink any further, the items overflow the container and stick out of its edge.

How far can an item shrink? Not below its minimum content size, which is usually the width of its longest word, because flex items have min-width: auto by default. Flex Grow, Shrink & Basis explains how to change that limit.

Think of passengers on a local train bench in Mumbai. When more people sit down, everyone squeezes up (shrinking). Once nobody can squeeze any more, someone ends up half off the bench (overflow). Wrapping is like opening a second bench: the extra passengers move to a new row.

Step 1: drag and watch

Each box below has a small handle at its bottom-right corner. Drag it to make the box narrower and watch the two rows behave differently.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>nowrap vs wrap</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }
    p { margin: 0.8rem 0 0.3rem; font-size: 0.9rem; }
    .frame {
      resize: horizontal;
      overflow: auto;
      width: 22rem;
      min-width: 6rem;
      max-width: 100%;
      padding: 0.5rem;
      border: 1px solid #98a2b3;
    }
    .chips {
      display: flex;
      gap: 0.5rem;
      outline: 2px dashed #f04438;
    }
    .chips span {
      padding: 0.35rem 0.8rem;
      background: #d1fadf;
      border-radius: 999px;
      white-space: nowrap;
    }
    .wrap { flex-wrap: wrap; }
  </style>
</head>
<body>
  <p><code>flex-wrap: nowrap</code> (default)</p>
  <div class="frame">
    <div class="chips">
      <span>Organic</span><span>Under ₹100</span><span>Fresh today</span><span>Local farms</span><span>Combo packs</span>
    </div>
  </div>
  <p><code>flex-wrap: wrap</code></p>
  <div class="frame">
    <div class="chips wrap">
      <span>Organic</span><span>Under ₹100</span><span>Fresh today</span><span>Local farms</span><span>Combo packs</span>
    </div>
  </div>
</body>
</html>

The red dashed outline is the flex container. In the first frame the chips spill out past it and the frame gets a scrollbar. We set white-space: nowrap on the chips so their text cannot break, which makes the overflow happen quickly. In the second frame the chips move onto new lines and nothing sticks out.

Is nowrap ever right? Yes, when overflow is the design: a sideways-scrolling strip of categories, as in the TazaTokri app's top bar. There you want one line, overflow-x: auto on the container, and flex: none on the items so they do not shrink.

How wrap builds lines

With flex-wrap: wrap, the browser fills the first line with items in source order. When the next item does not fit, it starts a new line below. Each line is then laid out on its own:

  • justify-content shares out the free space in each line separately.
  • flex-grow lets items grow only to fill their own line.
  • Lines stack along the cross axis, and align-content spaces them.

That second point is the heart of Flexbox. Items on different lines know nothing about each other. Item 2 on the first line and item 6 on the second line will not line up in a column, unless they happen to be the same size. Flexbox is a one-dimensional system: it thinks in lines, not in a table of rows and columns.

wrap-reverse

flex-wrap: wrap-reverse also wraps, but new lines are added above the first one instead of below. In terms of edges, cross-start and cross-end swap. Items on each line still run left to right; only the stacking of lines is flipped.

It is rarely needed. One case is a row of tags pinned to the bottom of a panel, where you want extra tags to appear above the first row. Remember the warning from Flex Direction: the visual order changes, but keyboard and screen reader order does not.

flex-flow: direction and wrapping together

flex-flow is a shorthand for flex-direction followed by flex-wrap. Its default is row nowrap.

CSS
.chips   { flex-flow: row wrap; }      /* the most common combination */
.shelf   { flex-flow: column wrap; }   /* columns that wrap sideways */
.reverse { flex-flow: row wrap-reverse; }

A wrapping column (column wrap) only wraps when the container has a limited height. Without a height or max-height, the column simply grows taller. Wrapping columns are uncommon; for newspaper-style columns of text, see CSS Multi-Column Layout.

gap with wrapped items

gap is the clean way to space wrapped items. It has two parts:

  • row-gap is the space between lines.
  • column-gap is the space between items on the same line.
CSS
.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 0.5rem; /* row-gap 0.75rem, column-gap 0.5rem */
}

The big advantage of gap is that it only goes between items. There is no extra space at the start of a line or after the last item, so every line lines up neatly with the container edges.

Flexible items that wrap

Wrapping becomes powerful when items can also grow. Give each product card a starting size and let it grow and shrink from there:

CSS
.product {
  flex: 1 1 10rem; /* grow, shrink, start from 10rem */
}

The browser fits as many 10rem cards on a line as it can, then stretches them to fill the line. On a phone you get one or two per line, on a laptop five or six, with no media queries. The meaning of the three numbers is covered in Flex Grow, Shrink & Basis.

The last-row problem

Here is the catch. Suppose TazaTokri shows seven products and five fit on each line. The first line has five cards; the second line has two. Because each line is laid out separately:

  • With flex: 1 1 10rem, the two cards on the last line grow to fill the whole line, so each is more than twice as wide as the cards above.
  • With fixed-width cards and justify-content: space-between, the two last cards go to the far edges with a huge hole between them.

Neither looks like a product grid. Flexbox is doing exactly what it promises; the problem is that you wanted rows and columns, a two-dimensional layout.

Step 2: the problem and the Grid fix

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Last row problem</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }
    p { margin: 1rem 0 0.3rem; font-size: 0.9rem; }
    .list { outline: 2px dashed #98a2b3; padding: 0.5rem; max-width: 36rem; }
    .card {
      padding: 0.8rem 0.5rem;
      background: #ecfdf3;
      border: 1px solid #12b76a;
      border-radius: 8px;
      text-align: center;
    }
    .flex { display: flex; flex-wrap: wrap; gap: 0.5rem; }
    .flex .card { flex: 1 1 8rem; }
    .grid {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr));
      gap: 0.5rem;
    }
  </style>
</head>
<body>
  <p>Flexbox: <code>flex-wrap: wrap</code> + <code>flex: 1 1 8rem</code></p>
  <div class="list flex">
    <div class="card">Onions</div><div class="card">Potatoes</div><div class="card">Bananas</div>
    <div class="card">Curd</div><div class="card">Atta 5 kg</div><div class="card">Ghee</div>
    <div class="card">Coriander</div>
  </div>
  <p>Grid: <code>repeat(auto-fill, minmax(8rem, 1fr))</code></p>
  <div class="list grid">
    <div class="card">Onions</div><div class="card">Potatoes</div><div class="card">Bananas</div>
    <div class="card">Curd</div><div class="card">Atta 5 kg</div><div class="card">Ghee</div>
    <div class="card">Coriander</div>
  </div>
</body>
</html>

In the Flexbox list, the leftover cards on the last line stretch wide. In the Grid list, every card is the same width and the last row simply has empty cells at the end. Grid works this way because it creates columns first and then drops items into cells, so every row shares the same column widths. You will learn this pattern properly in CSS Grid Introduction.

You may find Flexbox workarounds online, such as a max-width on each card, or empty "ghost" elements added at the end of the list. They work at some screen widths and break at others, and ghost elements add meaningless HTML. For a real grid of cards, use Grid.

Common mistakes

  • Leaving the default nowrap on content that can grow. A row of filters that fits today overflows when a new filter is added or the text is translated. Add flex-wrap: wrap.
  • Spacing wrapped items with margins. Margins add space at the edges and between lines unevenly. Use gap.
  • Expecting items on different lines to line up. Each flex line is separate. Use Grid for columns.
  • Building a product grid with space-between. The last row spreads out. Use Grid with auto-fill.
  • Using column wrap without a height. The column just grows taller and never wraps.

Interview questions

1. What happens to flex items that do not fit when flex-wrap is nowrap?

They first shrink, because flex-shrink defaults to 1. They cannot shrink below their minimum content size, because of min-width: auto, so after that they overflow the container. The line is never broken.

2. What does wrap-reverse change?

It wraps items onto multiple lines but stacks new lines in the opposite cross direction, so in a row the second line appears above the first. The order of items within each line is unchanged. Like other reversals, it does not change the DOM order used by keyboards and screen readers.

3. What is flex-flow?

It is a shorthand for flex-direction and flex-wrap, written in that order, for example flex-flow: row wrap. Its initial value is row nowrap.

4. Why does the last row of a wrapping flex layout often look wrong?

Each flex line distributes its own free space. Growing items on a short last line stretch to fill it, and space-between pushes the last items to the edges. Items on different lines do not share column widths, because Flexbox is one-dimensional.

5. How would you build a product card grid whose last row lines up with the rows above?

Use CSS Grid with grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr)) and gap. Grid defines shared column tracks, so every card in every row has the same width and an incomplete last row simply leaves empty cells.

Key points

  • nowrap (the default) shrinks items and then lets them overflow; wrap starts new lines.
  • Each flex line is laid out on its own: free space and growing happen per line.
  • wrap-reverse stacks new lines in the opposite direction, above the first in a row.
  • flex-flow combines flex-direction and flex-wrap, for example row wrap.
  • row-gap spaces lines and column-gap spaces items, with no extra space at the edges.
  • When items must line up in columns, the last-row problem is a sign to use Grid.

Exercise

Build two parts of the TazaTokri home page. First, a "Shop by category" strip that stays on one line and scrolls sideways on small screens: eight category links that never shrink. Second, a "Popular searches" box with ten search tags that wrap onto new lines, with 0.75rem between lines and 0.5rem between tags. Finally, write one sentence explaining whether you would use Flexbox or Grid for a list of twelve product cards, and why.

Show solution
HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>TazaTokri home</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }
    .categories { display: flex; flex-wrap: nowrap; gap: 0.5rem; overflow-x: auto; padding-block: 0.5rem; }
    .categories a { flex: none; padding: 0.4rem 0.9rem; background: #d1fadf; border-radius: 999px; }
    .searches { display: flex; flex-flow: row wrap; gap: 0.75rem 0.5rem; max-width: 28rem; }
    .searches a { padding: 0.3rem 0.7rem; border: 1px solid #12b76a; border-radius: 6px; }
  </style>
</head>
<body>
  <nav class="categories" aria-label="Categories">
    <a href="#">Fruits</a><a href="#">Vegetables</a><a href="#">Dairy</a><a href="#">Bakery</a>
    <a href="#">Snacks</a><a href="#">Beverages</a><a href="#">Staples</a><a href="#">Household</a>
  </nav>
  <h2>Popular searches</h2>
  <div class="searches">
    <a href="#">tomato</a><a href="#">paneer</a><a href="#">onion</a><a href="#">milk</a><a href="#">eggs</a>
    <a href="#">basmati</a><a href="#">toor dal</a><a href="#">bread</a><a href="#">curd</a><a href="#">mangoes</a>
  </div>
</body>
</html>

The category strip keeps nowrap, scrolls with overflow-x: auto, and flex: none stops the links from shrinking. The search tags use flex-flow: row wrap and a two-value gap. For twelve product cards, Grid is the better choice, because the cards should line up in columns and the last row should not stretch.

Quick Quiz

1. With the default flex-wrap: nowrap, what happens first when items are too wide for the container?

2. In a row with flex-wrap: wrap-reverse, where does the second line appear?

3. What does gap: 1rem 0.5rem set in a wrapping flex container?

4. Why do cards on the last line of a wrapping flex layout stretch wider than the cards above?

Learn CSS with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call