Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Fill Rows With Automatic Tracks

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-columns and grid-template-rows make 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-rows sizes 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-fit in 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), and 1fr shares 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 / -1 still reaches the last column line, however many there are.
  • auto-fill also fits as many columns as the width allows. Watch out: with all seven cards, every column has a card in it or across it, so auto-fill and auto-fit look 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-fill keeps all four columns, so the cards stay 170 pixels wide beside two empty columns. auto-fit collapses the empty columns to nothing, so the two cards stretch to 364 pixels each.
  • Use auto-fit when a few items should spread out, and auto-fill to 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:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your Coming Up cards sit in a grid, with the seed swap across the top. In the event list rule, you defined three columns. You never defined a single row. Yet the overlay numbers five row lines, so the grid has four rows.

00:15 The tracks you define make up the explicit grid. You defined no rows, so every row here is implicit. The browser adds one each time the cards need more cells.

00:26 By default, a row is as tall as its tallest one-row card.

00:32 The featured seed swap looks squashed. Its row is 163 pixels tall, against 247 for the next two rows, and the last row is 217. To give every row a minimum height, size the implicit rows in the event list rule.

00:49 Add grid-auto-rows, with a minmax of 15rem and auto, and save. Every row is now at least 240 pixels tall, or taller when a card needs it. The seed swap’s row and the last row grow to 240.

01:05 The two middle rows stay at 247, since their cards need more.

01:12 A fixed count of columns squeezes the cards when the space shrinks. In device mode, narrow the window to 500 pixels. The list is 452 wide, and the three columns squeeze to 135 each.

01:25 Widen it again. The repeat function can count the columns for you. In place of the 3, write auto-fit. Then give a minmax of 150 pixels and 1fr for the size.

01:38 That asks for as many columns as fit, each at least 150 wide. They’re still explicit columns. You define them, and the browser only works out how many. Save.

01:50 The list is 752 pixels wide, and four columns fit with their gaps. Then 1fr shares out the rest, so each column is 170 pixels. Since the columns are explicit, -1 is still the last column line, so the seed swap spans the row.

02:09 Now narrow the window again, slowly, and watch the list’s width. Each time it can’t hold another 150-pixel column, the grid drops one: three, then two, then one.

02:23 You’ve seen this grid work at narrow widths. Making whole pages work on a phone is the next step on the path. That’s all you need about narrow screens for now.

02:34 The repeat function takes a second keyword, auto-fill. Try it in the Styles pane. Saving the file would reload the page, and you’ll delete cards next. Change auto-fit to auto-fill, and nothing changes.

02:47 Watch out: the two keywords look the same here, because every column has a card in it or across it. They differ when a column is left empty.

02:59 To see that, delete five cards in the Elements panel, the seed swap too, since it spans every column. Select each one, and press Delete. Two work days are left.

03:10 With auto-fill, the grid keeps all four columns. The two cards fill the first two, 170 pixels each, and the other two stay empty.

03:20 Switch back to auto-fit. Auto-fit collapses the empty columns to nothing, so the two cards stretch across the row, 364 pixels each.

03:30 Use auto-fit when a few items should spread out, and auto-fill to keep the columns the same width however many items there are. Keep auto-fit for the cards.

03:40 Reload, and the five cards come back.

03:45 So you can let the grid decide how many tracks there are. It adds rows as the cards need them, and grid-auto-rows sizes them. With auto-fit or auto-fill, the repeat function makes as many columns as fit.

03:59 Next, in Align Items in Their Cells, you’ll move each item inside the cell the grid gives it.

Become a Member to join the conversation.