Let the items in a row drop to a new line when there is no room, so the same page fits a phone and a laptop.
The same web page has to work on a phone, a school laptop and a wide monitor, and a wrapping row does that without a different page for each.
A row of items has a width it fits, and a screen has a width it offers. A phone offers little, and a laptop a lot. A row that wraps fits both.
Make your prediction first. Help opens once you've locked it in.
<style>
.gallery {
display: flex;
gap: 12px;
}
.item {
flex: 1;
min-width: 140px;
border: 2px solid teal;
padding: 8px;
}
</style>
<div class="gallery">
<div class="item"><img src="trophy.svg" alt="Trophy" width="100"><p>Trophy</p></div>
<div class="item"><img src="medal.svg" alt="Medal" width="100"><p>Medal</p></div>
<div class="item"><img src="book.svg" alt="Book" width="100"><p>Book</p></div>
<div class="item"><img src="ball.svg" alt="Ball" width="100"><p>Ball</p></div>
<div class="item"><img src="star.svg" alt="Star" width="100"><p>Star</p></div>
<div class="item"><img src="music.svg" alt="Music" width="100"><p>Music</p></div>
</div>
flex-wrap, flex: 1 and min-width are new. Read the code: min-width: 140px means a card will not get narrower than 140px. Six cards need much more room than the preview has. What happens?
Compare with your prediction, and scroll the preview sideways to see the cards that run off. Then add flex-wrap: wrap; to the .gallery rule. Next, change min-width from 140px to 220px, then delete three cards: the number of cards on each line changes. The preview is narrow, so changing the number of cards or the minimum width is how you see what a phone and a laptop would do.
| Code | What it does | Why |
|---|---|---|
display: flex; |
Puts the cards in a row | It goes on the box that holds them, as before |
flex-wrap: wrap; |
Lets cards drop to a new line when the line is full | Without it, a flex row keeps every item in one line, even when they do not fit |
flex: 1; |
Makes each card grow to share the spare room on its line | Without it, the last card on a line leaves an empty gap at the end |
min-width: 140px; |
Stops a card getting narrower than 140px | A card that would be narrower drops to the next line instead of being squashed |
gap: 12px; |
Puts 12px between cards, across and down | It spaces the lines as well as the cards in a line |
<style>
.gallery {
display: flex;
gap: 12px;
}
.item {
flex: 1;
min-width: 140px;
border: 2px solid teal;
padding: 8px;
}
</style>
<div class="gallery">
<div class="item"><img src="trophy.svg" alt="Trophy" width="100"><p>Trophy</p></div>
<div class="item"><img src="medal.svg" alt="Medal" width="100"><p>Medal</p></div>
<div class="item"><img src="book.svg" alt="Book" width="100"><p>Book</p></div>
<div class="item"><img src="ball.svg" alt="Ball" width="100"><p>Ball</p></div>
<div class="item"><img src="star.svg" alt="Star" width="100"><p>Star</p></div>
<div class="item"><img src="music.svg" alt="Music" width="100"><p>Music</p></div>
</div>
<style>
.gallery {
display: flex;
gap: 12px;
}
.item {
flex: 1;
min-width: 140px;
border: 2px solid teal;
padding: 8px;
}
</style>
<div class="gallery">
<div class="item"><img src="trophy.svg" alt="Trophy" width="100"><p>Trophy</p></div>
<div class="item"><img src="medal.svg" alt="Medal" width="100"><p>Medal</p></div>
<div class="item"><img src="book.svg" alt="Book" width="100"><p>Book</p></div>
<div class="item"><img src="ball.svg" alt="Ball" width="100"><p>Ball</p></div>
<div class="item"><img src="star.svg" alt="Star" width="100"><p>Star</p></div>
<div class="item"><img src="music.svg" alt="Music" width="100"><p>Music</p></div>
</div>
Which line does which job?
Count the cards that fit on a line.
A fixed width is the same everywhere.
The cards stay in one line.
Match each line with what it does.
The cards should wrap onto new lines, but they stay in one line, and there is no error. Click the line that causes it.
The six cards share one line and get thin. Give each a minimum width of 120px.
The cards are squashed into tall, narrow columns with words broken across lines.
Add min-width: 120px; to the .item rule. A card that would be narrower drops to the next line.

Change the minimum width to 180px and see how many cards fit on a line now.
A wider minimum means fewer cards fit on each line.
Change 120px to 180px in the .item rule.

Stop the cards getting narrower than 120px.
This ticks when your page has all of the above.
Make the cards at least 180px wide, so fewer fit on a line.
This ticks when your page has all of the above.
The cards wrap, but they touch.
<style>
.gallery {
display: flex;
flex-wrap: wrap;
}
.item {
flex: 1;
min-width: 140px;
border: 2px solid teal;
padding: 8px;
}
</style>
<div class="gallery">
<div class="item"><p>Robots</p></div>
<div class="item"><p>Coding</p></div>
<div class="item"><p>Drones</p></div>
<div class="item"><p>Design</p></div>
</div>
Make a gallery for a school event: a heading and at least six pictures, each with good alt text. It should fit a phone and a laptop: the pictures wrap onto new lines and share the room on each line. Pictures you can use: trophy.svg, medal.svg, book.svg, ball.svg, star.svg, music.svg.
A gallery is a box that holds the items, and each picture is inside an item. The row rules go on the gallery.
Start with a <div class="gallery"> holding at least six <div class="item"> boxes, each with an img (and its alt).
Then .gallery with display: flex;, flex-wrap: wrap; and a gap, and .item with flex: 1; and a min-width.

Make a gallery of at least six pictures that wraps, with items that share the space and have a minimum width.
This ticks when your page has all of the above.
Students let a flex row wrap with flex-wrap, share each line with flex: 1 and set a min-width, and find out that a fixed width does not change with the screen. About 40 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Do the Make task yourself in the playground (about 5 minutes).
A flex row keeps all its items in one line unless it is told it may wrap. flex-wrap: wrap on the row allows new lines. flex: 1 on the items makes them share the spare room on their line. min-width is the narrowest an item may be: once a line cannot fit another item at that width, the item drops to the next line. Together they make a layout that fits a phone and a laptop with no separate versions.
The preview pane is narrow, so students do not need to resize the browser. They change the number of cards or the min-width and see the same effect a screen of a different width would give.
Believing the browser moves items down by itself. Ask what the row has been told about wrapping before saying.
Stretch: Give the gallery a width of 300px, then 600px, and write down how many cards fit on a line each time. Explain the numbers with min-width and gap.
Support: Give students the Run page and ask them only to add the wrap line and to change the number of cards. Tell them what each change does to the cards on a line.
Next lesson uses a wrapping row for a hero and a row of cards, and shows that one rule can style any number of copies. Class idea: open a school website and drag the window narrower; ask which parts wrap.