Skip to content
FORGENO ADS GAMESUnlock all $8

CSS · Unit 3: Layout with flexbox and grid · Lesson 3 of 6

Wrapping and flexible sizes

By default, flex items squeeze onto one line.

Opens this lesson in Code Academy. Lessons open one after another, and each unit opens when you pass the checkpoint quiz before it (you can take a checkpoint early to skip ahead). If this lesson is not open yet, the button shows you its unit on your map.

The idea

By default, flex items squeeze onto one line. flex-wrap: wrap lets them flow onto the next line when there is no room, like words in a paragraph.

flex: 1 makes items share the spare space equally. flex: 1 1 200px means: start at 200px, then grow or shrink as needed.

Wrapping plus a starting size gives you cards that rearrange themselves on phones without any extra code.

Worked example

CSS

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.card {
  flex: 1 1 200px;
  padding: 16px;
  background-color: #fde68a;
}

The HTML it styles

<div class="cards">
  <div class="card">A</div>
  <div class="card">B</div>
  <div class="card">C</div>
</div>

Key terms

flex-wrap
wrap lets flex items flow onto new lines when there is no room.
Flexbox
display: flex lines children up in a row or column; gap spaces them.

Practise it in Code Academy

It has 4 exercises (multiple choice, fill in the blank and fix the broken code) and takes about 7 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.

Every lesson is free: no ads, no lives and no paywall.