Lesson 6 · Apps for your phone
Looks good on every screen
Use media queries to make one page fit phones, tablets and laptops.
About 25 minutes
Tab indents. Press Escape, then Tab, to leave the editor.
Most people who visit your site will be holding a phone. The same page also has to work on a tablet, a laptop and a giant monitor, and you're not going to build four versions. Instead you build one page that adapts: it stacks into a single column on a narrow phone and spreads out into a grid when there's room. That's responsive design, and the main tool for it is the media query.
Step zero: the viewport tag
Phones were around before most sites were built for them. So by default, mobile browsers pretend the screen is about 980px wide, lay the page out at that size, then shrink the whole thing down to fit. Everything ends up tiny and you have to pinch to zoom.
One line in the page's <head> turns that off and tells the browser to use the real screen width:
<meta name="viewport" content="width=device-width, initial-scale=1">
Every responsive page needs it. In the playgrounds you only edit the body, so you won't see it there, but put it in every page you make yourself.
Use flexible sizes
Fixed pixel widths are what break pages on phones. A width: 700px box is wider than nearly every phone screen held upright, so the page scrolls sideways. Prefer sizes that adapt:
| Unit or value | Means | Good for |
|---|---|---|
% | A share of the parent's width | Widths of columns and images |
rem | A multiple of the root font size (usually 16px) | Text, padding, spacing |
fr | A share of the free space in a CSS grid | Grid columns |
max-width | "Up to this wide, but narrower is fine" | Keeping text readable on huge screens |
And one rule that saves a lot of pain:
img { max-width: 100%; height: auto; }
Images then shrink to fit their container instead of bursting out of it.
Media queries
A media query wraps CSS that only applies when a condition is true, usually about the screen width:
.cards { grid-template-columns: 1fr; } /* everyone gets this */
@media (min-width: 600px) {
.cards { grid-template-columns: 1fr 1fr; } /* only 600px wide and up */
}
min-width: 600px means "when the viewport is at least 600px wide". The width where the layout changes is called a breakpoint. There's no official list of breakpoints: pick them where your layout starts to look cramped or stretched, not for specific devices.
Mobile first
Notice the order above: the plain CSS is the phone layout, and media queries add columns as the screen gets wider. That's called mobile first, and it's how most responsive sites are written today. The phone gets the simplest CSS, and you only layer on complexity when there's room for it. Writing desktop styles first and then undoing them with max-width queries works, but you end up fighting your own CSS.
Fingers aren't mouse pointers
A mouse pointer is a single pixel. A fingertip is a lot bigger, and it covers the thing it's tapping. So on touch screens, buttons and links need to be big enough to hit:
- Apple's design guidelines recommend tap targets of at least 44 × 44 points.
- Google's Material Design recommends at least 48 × 48 dp.
- The WCAG 2.2 accessibility standard sets a minimum of 24 × 24 CSS pixels (or enough space around smaller targets).
On the web, a CSS pixel is roughly a point or a dp, so aiming for buttons at least 44px tall is a solid habit. min-height: 44px and some padding does it.
Build it: a game gallery that fits any screen
Challenge
Make the gallery responsive
This gallery was built for a laptop: three fixed 220px columns. On a phone it overflows and the buttons are tiny. Rewrite the CSS mobile first:
- Make
.cardsone column by default (grid-template-columns: 1fr). - Add a media query that switches to two columns at a wider width (like
min-width: 600px), and optionally another for three columns. - Make the Play buttons at least 44px tall.
Then drag the preview's width slider to watch the layout change.
- At phone width, the cards stack in one column
- Nothing sticks out sideways at phone width
- Your CSS has a media query for wider screens
- The Play buttons are at least 44px tall
Recap
- Add
<meta name="viewport" content="width=device-width, initial-scale=1">or phones will shrink a fake desktop-width page. - Use flexible sizes (
%,rem,fr,max-width) instead of fixed widths, andimg { max-width: 100%; }. - Media queries like
@media (min-width: 600px)apply CSS only when the screen is wide enough. - Mobile first: write the phone layout as the default and add columns at breakpoints.
- Make tap targets big: aim for at least 44px.
Finished this lesson?
Mark it complete to track your progress on this device.