Skip to content
Codewilds

Lesson 20 of 29 · Layout with Flexbox

CSS flex-wrap and flex-grow: Rows That Wrap and Stretch

medium20 XP3 min read

In the last lesson every flex item kept its natural size, and all items stayed in one line. Real layouts need more: a search field that fills the space next to its button, or a row of cards that wraps onto a new line when the screen gets narrow. That's what flex-wrap and the flex-grow, flex-shrink and flex-basis properties are for.

Wrapping onto new lines

By default a flex container squeezes all its items into a single line (flex-wrap: nowrap), even when they get uncomfortably narrow. With flex-wrap: wrap, items that don't fit move to the next line:

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px; /* used between items and between lines */
}

How big is a flex item?

Three properties on the items control their size along the main axis:

  • flex-basis is the starting size, before any free space is shared out. In a row it works like a width: flex-basis: 200px. The default, auto, uses the item's width or its content.
  • flex-grow decides who gets the leftover space. It is 0 by default, so items don't grow. An item with flex-grow: 1 takes a share of the free space, and an item with flex-grow: 2 takes twice as much as one with 1.
  • flex-shrink decides who gives up space when there isn't enough. It is 1 by default, so all items shrink evenly. flex-shrink: 0 keeps an item at its basis, which is handy for icons and logos.

A search bar is the classic example of flex-grow. The button keeps its natural size, and the input takes everything else:

.search {
  display: flex;
  gap: 8px;
}
.search input {
  flex-grow: 1;
}

The flex shorthand

You'll rarely write the three properties one by one. The flex shorthand sets them all at once, in the order grow, shrink, basis:

.card {
  flex: 1 1 200px; /* grow: 1, shrink: 1, basis: 200px */
}

Two shortcuts are worth remembering: flex: 1 means 1 1 0%, so all items share the row equally whatever their content, and flex: none means 0 0 auto, so the item keeps its natural size.

Put flex-wrap: wrap on the container and flex: 1 1 200px on the items, and you get a responsive card row without a single media query. Every card starts at 200px, as many as fit share a row and grow to fill it, and the rest wrap onto the next line. On a phone, each card gets a row of its own.

Common mistakes

  • Putting flex-grow on the container. Grow, shrink and basis belong on the items; flex-wrap belongs on the container.
  • Forgetting flex-wrap: wrap. Without it, the items shrink into one long, cramped line instead of moving to the next one.
  • Expecting flex-grow to make everything equal. Growing only shares out the free space, so an item with a bigger basis or more content stays bigger. For exactly equal items, use flex: 1.

Assignment

Finish the realm search page:

  1. Make the search input grow to fill all the free space in .search (flex-grow: 1). The form is already a flex container.
  2. Make .cards a flex container that wraps (flex-wrap: wrap), with a 16px gap.
  3. Give every .card the shorthand flex: 1 1 200px.

On the 1024px-wide test page, four cards fit in the first row and grow to fill it, and the fifth card wraps onto a second row of its own. On a 600px screen, the cards form rows of two.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.

Lesson last updated on .

Write code ↓
HTML & CSS+20 XP

Lesson 20 is part of the full course. The first 7 lessons are free.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.