In this lesson, you gave the events page’s card grid a wider minimum for Rosa, saw it overflow a phone, and capped it with min() so the grid fits every screen with no media query.
Key Points
- A grid that adapts by itself. The Coming Up cards’ columns come from
repeat(auto-fit, minmax(250px, 1fr)), with no media query.auto-fitmakes as many 250-pixel columns as the list has room for, and1frshares out what’s left. Narrow the page, and three columns become two, then one. At 320 pixels, the list is 272 pixels wide, and the one column fits it. - Rosa’s request. She wants each card’s day and time on one line: Saturday, April 3, from 9 a.m. to noon. At 1280 pixels, each card is 335 pixels wide, and that sentence breaks in all seven cards.
- Raising the minimum works on wide screens. With
minmax(22rem, 1fr), each column is at least 352 pixels. That’s wide enough that only two columns fit at 1280 pixels, each 514 wide, and the sentence fits on one line in every card. At 768, there’s one column of 720 instead of two, and it still fits. - The minimum is a floor. At 320 pixels, there’s room for 272 pixels, which is the screen minus the page’s side padding.
auto-fitcan drop columns, down to one, but it never makes a column narrower than its minimum. - The column pushes the page wider. At 320 pixels, the one column stays 352 pixels wide and pushes the list and the page wider than the screen. The cards run past the right edge, and the page is 400 pixels wide and scrolls sideways. The old 250-pixel minimum fit only because 250 is less than 272.
- Cap the minimum with
min().min()gives back the smallest of its values, like the min function in most programming languages, except that the browser works it out again each time the width changes. 100%is the room the list has, inside a grid’s columns. At 320 pixels,22remis 352 and100%is 272, somin(22rem, 100%)is 272. The column is 272 pixels wide, and the page is 320 again.- On wider screens, nothing changes. There,
22remis the smaller value. You still get two columns of 514 at 1280 and one of 720 at 768. The sentence can still wrap on a phone and at a few widths around 800 pixels, but nothing scrolls sideways.
The Rule
- Give a grid’s minimum a cap:
min(<your minimum>, 100%). Then the grid fits from a 320-pixel phone to a wide screen, with no media query. - When the change is more or fewer of the same thing as the room allows, like these cards, let the grid fit them. Sometimes you choose what changes, and at which width, like the home page’s sidebar arriving at
45rem. Then keep the media query.