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

Keep Sizes Flexible on Small Screens

In this lesson, you looked at the events page on a phone’s screen in DevTools’ device mode. You kept the seed swap photo inside its card, watched the column give way as the screen narrowed, and saw why a fixed width or height doesn’t.

Follow Along

The video starts from events/upcoming.html and events/events.css as you left them in Size Boxes From Their Container, with the banner’s min-height: 100vh. Open the page in Chrome or Edge, with DevTools docked beside it, and scroll down to the Coming Up section. Rosa’s volunteers read the events page on their phones.

Key Points

  • See the page at a phone’s size. In DevTools, click the Toggle device toolbar button, at the top left. A toolbar above the page sets the screen’s size: type 375 as the width. The whole page shrinks, and the text gets tiny. Scroll down to the cards. The seed swap photo has stuck out of its card for a while now, and on a phone, that does more damage, because the browser zooms out to fit the photo on the screen.
  • The photo’s attributes set its size. Select the photo in the Elements panel. In the Styles pane, under your img rule, its width and height attributes show up as styles: 800 pixels by 600.
  • Give the photo a ceiling. Add max-width: 100% to the img rule in your base stylesheet. A percentage is a share of the container’s width, so the photo can’t be wider than its box. The page is back at full size, and the photo fits inside its card, but it’s squashed, 195 pixels wide and still 600 tall.
  • Let the height follow. The height attribute still asks for 600 pixels. With height: auto, the height follows the width and keeps the photo’s shape, about 146 pixels tall at 375.
  • The column gives way. Type 900 as the width, then drag the handle at the page’s right edge slowly down to 375. The green column stays 700 pixels wide, then narrows with the screen, and the text inside it wraps. That’s its max-width. It’s a ceiling, so the column can be narrower.
  • Watch out: a plain width doesn’t mean “at most”. Select the Coming Up section and turn its max-width into width in the Styles pane. At 375, the column stays 700 pixels wide and runs past the phone’s right edge, so the page scrolls sideways. A live edit doesn’t make the browser zoom out again. If that width were in your file, a fresh load would zoom the page out instead. Either way, the column doesn’t fit. Change it back, and the column fits the phone again.
  • Heights come from the content. Type 900 as the width again. The work day card is 164 pixels tall there, because a block’s height comes from its content.
  • Watch out: a height you set doesn’t grow. Say you pin the card at that height to keep its look. Select the work day card. At the top of the Styles pane, element.style is this one element’s own style attribute, and editing it doesn’t touch your files. Give it height: 164px, and nothing changes yet.
  • The text spills out. Make the screen 320 wide, and the card stays 164 pixels tall. Its text still needs more room. The date wraps onto a third line, and the badge’s line ends up outside the card’s content box, on its bottom border.
  • Let the box grow. Change height to min-height. With min-height: 164px, the card grows to fit, and in a wide window, it’s still at least 164 pixels.
  • The rule: a fixed size doesn’t give way on a small screen. Use limits instead: a max-width for a column, a min-height for a box of text, and max-width: 100% with height: auto for images.

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 In the last lesson, you saw where a box’s width and height come from. In this video, you’ll look at the events page on a phone’s screen and see which of your sizes give way.

00:11 Rosa’s volunteers read the events page on their phones, and DevTools can show it at a phone’s size. In DevTools, click the Toggle device toolbar button, at the top left.

00:22 A toolbar above the page now sets the screen’s size. Type 375 as the width. The whole page shrinks, and the text gets tiny. Scroll down to the cards. You’ve seen the seed swap photo stick out of its card for a while now.

00:37 On a phone, it does more damage, because the browser zooms out to fit the photo on the screen.

00:44 Select the photo in the Elements panel. In the Styles pane, under your img rule, the photo’s width and height attributes show up as styles: 800 pixels by 600.

00:56 Nothing tells the photo to stay inside its card.

01:00 So give it a ceiling. In events.css, add max-width: 100% to the image rule in your base stylesheet. A percentage is a share of the container’s width, so the photo can’t be wider than its box.

01:14 Save. The page is back at full size, and the photo fits inside its card. But it’s squashed, 195 pixels wide and still 600 tall.

01:25 The height attribute still asks for 600 pixels. Add height: auto, and save. Now the height follows the width and keeps the photo’s shape, about 146 pixels tall.

01:39 Now type 900 as the width. Then drag the handle at the page’s right edge slowly down to 375.

01:48 The green column stays 700 pixels wide, then narrows with the screen, and the text inside it wraps. That’s the column’s max-width, from two lessons back. It’s a ceiling, so the column can be narrower.

02:01 Watch out: a plain width doesn’t mean “at most”. Check it in the Styles pane. Select the Coming Up section, and in its rule, turn max-width into a plain width declaration.

02:14 The column stays 700 pixels wide and runs past the phone’s right edge, so the page scrolls sideways. A live edit doesn’t make the browser zoom out again. If that width were in your file, a fresh load would zoom the page out instead.

02:27 Either way, the column doesn’t fit.

02:31 Change it back, and the column fits the phone again.

02:35 Type 900 as the width again. The work day card is 164 pixels tall here, because a block’s height comes from its content. Watch out: a height you set doesn’t grow.

02:48 Say you pin the card at 164 pixels to keep its look. Select the work day card. At the top of the Styles pane, element.style is this one element’s own style attribute, and editing it doesn’t touch your files.

03:03 Give it a height of 164 pixels. Nothing changes yet. Now make the screen 320 wide, as on the narrowest phones. The card stays 164 pixels tall. Its text still needs more room.

03:16 The date wraps onto a third line, and the badge’s line ends up outside the card’s content box, on its bottom border.

03:24 Now change height to min-height, and the card grows to fit. In a wide window, it’s still at least 164 pixels.

03:33 So a fixed size doesn’t give way on a small screen. Use limits instead: a max-width for a column, a min-height for a box of text, and max-width: 100% with height: auto for images.

03:48 Your edits in the Styles pane weren’t saved, so your file only has the new image rule.

03:54 The events page now fits a phone, and the photo keeps its own shape. Next is the lesson Fit Photos Into Their Boxes, where you’ll give the photo a box of a set shape.

Become a Member to join the conversation.