Responsive Design
A responsive page adapts its layout to the space it is given — a phone, a laptop, a tablet held sideways, a split-screen window — without separate sites or horizontal scrolling. The technique is mature: fluid units, a few carefully chosen breakpoints, and images that ship at the right resolution. The discipline is in the order of operations, because a mobile-first stylesheet starts simple and earns complexity at wider widths, while a desktop-first one starts fragile and tries to undo damage.
This lesson covers the mobile-first sequence, how to pick breakpoints that reflect your content, fluid typography with clamp, responsive image delivery, and the small details — viewport meta, touch targets — that make a page feel native on a phone.
Why Fixed Layouts Break
A layout locked to 960 pixels forces phones into a zoomed-out pinching ritual and leaves huge empty margins on ultrawide monitors. The viewport meta tag is the first fix: without width=device-width, initial-scale=1.0 in the head, mobile browsers pretend to be desktop browsers and scale your CSS pixels away.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Field Notes Café — Menu</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header class="site-header">
<a class="logo" href="/">Field Notes Café</a>
<button class="nav-toggle" aria-expanded="false" aria-controls="site-nav">Menu</button>
<nav id="site-nav" class="site-nav">
<a href="/menu">Menu</a>
<a href="/events">Events</a>
<a href="/visit">Visit</a>
</nav>
</header>
<main class="menu-grid"></main>
</body>
</html>
With the viewport set, CSS pixels match the device's layout viewport, and media queries become meaningful. From here on, every decision is about how rules layer.
The Mobile-First Sequence
Mobile-first means writing the base stylesheet for the narrowest case: one column, stacked header, large tap targets. Then min-width media queries add capability as space appears — two columns, a horizontal nav, sidebars. The benefits compound. Phones download only the base rules; progressive enhancement mirrors how CSS itself loads; and you are never writing display: block to undo a desktop rule that never should have applied.
/* Base: phones, single column */
.menu-grid {
display: grid;
grid-template-columns: 1fr;
gap: 1.25rem;
padding: 1rem;
}
.site-nav {
display: none;
}
.site-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
padding: 0.75rem 1rem;
}
/* Enhance as space allows */
@media (min-width: 640px) {
.menu-grid {
grid-template-columns: 1fr 1fr;
padding: 1.5rem;
}
}
@media (min-width: 960px) {
.nav-toggle {
display: none;
}
.site-nav {
display: flex;
gap: 1.25rem;
}
.menu-grid {
grid-template-columns: 2fr 1fr;
padding: 2rem;
}
}
Notice what did not happen: there is no rule hiding the nav on desktop; the base simply never showed it until a wide-screen rule turned it on.
Breakpoints: Where and How Many
Breakpoints belong where your content breaks, not where a popular device happens to be. Resize a browser window slowly and watch the layout: when text lines get too short, cards squeeze to unreadable widths, or whitespace balloons, that width is a candidate breakpoint. Common content-driven values cluster around 480, 640, 768, 960, and 1280 pixels, but your design may need only two or three.
| Range | Typical role | Layout changes |
|---|---|---|
| Up to 480 | Small phones | One column, stacked controls |
| 480 to 640 | Large phones | Slightly roomier padding, 2-up where dense |
| 640 to 960 | Tablets | Two-column grids, nav switches to horizontal |
| 960 to 1280 | Laptops | Sidebars appear, wider gutters |
| Above 1280 | Desktops | Constrained measure, max-width containers |
A max-width query such as @media (max-width: 639px) is occasionally useful for tuning a single small-screen case, but relying on max-width exclusively recreates desktop-first undoing.
Fluid Type with clamp
Fixed font sizes need a media query each step; fluid type interpolates. The clamp(min, preferred, max) function sets a preferred value that scales between hard bounds:
:root {
--measure: 65ch;
}
html {
font-size: 100%;
}
h1 {
font-size: clamp(1.75rem, 1.2rem + 2vw, 2.75rem);
line-height: 1.15;
}
p {
font-size: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
line-height: 1.65;
max-width: var(--measure);
}
img {
max-width: 100%;
height: auto;
}
The middle term is a linear function of viewport width: at small sizes the minimum holds, at large sizes the maximum caps growth, and between them type scales smoothly. Keep the root at the user's preferred size so rem-based clamps respect accessibility settings.
Responsive Images: srcset and picture
Serving a 2000-pixel photo to a 400-pixel screen wastes bandwidth and earns Lighthouse penalties. The srcset attribute lists candidate widths; the browser picks based on viewport and device pixel ratio, guided by sizes:
<img
src="/img/pastry-800.jpg"
srcset="/img/pastry-400.jpg 400w,
/img/pastry-800.jpg 800w,
/img/pastry-1600.jpg 1600w"
sizes="(min-width: 960px) 640px, 100vw"
width="800" height="600"
alt="Almond croissants cooling on a wire rack"
loading="lazy">
The sizes attribute mirrors your layout: above 960 pixels the image occupies a 640-pixel column; below that it spans the full viewport width. When the shape of the image should change — a wide landscape crop on desktop, a tall portrait on phones — use the picture element with media-based source children and an img fallback.
Touch, Motion, and Other Mobile Details
Phones are fingers, not cursors. Interactive targets should be at least 44 by 44 CSS pixels, spaced so taps do not mis-fire. Hover-only affordances fail on touch devices — keep critical information visible without hover. The prefers-reduced-motion media query lets you disable non-essential animation for readers who request it:
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
Testing is simple and non-negotiable: use the browser's device emulation, then try a real phone. Rotate the device, throttle the network, and turn text scaling up in the operating system. Breakpoints that only work at 100 percent zoom are not breakpoints.
Container Queries: Components That Know Their Width
Media queries ask about the viewport. Components live in sidebars, modals, and grid cells whose width has nothing to do with the window, so a card styled with viewport breakpoints looks wrong the moment it moves. Container queries invert the question: instead of asking whether the screen is wide, the component asks whether it is wide, and answers with its own styles.
Two declarations make it work. A parent opts in as a query container with container-type: inline-size, and descendants respond with @container rules:
.card-row {
container-type: inline-size;
container-name: cardrow;
display: grid;
gap: 1rem;
}
/* Applies inside .card-row whenever the container, not the viewport, is wide */
@container cardrow (min-width: 420px) {
.card {
grid-template-columns: 120px 1fr;
}
.card .meta {
display: flex;
gap: 0.75rem;
}
}
The same card markup now shows a thumbnail layout when its column is roomy and a stacked layout when it is squeezed into a narrow rail — no modifier classes, no JavaScript measuring widths. The practical impact on design systems is large: buttons, badges, navigation items, and cards become self-contained layout units you can drop anywhere without auditing every page that might host them.
Two details deserve attention. Container-type inline sizing makes the element's inline size independent of its contents, which is exactly what enables the measuring, but it also means the container cannot size itself from content in that axis — usually harmless, occasionally surprising. And container queries do not replace viewport breakpoints for page-level structure; they compose with them. The viewport sets the stage; containers tune the actors.
💡A five-minute responsive test routine
Resize the browser from its widest setting down to about 320 pixels and watch three things: line lengths staying comfortable, cards never squeezing below readable widths, and no horizontal scrollbar appearing at any point. Then turn on device emulation, rotate the phone, and bump the page zoom to 200 percent — WCAG requires the layout to remain usable there. Finally, scale the root font size up in your operating system settings and confirm nothing overlaps. Layouts that survive those three checks handle essentially every real visitor, including the ones on five-year-old hardware and unfamiliar browsers.
Best Practices
- Write the base stylesheet for the smallest screen, then enhance with min-width queries.
- Pick breakpoints where your content breaks, not where a device model sits.
- Use clamp for type scales so sizes flow between sensible bounds.
- Ship responsive images with srcset and sizes; reserve space with width and height attributes.
- Respect prefers-reduced-motion and test with text scaling enabled.
- Keep line lengths near 65 characters for comfortable reading at every width.
Key Takeaways
- The viewport meta tag is step one — without it, mobile browsers fake a desktop layout.
- Mobile-first layering means phones download less CSS and rules never need undoing.
- Breakpoints follow content failure points; two or three well-chosen ones beat seven device targets.
- clamp(min, preferred, max) replaces stepped font sizes with smooth fluid type.
- srcset plus sizes lets the browser choose image resolution; picture handles art direction.
- Touch target size, reduced motion, and real-device testing separate polished responsive work from theory.