In this lesson, you let the Coming Up grid on the events page decide how many tracks it has: rows as the cards need them, with a minimum height, and as many columns as fit the width, re-flowing as the window narrows.
Key Points
- The tracks you define with
grid-template-columnsandgrid-template-rowsmake up the explicit grid. The events list defines no rows, so every row in it is implicit: the browser adds one each time the items need more cells. By default, a row is as tall as its tallest one-row item, and an item spanning two rows, like Plant the Potatoes, only needs the two rows together, with the gap between them, to be tall enough. grid-auto-rowssizes the implicit rows.grid-auto-rows: minmax(15rem, auto)makes every row the grid adds at least 15rem tall (240 pixels), or taller when a card needs it: the seed swap’s row and the last row grew to 240, and the two middle rows stayed at 247.- A fixed count of columns squeezes the cards when the space shrinks. In
repeat(),auto-fitin place of a number asks for as many columns as fit.repeat(auto-fit, minmax(150px, 1fr))makes columns at least 150 pixels wide. They’re still explicit columns: you define them, and the browser only works out how many. In the 752-pixel list, four fit with their 24-pixel gaps (a fifth would need 846), and1frshares out the rest, so each column is 170 pixels. - As the window narrows, the grid drops a column whenever the list can no longer hold one more. Below an 800-pixel window, the list is 48 pixels narrower than the window, so at 720 the list is 672, exactly enough for four columns (4 × 150 + 3 × 24). Narrower windows give three columns, then two, then one. Since the columns are explicit,
grid-column: 1 / -1still reaches the last column line, however many there are. auto-fillalso fits as many columns as the width allows. Watch out: with all seven cards, every column has a card in it or across it, soauto-fillandauto-fitlook the same. They differ when a column is left empty, with no item in it or spanning it.- With five cards deleted, the full-width seed swap among them, and two cards left,
auto-fillkeeps all four columns, so the cards stay 170 pixels wide beside two empty columns.auto-fitcollapses the empty columns to nothing, so the two cards stretch to 364 pixels each. - Use
auto-fitwhen a few items should spread out, andauto-fillto keep the columns the same width however many items there are. - You’ve seen this grid work at narrow widths. Making whole pages work on a phone is the next step on the path.
Final Code
The .event-list rule in events/layout.css: