×

css · level 2 · unit 2 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Wrap plus grow makes cards that on any screen.

Mix what you know: flex-wrap: wrap on the parent, and flex: 1 1 200px on each card. Every card starts at 200px, wraps when it cannot fit, then grows to fill its line.

A phone gets one card per line and a laptop three or four. Nothing tells the cards how many to show. They reflow, rearranging themselves for the room they have.

When the last line holds fewer cards, they grow wider than the rest. That is how flex works: each line shares out its own free space.

<div class="cards">
  <div class="card">Pasta</div>
  <div class="card">Curry</div>
  <div class="card">Tacos</div>
  <div class="card">Ramen</div>
</div>
.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
.card {
  flex: 1 1 200px;
  padding: 24px;
  background-color: lavenderblush;
}

Press show my page to see it here.

On a phone the four cards stack; on a wide screen they share one line.