Lesson 19 of 52Layout
Responsive Design
Build pages that work on every screen with the viewport meta tag, mobile-first media queries, fluid type, responsive images and container queries.
More than half of all web traffic comes from phones, and the rest is spread across laptops, tablets, giant monitors and everything in between. You cannot design a page for one screen size and hope. Responsive design means one page, one set of HTML, that rearranges itself to suit whatever screen it lands on. The good news is that modern CSS does a lot of this for you: Grid's auto-fit, Flexbox's wrapping and max-width on images already adapt. This lesson adds the tools for the rest: the viewport meta tag, media queries, fluid sizes, responsive images and container queries.
Step zero: the viewport meta tag
When smartphones first appeared, almost every website was built for desktop screens about 1000px wide. So mobile browsers pretended to be 980px wide and then zoomed the whole page out to fit. Text became tiny and you had to pinch to read anything.
To tell the browser that your page is ready for small screens, every page needs this line in its head:
<meta name="viewport" content="width=device-width, initial-scale=1">width=device-width says "make the layout as wide as the device actually is", and initial-scale=1 says "do not zoom". Without it, none of your responsive CSS will behave as expected on a phone, because the phone still thinks it is 980px wide. Avoid adding maximum-scale=1 or user-scalable=no: they stop people from zooming, which hurts anyone who needs bigger text.
Start fluid
Before writing a single breakpoint, make the layout flexible by default. Most responsive bugs come from fixed widths.
- Prefer
max-widthoverwidth.width: 800pxoverflows a 375px phone.max-width: 800pxis 800px on a laptop and shrinks on a phone. - Make media shrink:
img, video { max-width: 100%; height: auto; }is in almost every reset stylesheet. - Let things wrap. Flexbox with
flex-wrap: wrapand Grid withrepeat(auto-fit, minmax(...))change their number of columns on their own. - Use relative units such as
remfor type and spacing, and%orfrfor widths.
Often that is enough for a small component. Media queries then handle the bigger changes, like moving a sidebar from below the content to beside it.
Media queries and breakpoints
A media query wraps CSS rules in a condition about the device. The rules inside only apply when the condition is true.
.layout {
display: grid;
gap: 1rem;
}
@media (min-width: 48rem) {
.layout {
grid-template-columns: 16rem 1fr; /* sidebar beside content */
}
}min-width: 48rem means "when the viewport is at least 48rem wide" (768px with the default 16px font size). The width that a query switches at is called a breakpoint. Using rem or em in media queries means the breakpoints scale if someone sets a bigger default font size in their browser.
Modern browsers also accept a range syntax that reads more like maths. It is supported in all current major browsers; check Can I Use if you support old ones.
@media (width >= 48rem) { /* same as min-width: 48rem */ }
@media (40rem <= width < 64rem) { /* tablets only */ }There are no magic breakpoint numbers. Phones, tablets and laptops come in hundreds of sizes. Instead, widen your browser slowly until the design starts to look wrong (lines too long, cards too stretched, too much empty space), and put a breakpoint there. Most sites end up with two or three. Media queries can test more than width, too:
| Query | True when |
|---|---|
(min-width: 48rem) | The viewport is at least 48rem wide |
(orientation: landscape) | The viewport is wider than it is tall |
(hover: hover) | The main pointer can hover (a mouse, not a finger) |
(prefers-color-scheme: dark) | The user chose a dark theme in their system settings |
(prefers-reduced-motion: reduce) | The user asked for less animation |
print | The page is being printed |
Mobile-first
Mobile-first means your base CSS, outside any media query, is for the smallest screens. You then add min-width queries that layer on extra layout as space grows. The opposite, desktop-first, starts big and uses max-width queries to undo things for phones.
Mobile-first usually wins for three reasons. Small-screen layouts are simpler (often one column), so the base is short. Phones, often on slower connections, get the least CSS to apply. And it forces you to decide what really matters on the page.
Every screen gets the base styles. Write them for a narrow phone: one column, full-width buttons.
Put these rules in the order they should appear in a mobile-first stylesheet.
- Tap the steps below in the right order.
Testing in devtools device mode
The preview panes on this page are narrow and do not change width when you resize your laptop window, so a media query demo here would just sit there. Test media queries in your own browser instead:
- Make a file with the base styles and the media query above, and open it in Chrome, Edge or Firefox.
- Open devtools (F12, or Cmd+Option+I on a Mac).
- Turn on device mode: the phone and tablet icon in Chrome and Edge (Ctrl+Shift+M, or Cmd+Shift+M on a Mac), or Responsive Design Mode in Firefox (Ctrl+Shift+M, or Cmd+Option+M on a Mac).
- Pick a device from the list, or drag the edge of the page to resize it and watch the layout snap at your breakpoints.
Device mode is a good first check, but it is still your laptop pretending. Before launch, open the page on a real phone too. Touch targets, the on-screen keyboard and real fonts often reveal problems an emulator does not.
Fluid type with clamp()
A heading that looks right at 48px on a laptop is huge on a phone. You could step it down with a media query, but clamp() lets the size slide smoothly instead:
h1 {
font-size: clamp(1.75rem, 1rem + 4vw, 3.5rem);
}clamp(min, preferred, max) uses the middle value, but never goes below the first or above the third. 1rem + 4vw grows with the viewport (1vw is 1% of its width). Keeping a rem part in the middle matters: a pure vw size would ignore the user's zoom and font settings.
The demo below uses the same idea with cqi units, which measure the container rather than the viewport (more on containers soon), so you can see it work inside this narrow preview. Drag the corner of the dashed box.
Preview
Change the maximum from 2.6rem to 4rem and widen the box again. Then change 6cqi to 2cqi and notice the heading barely changes size; the middle value controls how fast it grows.
Responsive images
max-width: 100% makes an image shrink, but a phone still downloads the full desktop-sized file. The srcset and sizes attributes let the browser pick a smaller file:
<img
src="cake-800.jpg"
srcset="cake-400.jpg 400w, cake-800.jpg 800w, cake-1600.jpg 1600w"
sizes="(min-width: 48rem) 50vw, 100vw"
alt="A slice of lemon cake on a blue plate"
width="800" height="600">srcset lists the files and their real widths (400w means 400 pixels wide). sizes tells the browser how wide the image will be displayed: half the viewport on wider screens, the full width otherwise. The browser does the maths, including the screen's pixel density, and downloads only the best fit. The width and height attributes give the shape in advance so the page does not jump when the image loads.
When you want a different crop on small screens, not just a smaller file, use the picture element with source media="..." children. The images lesson covers both in more detail.
Container queries
Media queries ask about the viewport. That is a problem for components. A product card might sit in a wide main column on one page and in a narrow sidebar on another, on the same screen. The viewport is the same width in both places, but the card has very different space.
Container queries let a component respond to the size of its own container. You mark an element as a container, then query it:
.card-wrap { container-type: inline-size; }
@container (min-width: 22rem) {
.card { display: flex; gap: 1rem; }
}inline-size means "track my width" (in English text, the inline direction is horizontal). Container size queries are supported in all modern browsers since 2023. Drag the box to see the card switch from stacked to side by side:
Preview
Change min-width: 320px to 240px and see the switch happen earlier. A good rule of thumb: use media queries for the page layout (how many columns the page has), and container queries for components that get reused in different spaces.
Match each tool to the problem it solves.
- responsive design
- One page whose layout adapts to any screen size.
- viewport meta tag
- A head tag that tells mobile browsers to use the device's real width and not zoom out.
- media query
- A CSS condition, like min-width: 48rem, that applies rules only when it is true.
- breakpoint
- The width at which a media or container query changes the layout.
- mobile-first
- Writing base styles for small screens and adding min-width queries for larger ones.
- container query
- A query about the size of an element's container instead of the viewport.
- srcset
- An img attribute listing several image files so the browser can download the best size.