Responsive design principles
Overview
People visit your site on phones, tablets, laptops, huge monitors, folding phones, and with browser windows of every size — often zoomed in. Responsive design means one page that works well at all of them. It isn’t a separate “mobile site”; it’s a way of writing CSS that assumes you don’t know the screen size.
This chapter covers the tools. This lesson covers the principles that make them work.
1. The viewport meta tag
Phones pretend to be about 980px wide by default, then zoom the page out to fit — because in 2007 most sites were built for desktops. This meta tag tells the browser your page is designed to adapt:
<meta name="viewport" content="width=device-width, initial-scale=1" />Without it, your media queries see a 980px “viewport” on a phone and your carefully designed mobile layout never appears. Every page needs this line in the <head>.
Don’t add maximum-scale=1 or user-scalable=no: they stop people from zooming, which many people rely on to read.
2. Flexible everything
Fixed widths are the main reason pages scroll sideways on phones. Replace them with flexible values:
| Instead of | Use |
|---|---|
width: 1000px |
max-inline-size: 70rem; margin-inline: auto |
width: 700px on a column |
grid tracks (2fr 1fr) or flex: 1 |
width: 700px on an image |
max-inline-size: 100%; block-size: auto |
font-size: 48px everywhere |
clamp() (see Fluid sizing with clamp) |
height: 600px on a section |
min-block-size or no height |
The image rule deserves to be in every stylesheet’s base layer:
img, video, svg { max-inline-size: 100%; block-size: auto; }3. Mobile first
Mobile-first means your base CSS (outside any media query) is the small-screen layout, and media queries add layout as space increases:
/* Base: everyone gets this — one column */
.layout { display: grid; gap: 1rem; }
/* More room: two columns */
@media (width >= 40rem) {
.layout { grid-template-columns: 1fr 1fr; }
}
/* Lots of room: sidebar + content */
@media (width >= 64rem) {
.layout { grid-template-columns: 16rem 1fr 1fr; }
}Why start small?
- The small layout is the simplest. Most elements are full-width blocks in normal flow, which is what CSS does by default. You write less code and override less.
- Additions are easier than removals. Desktop-first means undoing floats, widths and columns at each smaller size (
width: auto; float: none; …). - It forces priorities. On a small screen you have to decide what matters most, which usually improves the large layout too.
4. Breakpoints come from content
A breakpoint is a width at which the layout changes. Don’t pick them from device lists (“iPhone is 390, iPad is 820…”) — devices change every year, and windows can be any width.
Instead, start with the small layout and widen the browser until the design looks wrong: lines of text too long, cards too stretched, too much empty space. That width is your breakpoint. Write it in rem or em so it scales with the user’s font size:
@media (width >= 48rem) { … } /* 768px at the default 16px font size */Most sites need only two or three.
5. Prefer intrinsic layouts
The best responsive layout needs no breakpoints at all. You’ve already met several intrinsic techniques that adapt to the space they’re in:
flex-wrap: wrapwithflex-basis— items wrap when they don’t fit,repeat(auto-fit, minmax(min(15rem, 100%), 1fr))— as many columns as fit,clamp()for type and spacing that scale smoothly,- container queries for components that adapt to their own width.
Use media queries for the big page-level changes (navigation style, the number of page columns) and intrinsic techniques for everything inside.
6. Test properly
- Resize the browser slowly from 320px to very wide, and look for the moment anything breaks.
- Use DevTools’ device mode for touch and pixel-density checks — but remember real devices also have browser bars, notches and on-screen keyboards.
- Zoom to 200% and 400%. At 400% on a 1280px screen, the viewport is effectively 320px wide. Content must reflow into one column without sideways scrolling (see Layouts that work for everyone).
- Increase the default font size in browser settings. Layouts built in
remrespond; layouts built inpxoften break.
Checklist: viewport meta tag, no fixed widths, flexible images, mobile-first CSS, content-based breakpoints, and a test at 320px and at 400% zoom.
Make a fixed-width page responsive
The starter page was built for a 1000px desktop and scrolls sideways on phones. Without changing the HTML structure: add the viewport meta tag, remove the fixed widths, make the image flexible, and rewrite the layout mobile-first so the article and aside stack on small screens and sit side by side (2fr / 1fr) from 50rem. There must be no horizontal scrolling at 320px.
<meta charset="utf-8" />
<div class="page">
<article>
<h1>Learn CSS layout</h1>
<img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='800' height='400'%3E%3Crect width='800' height='400' fill='%23c9d8f8'/%3E%3C/svg%3E" alt="Course preview" />
<p>Modern layout is flexible by default.</p>
</article>
<aside>Related courses</aside>
</div>
<style>
.page { width: 1000px; margin: 0 auto; }
article { float: left; width: 700px; }
aside { float: right; width: 280px; background: #eaf0fc; }
img { width: 700px; }
</style><meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<div class="page">
<article>
<h1>Learn CSS layout</h1>
<img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='800' height='400'%3E%3Crect width='800' height='400' fill='%23c9d8f8'/%3E%3C/svg%3E" alt="Course preview" />
<p>Modern layout is flexible by default.</p>
</article>
<aside>Related courses</aside>
</div>
<style>
*, *::before, *::after { box-sizing: border-box; }
img { max-inline-size: 100%; block-size: auto; display: block; }
/* Mobile first: one column, flexible width */
.page {
max-inline-size: 70rem;
margin-inline: auto;
padding-inline: 1rem;
display: grid;
gap: 1.5rem;
}
aside { background: #eaf0fc; padding: 1rem; }
/* Enhance when there's room */
@media (width >= 50rem) {
.page { grid-template-columns: 2fr 1fr; }
}
</style>Summary
- Always include
<meta name="viewport" content="width=device-width, initial-scale=1">, or phones render a zoomed-out desktop page. - Use flexible units and max widths instead of fixed pixel widths; make images
max-inline-size: 100%. - Mobile-first: write the simple single-column layout first, then add complexity with
min-width(orwidth >=) queries. - Choose breakpoints where your content breaks, not at specific device widths.
- Intrinsic techniques (flex-wrap, auto-fit grids, clamp, container queries) often remove the need for breakpoints entirely.