Track 02 · Lesson 15 of 17Inside the browser

Track 02 · Inside the browser

Responsive design

Lesson 15 of 17· 6 min

One page, every screen size. Layouts start with a single column for small screens and add columns as space allows, using media queries at breakpoints. Drag the width, and try a phone without the viewport meta tag.

Pour it into any glass

Drag the slider across 600px and 960px

Bookly

Summer reading

Big stories for long evenings.

The Quiet Orchard
Salt and Iron
Night Ferry
Paper Moons
The Long Field
Glass Harbor
/* Mobile first: one column by default */
.grid { display: grid; grid-template-columns: 1fr; }
.nav  { display: none; }

@media (min-width: 600px) {
  .grid { grid-template-columns: repeat(2, 1fr); }
  .nav  { display: flex; }
}

@media (min-width: 960px) {
  .grid { grid-template-columns: repeat(3, 1fr); }
}

At 375px the page shows 1 column and a menu button. Drag the slider to cross a breakpoint.

Mobile first#

Write the CSS for the smallest screen first: one column, a menu button, nothing fancy. Then add @media (min-width: …) rules that add columns and features as space grows. Starting small keeps phones fast, because they never download or undo styles meant for big screens.

The one line you can't forget#

Without <meta name="viewport" content="width=device-width, initial-scale=1">, phones assume the page was built for a desktop, lay it out about 980 pixels wide, and shrink the result to fit. Everything becomes tiny. Tick the box in the demo to see it.

Media queries versus container queries#

A media query asks how big the screen is. A container query, @container, asks how big the component's own box is, so a card can switch layout whether it sits in a wide main column or a narrow sidebar. The demo's mini page is built with container queries.

Check yourself

A site looks tiny and zoomed-out on phones, though its CSS has media queries. What's the likeliest cause?