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

Set a Grid Minimum That Fits

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-fit makes as many 250-pixel columns as the list has room for, and 1fr shares 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-fit can 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, 22rem is 352 and 100% is 272, so min(22rem, 100%) is 272. The column is 272 pixels wide, and the page is 320 again.
  • On wider screens, nothing changes. There, 22rem is 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.

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 So far, each change of layout has come from a breakpoint you chose. In this video, you’ll make a grid of wider cards still fit every screen, with no breakpoint at all.

00:10 Open the events page in device mode at 1280 pixels. The Coming Up cards sit in a grid, and its rule has no media query. Its columns come from auto-fit and minmax, with a minimum of 250 pixels.

00:25 Auto-fit makes as many 250-pixel columns as the list has room for, and 1fr shares out what’s left. Drag the page narrower. Three columns become two, then one.

00:36 At 320 pixels, the list is 272 pixels wide, and the one column fits it.

00:44 Rosa has a request. She wants each card’s day and time on one line: Saturday, April 3, from 9 a.m. to noon. Back at 1280 pixels, each card is 335 pixels wide, and that sentence breaks in all seven cards.

01:01 The cards are too narrow, so raise their minimum. Try 22rem. That’s 352 pixels, wide enough that only two columns fit at 1280.

01:12 Save. At 1280 pixels, there are two columns of 514, 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.

01:26 Now the phone. At 320 pixels, there’s room for 272 pixels, which is the screen minus the page’s side padding. Your turn: pause the video and predict how wide the column is at 320 pixels.

01:42 It’s 352 pixels, wider than the room. The cards run past the right edge, so the page is 400 pixels wide, and it scrolls sideways.

01:54 Why didn’t the column shrink? In minmax, the first value is a floor. Auto-fit can drop columns, down to one, but it never makes a column narrower than its minimum.

02:05 So the one column stays 352 pixels wide, and it pushes the list and the page wider than the screen. The old minimum only fit because 250 is less than 272. Raising the minimum was half right.

02:20 It works on wide screens, but it needs a cap for small ones.

02:26 CSS has a min function, like the one in most programming languages. You give it values, and it gives back the smallest. But where your code would call it once, the browser works it out again each time the width changes.

02:39 Replace 22rem with min of 22rem and 100%. Inside a grid’s columns, 100% is the room the list has. At 320 pixels, 22rem is 352, and 100% is 272, so the minimum is 272.

02:59 Save. The column is 272 pixels wide, and the page is 320 again. On wider screens, 22rem is the smaller value, so nothing changes. There are two columns of 514 at 1280, and one of 720 at 768.

03:16 The sentence can still wrap on a phone and at a few widths around 800, but nothing scrolls sideways.

03:25 So give a grid’s minimum a cap: min of the minimum you want and 100%. Then the cards fit from a 320-pixel phone to a wide screen, with no media query.

03:37 This replaces a breakpoint when the change is more or fewer of the same thing as the room allows, like these cards. Sometimes you choose what changes, and at which width, like the home page’s sidebar arriving at 45rem.

03:50 Then keep the media query.

03:53 The grid now fits any screen, but the headings are the same size on a phone as on a wide screen. Next, in Make Type and Spacing Fluid, you’ll make them grow with the window.

Become a Member to join the conversation.