Wrapping and gap

Topic 4 of 5 in Flexbox6 min readBeginnerFree course

Overview

By default, a flex container keeps all its items on one line, shrinking them to fit — and if they can’t shrink enough, they overflow. For a toolbar that’s often right. For tags, buttons, cards or anything whose number varies, you want items to wrap onto new lines when they run out of room.

On the left, flex-wrap: nowrap (the default): five items shrink, then overflow the container. On the right, flex-wrap: wrap with gap: 12px: items move to a new line when they don’t fit. gap sets the space between items, in rows and columns, without extra space at the edges. Each wrapped line is its own flex line — items don’t line up in columns. For that, use grid.

flex-wrap

CSS
.tags {
  display: flex;
  flex-wrap: wrap;
}
Value Behaviour
nowrap one line; items shrink, then overflow (default)
wrap items move onto new lines, top to bottom
wrap-reverse new lines are added above

The shorthand flex-flow combines direction and wrapping: flex-flow: row wrap.

When does an item wrap? The browser fills each line with items at their flex-basis (their starting size). When the next item’s basis doesn’t fit, it starts a new line. Then each line’s items grow or shrink to fill that line.

gap

gap sets the space between items and between lines:

CSS
.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;              /* same in both directions */
  /* gap: 1rem 0.5rem;      row-gap column-gap */
}

Before gap worked in flexbox, people used margins on items and negative margins on the container to cancel the extra space at the edges. You don’t need that any more — gap is supported everywhere and only adds space between items.

Responsive rows without media queries

Combining wrapping with a flexible basis gives you layouts that respond to their space automatically:

CSS
.features {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}
.features > * {
  flex: 1 1 14rem;   /* grow, shrink, start at 14rem */
}

How it behaves:

  • On a wide screen, three 14rem boxes fit on one line, and they grow to share the full width.
  • When the container gets narrow enough that the third box’s 14rem basis no longer fits, it wraps to the next line — and grows to fill that whole line.
  • On a phone, each box is on its own line, full width.

No breakpoints, and it works anywhere you put the component — a sidebar, a modal, a full-width page — because it responds to its container, not the viewport.

The last-line problem

Each wrapped line is laid out independently. With five items and three per line, the two items on the second line grow to fill it — so they’re wider than the three above. And items on different lines don’t line up into columns:

Text
┌──────┐┌──────┐┌──────┐
│  1   ││  2   ││  3   │
└──────┘└──────┘└──────┘
┌──────────┐┌──────────┐
│    4     ││    5     │
└──────────┘└──────────┘

Sometimes that’s exactly what you want (tags, buttons, a “fill the row” effect). When you want a true grid — equal columns, items aligned across rows — use CSS grid with repeat(auto-fit, minmax(…)), covered in auto-fit and minmax. Choosing between the two is the subject of Flexbox or grid?.

You can limit growth with max-width on items, or set flex-grow: 0 so items keep their basis size and leave space at the end of the last line.

The sidebar trick

A neat flexbox technique uses wrapping to make a sidebar layout that stacks by itself when there isn’t room — no media query:

CSS
.with-sidebar { display: flex; flex-wrap: wrap; gap: 1rem; }
.with-sidebar > .sidebar { flex: 1 1 15rem; }
.with-sidebar > .content { flex: 999 1 0; min-inline-size: 60%; }

The content’s huge grow factor means it takes nearly all the free space, and min-inline-size: 60% forces a wrap as soon as the content would be narrower than 60% of the container. You’ll build it in Sidebar layouts.

Wrapping and alignment

With multiple lines, align-content distributes the lines in the cross axis (when the container is taller than its lines), and justify-content aligns items within each line:

CSS
.buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;   /* each line's buttons are centred */
}

Rule of thumb: wrapping flex is perfect for content-sized things that flow like words — tags, chips, buttons, avatars — and for simple responsive rows. When items must line up in columns, switch to grid.

Exercise

Responsive tags and feature boxes

Make .tags a row of tag links that wraps onto new lines when needed, with 0.5rem between tags in both directions. Then make .features a row of feature boxes that sit three across on wide screens and stack one per line on narrow screens — with no media query. Each box should be at least 14rem wide and grow to fill the row.

HTML
<div class="tags">
  <a href="#">flexbox</a><a href="#">grid</a><a href="#">responsive</a>
  <a href="#">container queries</a><a href="#">accessibility</a><a href="#">anchor positioning</a>
</div>

<div class="features">
  <section>Live projects</section>
  <section>Certificates</section>
  <section>Mentoring</section>
</div>

<style>
  * { box-sizing: border-box; }
  .tags a { background: #eaf0fc; padding: 0.25rem 0.75rem; border-radius: 999px; }
  .features section { background: #f1ecfd; padding: 1.5rem; }
  /* your code here */
</style>

Summary

  • By default flex items stay on one line and shrink; flex-wrap: wrap lets them move to new lines instead.
  • gap, row-gap and column-gap add space between items and lines — never at the outer edges.
  • Combining flex-wrap: wrap with flex: 1 1 <min-size> creates responsive rows without media queries.
  • Each wrapped line is laid out independently, so items on different lines don’t align into columns.
  • A lone item on the last line grows to fill it; if you need aligned columns, use grid instead.