Lesson 8 · Mobile apps

Mobile-first responsive design

Build layouts for small screens first, with touch-friendly controls.

About 25 minutes

Tab indents. Press Escape, then Tab, to leave the editor.

Most people who visit a website today will see it on a phone first, and in many parts of the world a phone is the only computer people use. Google indexes the mobile version of sites to decide search rankings. So professional teams design for the small screen first and then add layout as space allows. This approach is called mobile-first, and it's less about phones specifically than about a way of writing CSS that stays simple as a design grows. This lesson covers the viewport, mobile-first media queries, sizing that adapts, and controls that work under a thumb.

The viewport meta tag

Early smartphones had to display websites built for desktop monitors. Their browsers pretended to be about 980 pixels wide, rendered the page at that size, and shrank it to fit the screen. Many still do that unless a page tells them not to, with this line in the <head>:

<meta name="viewport" content="width=device-width, initial-scale=1">

width=device-width sets the layout viewport to the device's width in CSS pixels (about 360 to 430 on most phones), and initial-scale=1 starts at 100% zoom. Without it, none of your responsive CSS behaves as you expect on a real phone, because the browser thinks the screen is 980px wide. Every page you ship needs this tag. The playground below only edits the page body, but real pages always put it in the <head>.

Don't add maximum-scale=1 or user-scalable=no to stop people zooming. Zoom is an accessibility feature that people with low vision rely on, and Safari on iOS ignores those settings anyway.

Mobile-first media queries

A media query applies CSS only when a condition is true, most often about the viewport width. There are two ways to write a responsive stylesheet:

/* Desktop-first: big layout by default, then undo it for small screens */
.products { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 599px) {
  .products { grid-template-columns: 1fr; }
}

/* Mobile-first: simple layout by default, then add to it as space grows */
.products { grid-template-columns: 1fr; }
@media (min-width: 600px) {
  .products { grid-template-columns: repeat(3, 1fr); }
}

They look symmetrical, but they age very differently. In desktop-first CSS, every small-screen fix is an override of something complex, and overrides pile up. In mobile-first CSS, the base styles are the simplest version, which is also the one that loads on the slowest devices, and each min-width query only adds things. Phones never have to process and then undo a desktop layout. Newer CSS also accepts a range syntax, @media (width >= 600px), which means the same as min-width: 600px and works in all current browsers.

Choose breakpoints (the widths where a query kicks in) where your content starts to look wrong, not at the widths of particular devices. Drag the preview narrower and wider; when lines get too long or cards get too cramped, that's a breakpoint. New devices come out every year, but content-based breakpoints keep working.

Question

Quick check

In a mobile-first stylesheet, where does the CSS for a three-column desktop layout go?
Pick an answer.

Sizes that adapt

Fixed pixel widths are the most common cause of the dreaded sideways scroll on phones. Prefer sizes that respond to their container:

  • Percentages and fr size things relative to the space available.
  • max-width: 100% on images (and video) lets them shrink to fit their container but never stretch past their natural size. Put it in your base styles: img { max-width: 100%; height: auto; }.
  • rem sizes text and spacing relative to the root font size, so everything scales together when someone increases their browser's text size.
  • clamp(min, preferred, max) gives a value that grows with the viewport between limits. font-size: clamp(1.5rem, 5vw, 2.5rem) makes a heading that scales with the screen but never gets too small or too large.

Shrinking an image with CSS doesn't shrink the download, though. A phone on a mobile connection shouldn't fetch a 2400px photo to show it 360px wide. The srcset and sizes attributes list several versions of an image and tell the browser how wide it will be shown, so the browser picks the smallest file that still looks sharp on that screen:

<img src="dawn-800.jpg"
     srcset="dawn-400.jpg 400w, dawn-800.jpg 800w, dawn-1600.jpg 1600w"
     sizes="(min-width: 600px) 50vw, 100vw"
     alt="Mountains at dawn">

Phones often have slower processors and networks than the laptop you develop on, so on mobile, performance is part of the design.

Keep body text at least 16px (1rem by default). Smaller text is hard to read on a phone, and Safari on iOS zooms the page when someone focuses a form field with text smaller than 16px, which feels broken.

Designing for touch

A fingertip is much less precise than a mouse pointer, and there's no hover on a touchscreen. Platform guidelines are specific about target sizes: Apple's Human Interface Guidelines recommend at least 44 by 44 points for tappable controls, and Google's Material Design recommends 48 by 48 dp. WCAG 2.2 sets 24 by 24 CSS pixels as the minimum for its AA level and 44 by 44 for AAA. In practice, aim for 44px or more in both directions, with space between neighboring targets.

The visible part of a control can be smaller than its tappable area. Padding counts, so a small icon button can have a generous tap area without looking large. And because touchscreens can't hover, never hide something important behind hover alone; you can apply hover effects only where they make sense with @media (hover: hover).

Question

Quick check

A row of icon buttons are 28px square, which suits the design. How do you make them easier to tap without making the icons bigger?
Pick an answer.

Make a page mobile-first

Challenge

Convert a page to mobile-first

This page was built desktop-first and it's rough on a phone. Fix it:

  1. Make the base .products style a single column, and add a @media (min-width: 600px) query that switches to three columns.
  2. Let the image shrink to fit: replace the fixed width with max-width: 100%; height: auto;.
  3. Make body text at least 16px.
  4. Make every button at least 44px tall and wide.

Use the width slider to check the phone and wider layouts.

  • The base product list is one column, outside any media query
  • A min-width media query gives wider screens several columns
  • The image shrinks to fit a small screen
  • Body text is at least 16px
  • Every button is at least 44px tall and wide

Recap

  • The viewport meta tag makes phones lay pages out at their real width; without it, responsive CSS doesn't work on a device.
  • Mobile-first CSS puts the simple, small-screen layout in the base styles and adds to it with min-width media queries.
  • Choose breakpoints where your content needs them, not at specific device widths.
  • Use flexible sizes: max-width: 100% for images, rem for type, fr and percentages for layout; keep body text at 16px or more.
  • Make touch targets at least 44px in both directions, and never hide essentials behind hover.

Finished this lesson?

Mark it complete to track your progress on this device.