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
375as 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
imgrule, itswidthandheightattributes show up as styles: 800 pixels by 600. - Give the photo a ceiling. Add
max-width: 100%to theimgrule 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
heightattribute still asks for 600 pixels. Withheight: auto, the height follows the width and keeps the photo’s shape, about 146 pixels tall at 375. - The column gives way. Type
900as 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 itsmax-width. It’s a ceiling, so the column can be narrower. - Watch out: a plain
widthdoesn’t mean “at most”. Select the Coming Up section and turn itsmax-widthintowidthin 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
900as the width again. The work day card is 164 pixels tall there, because a block’s height comes from its content. - Watch out: a
heightyou 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.styleis this one element’s own style attribute, and editing it doesn’t touch your files. Give itheight: 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
heighttomin-height. Withmin-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-widthfor a column, amin-heightfor a box of text, andmax-width: 100%withheight: autofor images.