×

css · level 2 · unit 4 · 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

and let a grid count its own columns.

minmax(180px, 1fr) makes a track at least 180px wide, and at most one share of the free space.

Put it in repeat() with auto-fit where the number goes: repeat(auto-fit, minmax(180px, 1fr)). The grid fits in as many 180px columns as it can, then stretches them to fill the row.

A phone gets one column, a tablet three or four, a wide screen even more. It reflows like flex cards do, but every column lines up.

<div class="cards">
  <div class="card">Cats</div>
  <div class="card">Dogs</div>
  <div class="card">Birds</div>
  <div class="card">Fish</div>
</div>
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}
.card {
  padding: 20px;
  background-color: mintcream;
}

Press show my page to see it here.

No media query in sight, yet the number of columns changes with the screen.