Responsive Design & Media Queries
Why mobile-first media queries, fluid units, and responsive images are how one stylesheet adapts to every screen instead of needing a dozen versions.
Responsive Design & Media Queries
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Mobile-first means writing your base styles for the smallest screen and using min-width media queries to add complexity as the viewport grows, instead of subtracting it.
- A breakpoint should be chosen based on where your own layout starts to look awkward, not copied from a fixed list of device widths.
- Percentages, vw/vh, and clamp() size elements relative to their container or viewport instead of a fixed pixel value, which is what lets layouts adapt without a media query for every size.
- clamp(min, preferred, max) lets a value scale fluidly between two bounds, replacing what used to require several separate media-query overrides.
- Responsive images use srcset and sizes so the browser can download a size appropriate to the viewport and screen density, instead of always shipping the largest version.
Why “one website, one layout” stopped working
CSS was designed in an era when “the screen” meant a desktop monitor of fairly predictable size. That assumption broke completely once phones, tablets, and every size in between started loading the same web pages. A layout built for a 1400px desktop screen doesn’t just look a little cramped on a 375px phone — it usually overflows, breaks, or becomes unusable. Responsive design is the set of techniques that let one stylesheet serve every screen size well, instead of forcing you to maintain a separate site for each device class the way companies briefly tried to in the early smartphone era (“m.example.com,” anyone?).
The core tool is the media query — a CSS rule that only applies when some condition about the viewport is true, most commonly its width. But media queries alone don’t make a design responsive; it’s the strategy around how you use them that matters, and the strategy that won is mobile-first.
Mobile-first: add complexity, don’t subtract it
Mobile-first means your default, unconditional CSS — the styles that apply
with no media query at all — describes the smallest, simplest version of
the layout. You then use min-width media queries to layer on more complex
layout as the viewport grows:
.cards {
display: flex;
flex-direction: column;
}
@media (min-width: 768px) {
.cards {
flex-direction: row;
flex-wrap: wrap;
}
}
The alternative — writing the full desktop design first and using
max-width queries to strip things down for small screens — tends to
produce messier CSS, because you end up fighting your own base styles,
undoing things you just declared a few lines up. It also mismatches how most
traffic actually arrives today: starting from the constrained case and
expanding outward tends to produce a simpler, more honest hierarchy of what
actually matters on the page, since you’re forced to decide what belongs in
the cramped version first.
Where to put a breakpoint
A common beginner question is “what breakpoints should I use — 480px, 768px, 1024px?” Those numbers show up everywhere because they roughly map to old device classes, but treating them as rules is a trap. The real answer: resize your own browser window and watch where your own layout starts to look wrong. Maybe your three-column card grid starts to feel cramped at 900px, not 768px. That’s your breakpoint. Content should dictate breakpoints, not a list copied from a tutorial — devices now span such a continuous range of widths that no fixed list of breakpoints reliably matches all of them anyway.
Fluid units: sizing relative to something, not a fixed number
A layout built entirely from pixel values can’t adapt — width: 600px means
the same immovable thing on every screen. Responsive design leans on units
that are relative instead:
- Percentages (
%) size an element relative to its parent’s size. - Viewport units (
vw,vh) size an element relative to the browser window itself —50vwis always half the viewport’s width, regardless of what contains the element. clamp()is the newest and, for a lot of use cases, the most useful: it takes a minimum, a preferred (usually fluid) value, and a maximum, and lets the browser pick the middle value unless it would fall outside the bounds.
h1 {
font-size: clamp(1.5rem, 4vw + 1rem, 3rem);
}
That heading never shrinks below 1.5rem, never grows past 3rem, and
scales smoothly with the viewport in between. Before clamp() existed,
getting a smoothly scaling value like this required several separate
media-query breakpoints, each nudging the font size up a little — functional,
but it produced visible, discrete jumps rather than a smooth scale. clamp()
collapses that into one line and removes the jump entirely.
Images don’t get a free pass
Flexible layout solves half the problem. The other half is that a 4000px-wide
photo doesn’t need to be downloaded in full on a phone screen that will only
ever display it at 400px — doing so wastes bandwidth and slows the page down
for exactly the users most likely to be on a constrained connection.
Responsive images solve this with the srcset and sizes attributes:
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 50vw"
alt="A description of the photo"
/>
srcset lists the available file sizes. sizes tells the browser how wide
the image will actually render at different viewport widths — in this
example, full viewport width below 600px, half the viewport above it. The
browser combines that information with the device’s actual screen size and
pixel density to pick the smallest file that will still look sharp, without
you writing any JavaScript or media queries for images specifically. This is
one of the rare cases where the browser is doing real decision-making on
your behalf, and it only works because you gave it enough information to
make that decision.
Responsive design is a mindset, not a checklist
None of these tools work in isolation. A page can have a perfect clamp()
scale and still break if the underlying layout is still fixed-width Flexbox
or Grid tracks. Responsive design means treating flexibility as the default
assumption at every layer — layout, typography, and media — rather than
bolting a handful of breakpoints onto a layout that was really designed for
one specific screen size.
Check yourself
Why does mobile-first CSS use min-width media queries instead of max-width ones?
Mobile-first writes the simplest, smallest-screen layout as the default, unconditional CSS, then layers on additional rules only once the viewport grows past a breakpoint. min-width queries add complexity as space increases, which matches that direction; max-width queries would mean writing the full desktop layout first and subtracting from it, the opposite of the mobile-first approach.
What determines where you should place a breakpoint in a real project?
Wherever your own content and layout actually start to look broken or cramped, found by resizing the browser and watching it happen — not a fixed list of device widths, since devices and window sizes vary too much for any fixed list to stay accurate.
What problem does clamp() solve that a single media query override doesn't?
A media query override changes a value abruptly at one breakpoint, producing a visible jump. clamp(min, preferred, max) scales the value continuously between its bounds as the viewport changes, so the size adjusts smoothly instead of snapping at specific widths.
Why would a browser download a smaller image file for the same <img> tag on a phone than on a desktop monitor?
When the img tag has a srcset listing multiple image sizes and a sizes attribute describing how large the image will be rendered at different viewport widths, the browser picks whichever listed file is appropriately sized for the actual rendered width and the screen's pixel density, rather than always fetching the largest one.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.