Show contents
Chapter 1 of 7 · 20 min
Designing mobile first
By the end of this chapter, you'll be able to
- Explain the mobile-first approach and the role of the viewport tag
- Write a layout that adapts with media queries
- Size comfortable touch targets
- Estimate a page's loading time on a slow connection
Mobile first
The mobile-first approach means designing for the small screen first, then enriching the layout when there is more room. It is easier this way round: starting from the desktop forces you to remove and squeeze, starting from mobile forces you to pick what matters.
First essential line, in the <head> of every page:
<meta name="viewport" content="width=device-width, initial-scale=1">
It asks the phone's browser to use the screen's real width. Without it, the browser acts as if the page were about 980 pixels wide, then shrinks it to fit: everything becomes tiny.
Media queries that add
In mobile first, the base CSS describes the phone version (one column), and min-width media queries add columns as the screen gets wider:
.products { display: grid; gap: 1rem; grid-template-columns: 1fr; }
@media (min-width: 600px) {
.products { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 1000px) {
.products { grid-template-columns: repeat(4, 1fr); }
}
The same logic, written in JavaScript to see which rule applies depending on the width:
const breakpoints = [
{ min: 0, columns: 1 },
{ min: 600, columns: 2 },
{ min: 1000, columns: 4 },
];
function columns(width) {
return breakpoints.filter((b) => width >= b.min).at(-1).columns;
}
for (const width of [360, 412, 768, 1280]) {
console.log(`${width} px → ${columns(width)} column(s)`);
}
360 px → 1 column(s)
412 px → 1 column(s)
768 px → 2 column(s)
1280 px → 4 column(s)
Each min-width adds to the previous ones, and the last matching rule wins: that is why we take the last matching entry with .at(-1).
On a large screen, the page is richer; it was never designed by “shrinking the desktop”.
Text description of the animation
- The base CSS targets the phone: a single column, nothing to scroll sideways.
- From 600 pixels, a media query adds a second column.
- The tablet makes use of the space without changing the content.
- A second rule switches to four columns.
- On a large screen, the page is richer; it was never designed by “shrinking the desktop”.
Targets for fingers
A finger is far less precise than a mouse. The WCAG 2.2 accessibility rules require targets of at least 24 × 24 CSS pixels (or enough spacing), and mobile systems' guidelines rather recommend 44 to 48 pixels for real comfort. Links stuck to each other in a menu are the main source of mistaken taps.
Page weight
On a slow connection, every kilobyte counts. A simple estimate: the transfer time, plus one network round trip per file to fetch (in reality, some requests happen in parallel, but the order of magnitude is useful):
function loading(kilobytes, requests, mbps, latencyMs) {
const transfer = (kilobytes * 8) / (mbps * 1000);
return transfer + (requests * latencyMs) / 1000;
}
const page = { heavy: [3200, 85], light: [450, 12] };
for (const [name, [kb, req]] of Object.entries(page)) {
console.log(`${name}: ${loading(kb, req, 1.6, 150).toFixed(1)} s on slow 3G`);
}
heavy: 28.8 s on slow 3G
light: 4.0 s on slow 3G
Oversized images, multiple fonts, JavaScript libraries loaded “just in case”: reducing the weight and number of files is often the most worthwhile improvement for a mobile site. Modern image formats (WebP, AVIF) and the loading="lazy" attribute on off-screen images help a lot.
Mini-lab: a mobile-first product gridHTML · CSS
Preview
The preview is isolated from the page and runs no scripts · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: auditing touch targets
Here are the dimensions (in CSS pixels) of the clickable elements of a payment page. Write a function that classifies each element: "too small" under 24 px (in at least one dimension), "acceptable" from 24 to 43 px, "comfortable" from 44 px in both dimensions. Display the elements to fix.
const targets = [
{ name: "Pay", width: 320, height: 48 },
{ name: "close (×)", width: 16, height: 16 },
{ name: "terms checkbox", width: 20, height: 20 },
{ name: "Help link", width: 40, height: 28 },
];
Hint 1
Math.min(width, height).Hint 2
filter then map give the list of names to fix.Show the solution and common mistakes
const targets = [
{ name: "Pay", width: 320, height: 48 },
{ name: "close (×)", width: 16, height: 16 },
{ name: "terms checkbox", width: 20, height: 20 },
{ name: "Help link", width: 40, height: 28 },
];
function classify({ width, height }) {
const side = Math.min(width, height);
if (side < 24) return "too small";
if (side < 44) return "acceptable";
return "comfortable";
}
for (const t of targets) console.log(`${t.name}: ${classify(t)}`);
const toFix = targets.filter((t) => classify(t) === "too small").map((t) => t.name);
console.log("to fix:", toFix.join(", "));
Pay: comfortable
close (×): too small
terms checkbox: too small
Help link: acceptable
to fix: close (×), terms checkbox
A 20-pixel checkbox on a payment page is a guaranteed source of mistakes. Make it bigger, or make its whole label clickable with a <label> (the touch target then becomes the whole text).
Common mistakes:
- Only testing the width: a wide but very short link (40 × 12) remains hard to tap.
- Enlarging the icon instead of the target: you can keep a 16-pixel icon inside a 44-pixel button thanks to
padding.
Summary
- 1Mobile first: design for the small screen, then enrich with min-width media queries.
- 2The viewport tag (width=device-width) is essential, without blocking zoom.
- 3Touch targets are at least 24 px, ideally 44 to 48 px.
- 4On a slow connection, page weight and the number of files determine loading time.
- 5Modern images, lazy loading and little JavaScript make a mobile site fast.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.