Responsive design — media queries and mobile-first
Mobile-first isn't a slogan about which device matters more — it's a specific, mechanical choice about which direction min-width media queries layer on top of each other, and it changes how much CSS you actually have to write.
3 min read
What a media query actually does
.card { padding: 12px; }
@media (min-width: 768px) {
.card { padding: 24px; } /* only applies when the viewport is AT LEAST 768px wide */
}A media query wraps a block of CSS in a condition — most commonly a viewport-width threshold — that the browser continuously re-evaluates as the window resizes, applying or removing the wrapped rules live, with no JavaScript or page reload involved. min-width: 768px means "apply this when the viewport is 768px or wider" — the condition is about a lower bound, not an exact match, which is the detail the next section builds directly on.
Mobile-first: writing the small-screen styles as the default, then layering up
/* Base styles = the SMALLEST screen's layout, no media query needed at all */
.nav { display: block; }
@media (min-width: 768px) {
.nav { display: flex; } /* tablet and up: switch to a horizontal flex layout */
}
@media (min-width: 1200px) {
.nav { gap: 32px; } /* desktop and up: add more breathing room */
}"Mobile-first" means the unwrapped, default CSS is the small-screen layout, and each min-width media query only adds or overrides styles for progressively larger screens — never the reverse. Because min-width queries are additive going up, each breakpoint only needs to specify what's different from the screen size below it, not restate the whole layout — a 1200px rule doesn't need to repeat display: flex, since that's already active from the 768px breakpoint below it and stays active unless something later overrides it.
The alternative (desktop-first with max-width) and why it usually means more CSS
/* Desktop-first — base styles are the LARGEST screen's layout */
.nav { display: flex; gap: 32px; }
@media (max-width: 767px) {
.nav { display: block; gap: 0; } /* have to explicitly UNDO both properties for mobile */
}Writing the desktop layout as the default and using max-width queries to override it downward means every mobile-specific override has to explicitly undo whatever the desktop styles set — here, both display and gap need resetting, not just the one property that's actually different in spirit. This isn't wrong, exactly, but mobile-first with min-width tends to produce less CSS overall in practice, since additive overrides rarely need to fight the base styles the way subtractive ones do — one real, practical reason mobile-first became the more common default recommendation.
The viewport meta tag: the one line that makes any of this work on an actual phone
<meta name="viewport" content="width=device-width, initial-scale=1" />Without this tag, mobile browsers render the page at a fixed desktop-width virtual viewport (historically 980px) and then zoom the whole thing out to fit the screen — meaning every media query breakpoint is evaluated against that fake 980px width, not the phone's actual screen width, so a 768px breakpoint would never even trigger on a real phone. This single <meta> tag tells the browser to use the device's actual width as the viewport instead, which is a genuine prerequisite for any of the media queries above to behave the way they're written to — a real, common source of "my responsive styles aren't working on mobile" bugs when this tag is missing.
Further reading
Check your understanding
A quick comprehension check — not tracked, not graded, just for you.
1. What does a `min-width` media query condition actually check?
2. What does "mobile-first" mean mechanically, in terms of CSS?
3. Why is the viewport meta tag (`width=device-width, initial-scale=1`) a genuine prerequisite for responsive breakpoints to work on phones?