Responsive Design
Build one page that looks and works well on every screen size, from phones to large monitors.
What is it?
A page built for a large desktop monitor often breaks on a phone: text too small to read, content spilling off the screen, layouts that assume far more width than exists. The same page gets viewed on everything from small phones to wide displays, so it has to adapt.
Responsive design means building a page whose layout and styling adjust to the screen's size (and other traits) instead of assuming one fixed size. The main CSS tool is the media query: a rule that says "only apply these styles when the screen matches a condition," usually a minimum or maximum width.
A related idea is mobile-first: write base styles for the smallest screen, then use media queries to add complexity as more space becomes available. The opposite, designing for desktop and patching things down for small screens later, tends to produce messier CSS. Starting small gives simpler, sturdier CSS.
Explain like I'm 10
It's like a letter that reformats itself for the paper it's printed on: one column and large text on a small card, several columns and smaller text on a full sheet, with the same words either way.
Examples
A basic media query
.container {
display: flex;
flex-direction: column;
}
@media (min-width: 768px) {
.container {
flex-direction: row;
}
}On small screens, items stack vertically by default. Once the screen is at least 768px wide, the media query switches the layout to a row.
A responsive viewport setup
<meta name="viewport" content="width=device-width, initial-scale=1">Without this tag in the HTML head, mobile browsers often render the page at desktop width and shrink it to fit, making text tiny. The tag tells the browser to use the device's real width from the start.
How it works
The browser always knows the width (and other traits) of its viewport, the visible area of the page. Media queries are checked against it, and rules inside a matching query apply on top of, or instead of, the base styles.
When the window or device orientation changes, the browser re-checks the queries and updates the styles immediately, with no reload.
Why does it exist?
People browse on devices with wildly different screen sizes, not mostly one kind of desktop monitor. Building and maintaining separate "mobile" and "desktop" pages was expensive and error-prone. One page that adapts through CSS is far easier to maintain and keeps content consistent.
When to use it
Use responsive techniques for almost any public page, since you rarely control the device or window size. Mobile-first helps most when content and priorities really change with available space, not just visual size.
When not to use it
An internal tool used only on one known, fixed-size screen (like a kiosk) may not need full responsive treatment. Even then it rarely hurts, so responsive design is close to a default for anything opened in a regular browser.
Common mistakes
Forgetting the viewport meta tag, so mobile browsers render the page at a shrunk-down desktop width.
Designing desktop-first and patching for mobile afterward, which bloats the CSS with overrides.
Testing only by resizing a desktop browser window, without checking real or emulated mobile devices.
Practice exercises
- Easy:
Add a media query that changes a page's background color when the screen is narrower than 500px.
- Medium:
Build a mobile-first navigation bar that stacks links vertically by default and switches to a row above 768px.
- Hard:
Convert a desktop-only layout to mobile-first responsive CSS. List each media query you added and why.
Interview questions
What is a media query?
A CSS rule that applies a block of styles only when the viewport matches some condition, most commonly a minimum or maximum width.
What does 'mobile-first' mean in responsive design?
Writing base CSS for the smallest screen, then using media queries to add complexity and rearrange the layout as space grows, instead of starting from desktop and shrinking down.
Why is the viewport meta tag important for responsive design?
Without it, many mobile browsers render the page at a wide, desktop-like width and scale it down, so text and layout look too small. The tag makes the browser use the device's real width.
What's the difference between a `min-width` and a `max-width` media query, and which fits a mobile-first approach?
A min-width query applies once the viewport is at least that wide, layering enhancements on base styles. That fits mobile-first: unqueried styles target the smallest screens and min-width queries add to them. A max-width query applies when the viewport is at most that wide, the usual desktop-first pattern for shrinking things down.
If two media queries both match the current viewport and set different values for the same property, which one wins?
Normal cascade rules apply: with equal specificity, the later rule in the source wins. Which query's range is narrower doesn't matter, so the order of queries in the stylesheet does.
Why are relative units like `rem`, `em`, `%`, and `vw`/`vh` generally preferred over fixed `px` in responsive design?
They scale with something else (the root font size, a parent's size, or the viewport), so text and spacing adjust when the user's font-size setting or screen size changes. px values stay fixed.
What's the difference between `rem` and `em`, and what's a common gotcha with `em`?
rem is relative to the root (html) font size, so it's the same everywhere. em is relative to the current element's font size, so nested elements using em for font-size compound, each level multiplying the last. Deeply nested text can end up much larger or smaller than expected.
What is a 'breakpoint' in responsive design, and how should you choose one?
It's the viewport width where a media query changes the layout. Choose breakpoints where your content starts to look cramped or awkward, not at popular device widths. Device sizes keep changing; content-based breakpoints stay useful.
What do `max-width: 100%` and `height: auto` do for images in a responsive layout?
max-width: 100% stops an image from getting wider than its container, so it shrinks on narrow screens instead of overflowing. height: auto keeps the aspect ratio as the width changes, so it isn't stretched or squashed.
An image has `width: 100%` but still overflows its container on a narrow screen — what's a likely cause?
The container itself may be too wide, for example because of a fixed min-width, or because box-sizing isn't border-box and padding pushes it wider. The image is correctly filling 100% of a container that's already too wide.
What's the practical difference between using `srcset`/`sizes` versus just scaling one large image down with CSS?
srcset lets the browser pick and download an image file sized for the actual display size and device pixel ratio, so a phone downloads a smaller file. CSS-only scaling always downloads the full-size image, wasting bandwidth on small devices.
What's the difference between 'responsive' (fluid) design and 'adaptive' design?
Responsive design uses fluid, relative-unit layouts and media queries that adjust continuously across sizes. Adaptive design serves a few fixed layouts for specific breakpoints and switches between them.
How does `box-sizing: border-box` help make responsive layouts more predictable?
By default, padding and border are added on top of the width, so a width: 50% element with padding ends up wider than 50%. border-box includes padding and border in width/height, so percentage and fluid widths behave as they look.
If content is hidden at a certain breakpoint with `display: none`, does that mean its resources (like an image) were never downloaded?
Not necessarily. CSS display rules don't stop the parser from requesting resources, so an <img> inside a hidden element may still be fetched. It's only skipped if loading is conditional, for example a <picture> element with media conditions, or lazy loading.
What's the difference between a media query and a container query?
A media query responds to the whole viewport's size. A container query (@container, with container-type set on an ancestor) responds to the size of a specific containing element. A component can then adapt to the space it's given, whatever the screen size, which helps when it's reused in places of different widths.
Given `.card-wrapper { container-type: inline-size; }` and a container query targeting it, what does the query actually measure?
The inline size (the width, in a normal horizontal writing mode) of .card-wrapper itself, not the viewport. A .card in a narrow sidebar and the same .card in a wide main column can render differently in the same browser window.
Why can testing responsiveness only by resizing a desktop browser window miss real problems?
It misses real mobile conditions: touch targets fine for a mouse but too small for a finger, device pixel ratios that affect image sharpness, slower networks, and mobile browser chrome or viewport quirks.
What does the CSS `clamp()` function do, and how is it commonly used in responsive typography?
clamp(min, preferred, max) uses the preferred value but never goes below min or above max. For type, something like font-size: clamp(1rem, 2vw + 0.5rem, 2rem) scales with the viewport while never getting unreadably small or too large.
A fixed-position header overlaps the top of the page content differently across screen sizes — how would you handle this responsively?
Reserve the header's height with padding or margin on the content below it (adjusted per breakpoint if the header's height changes). Or use scroll-margin-top on anchor targets so jumping to an anchor doesn't hide content under the header.
Even with relative units, media queries, and container queries in place, why is testing on real or emulated mobile devices still worth doing?
Those tools handle layout sizing, but real devices reveal what CSS can't reliably simulate: real touch interaction and target sizes, real network latency, on-screen keyboard behavior, and browser-specific rendering quirks that differ from a desktop dev tools simulation.