CSS3 Beginner Modern Layout · 7 min read

CSS Flex Wrap and Gap for Wrapping Toolbars

Lesson 7 of 41 in the CSS3 learning path.

How do I let flex items wrap without float hacks?

A flex line does not wrap unless you say so. The initial flex-wrap is nowrap, so items stay on one line and either shrink or spill out of the parent. flex-wrap: wrap lets each item keep its width and move to the next line when the row runs out of room, which is how a toolbar of filters survives a narrow phone without float or inline-block hacks. gap still applies between items on a line and between the lines, using the two-value form row-gap then column-gap.

The sample is a lesson toolbar. Chips use flex: 0 0 auto so they stay content-sized, and the search field uses a basis of 12rem with shrink and grow enabled, plus a min-width so it can sit on its own line. align-content spreads extra cross-axis space when there is more than one line. Resize the window and the chips wrap as whole controls. Nothing is floated, and nothing uses a percentage width hack.

Syntax & Parameters

Syntax
.toolbar { display: flex; flex-wrap: wrap; align-items: center; align-content: space-between; gap: 0.5rem 0.75rem; }
.chip { flex: 0 0 auto; }

Example

Interactive preview available
styles.css
*, *::before, *::after { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Segoe UI", system-ui, sans-serif;
  line-height: 1.45;
  color: #1c2430;
  background: #f4f1ea;
}
.page { max-width: 36rem; margin-inline: auto; padding: 1.1rem; }
h1 { margin: 0 0 0.3rem; font-size: 1.4rem; }
.lead { margin: 0 0 0.85rem; color: #5c6b7a; }
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  align-content: space-between;
  gap: 0.5rem 0.75rem;
  min-height: 8rem;
  padding: 0.75rem;
  background: #fff;
  border: 1px solid #e3ddd2;
  border-radius: 12px;
}
.toolbar-label {
  flex: 0 0 auto;
  color: #0f766e;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.chip {
  flex: 0 0 auto;
  padding: 0.35rem 0.7rem;
  border: 0;
  border-radius: 999px;
  background: #f4f1ea;
  color: #1c2430;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
}
.chip-on { background: #0f766e; color: #f8fafc; }
.search {
  flex: 1 1 12rem;
  min-width: 10rem;
  padding: 0.4rem 0.6rem;
  border: 1px solid #d6d3d1;
  border-radius: 8px;
  font: inherit;
}
.clear {
  flex: 0 0 auto;
  margin-left: auto;
  border: 0;
  background: transparent;
  color: #0f766e;
  font: inherit;
  font-weight: 600;
}
.note { margin: 0.7rem 0 0; color: #5c6b7a; font-size: 0.85rem; }

Expected Output

The toolbar is a white rounded panel, 8rem tall, inside a column no wider than 36rem. The uppercase Filters label, several chips, a search field, and a Clear action share that panel. Because the column is narrower than the sum of the controls, the chips and the field occupy two lines. The lines are pushed to the top and bottom of the 8rem panel by align-content: space-between.

Gaps are 0.75rem between items on a line and 0.5rem between the lines. The active chip is teal. Clear sits at the right edge of whichever line it lands on. Narrow the frame further and another line appears. Nothing hangs out of the panel the way a nowrap row would.

Line-by-Line Walkthrough

  1. flex-wrap: wrap is the whole trick. The initial value is nowrap, which keeps every item on one line even when the sum of their sizes exceeds the parent.
  2. display: flex is still required. Wrap does nothing on a block container, and it does not revive float layout.
  3. gap: 0.5rem 0.75rem sets the row gap first and the column gap second, so wrapped lines are 0.5rem apart and neighbors on a line are 0.75rem apart.
  4. align-items: center aligns items inside each line, on the cross axis. It does not move the lines apart from each other.
  5. align-content: space-between distributes extra space between the lines. The min-height of 8rem gives the toolbar room to show that, once more than one line exists.
  6. .chip uses flex: 0 0 auto, so a chip stays as wide as its label, neither growing nor shrinking. The whole control moves to the next line.
  7. .search uses flex: 1 1 12rem and min-width: 10rem. It may grow, it may shrink down to 10rem, and then it wraps instead of crushing the chips.
  8. margin-left: auto on Clear consumes free space on its line and pins that button to the end. When it wraps, it sits at the start of the new line and auto margin still pushes it right if the line has room.
  9. Nothing in the toolbar is floated, and nothing is display: inline-block. Those older hacks needed clearfix and did not have gap.
  10. The page column is capped at 36rem so the wrap is visible without a tiny window. On a real nav you would let the toolbar use the full bar and wrap only when that bar gets narrow.

Chips wrap as whole buttons. The search field wraps as a whole field. Text inside a chip does not break onto its own line.

Common Pitfalls & Security

Wrap only starts when the container is too narrow for the items at their flex base. A toolbar that is still one line has not failed. It just has room.

  • Leaving flex-wrap at nowrap and setting a percentage width on each chip is the old float habit. The chip text then wraps inside the chip instead of the chip moving as a unit.
  • align-content has no visible effect on a single line. Give the container a cross size, or do not expect the one line to jump to the top and bottom.
  • gap plus margin on the same items double the space. Zero the item margins when you add gap.
  • flex: 1 1 0 on a chip makes it grow and share the line, so you no longer have a content-sized control that wraps cleanly. Keep chips at 0 0 auto.
  • A nowrap flex row inside a narrow sidebar can cover a neighboring button. Wrapping, or min-width: 0 plus shrink, keeps the control reachable.

Best Practices & Modern Standards

Use flex-wrap and gap for any row of controls that must stay usable on a phone: filters, tags, toolbar actions, and pagination. Size the items with flex basis and min-width, not with floats or inline-block. Keep align-items for alignment inside a line and align-content for alignment of the lines as a group.

When the wrapping should follow the component width rather than the viewport, put container-type: inline-size on the toolbar and adjust the basis inside a container query. A card gallery that should reflow into equal columns is a better fit for grid auto-fit than for flex wrap. Custom properties can hold the gap and the search basis so a dense variant overrides two tokens.

Real-World Use

A lesson library toolbar holds a Filters label, subject chips, a search field, and a Clear action. On a desktop bar they sit on one line. On a phone, or in a 36rem column, the chips wrap as whole pills and the search field drops with them instead of overflowing.

The same stylesheet fits a tag editor under a post, a set of applied-discount chips on a checkout, or the action row in a table header. You do not write a float clearfix for any of those.

Frequently Asked Questions

Why are my flex items still on one line?

The initial flex-wrap is nowrap, so items shrink or overflow instead of moving to the next line. Set flex-wrap: wrap on the container, not on the items. If they still refuse to wrap, check for a large flex-basis or a min-width that is wider than the container.

What is the difference between align-items and align-content?

align-items positions items inside a single line, along the cross axis. align-content positions the lines themselves when there is extra cross-axis space and more than one line. A toolbar with one line will not show align-content. Give it a min-height, or let it wrap, before you judge that property.

How do I write a two-value gap?

gap: 0.5rem 0.75rem means row-gap then column-gap. The first number is the space between wrapped lines, and the second is the space between items on one line. A single value sets both. Gap is not a margin, so it does not collapse and it does not appear outside the first item.

Should a wrapping toolbar use Flexbox or Grid?

Use Flexbox when the controls have different widths and should wrap as a group, the way chips do. Use Grid with auto-fit when you want equal columns that reflow, the way a card gallery does. A toolbar that mixes a growing search field and fixed chips is a flex job.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…