Media Queries and Mobile-First
How media queries make styles conditional on viewport and device, why range syntax reads better than min-width and max-width, and what mobile-first buys.
Media Queries and Mobile-First
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A media query is an `if` for CSS: the rules inside `@media (condition) { ... }` apply only when the condition is true. The conditions describe the viewport (width, height, orientation) and the user's device and preferences.
- Write breakpoints in range syntax: `@media (width >= 40rem)` and `@media (400px <= width <= 700px)`. It means the same as `min-width` and `max-width` and is Baseline, in every major browser since March 2023.
- Mobile-first means the base CSS is the small-screen layout and `min-width` queries add complexity as space appears. The small screen gets the least CSS and nothing has to be undone.
- Breakpoints in `em` or `rem` units follow the user's browser font size, so a layout reflows for people who have enlarged their text. In media queries, `em` and `rem` both mean the browser's default font size, not your CSS.
- Media queries only know about the viewport, not the size of the element they style. For component-level responsiveness, use container queries in the next lesson.
An if for CSS
Pages are viewed on a phone held upright, a laptop, a projector, a printer and a screen reader. A single fixed layout can’t suit them all. A media query lets a stylesheet say “only apply these rules when this is true”:
.cards { display: grid; gap: 1rem; }
@media (width >= 40rem) {
.cards { grid-template-columns: 1fr 1fr; }
}
Below 40rem the .cards are one column, because that is the default grid. At and above it, they are two. The condition inside the parentheses is a media feature. Features include width, height, orientation, resolution, and also device and user traits such as hover, pointer and the prefers- family from the dark mode lesson. Media types also exist: @media print is the hook for print stylesheets.
The viewport tag
Before any of this works on a phone, the HTML needs:
<meta name="viewport" content="width=device-width, initial-scale=1">
Mobile browsers started out pretending the screen was about 980px wide, so desktop pages would fit, and then shrunk the result. This tag tells the browser to use the real device width. Without it, your max-width: 600px query on a phone never matches, because the browser thinks the viewport is wide.
Range syntax
For years the conditions were min-width and max-width, which are awkward to read and impossible to write for exclusive bounds. Media queries now accept comparison operators:
@media (width >= 40rem) { /* 40rem and up */ }
@media (width < 60rem) { /* below 60rem */ }
@media (40rem <= width < 60rem) { /* between, upper bound exclusive */ }
Checked with matchMedia in a browser at 800px wide: (width >= 600px) was true, (400px <= width <= 700px) was false, and (width > 800px) was false. The range syntax is Baseline, in every major browser since March 2023. Old min-width/max-width code isn’t wrong, and it still works. In Kevin Powell’s video the range syntax sits right after a section on creating ranges the old way.
Mobile-first
Mobile-first is a way of organising the cascade. The base CSS, outside any query, is the small-screen layout. Each query adds to it as space opens up.
.layout { display: grid; gap: 1rem; } /* base: one column */
@media (width >= 48rem) { .layout { grid-template-columns: 220px 1fr; } }
@media (width >= 72rem) { .layout { grid-template-columns: 220px 1fr 200px; } }
There are two reasons it is the default recommendation. The small screen is usually the simplest layout, and a phone shouldn’t have to download and parse and then undo desktop rules. And you never have to write a rule that cancels another, such as resetting a float or width, which is where bugs hide. Desktop-first with max-width queries gets to the same end, but you undo as the screen shrinks.
It isn’t a law. A component that only makes sense on wide screens can use a max-width query to switch something off, and with the intrinsic techniques in intrinsic and fluid layouts many layouts need no query at all. The runner-up video by Kevin Powell is a more nuanced look at when max-width is the better choice.
Choosing breakpoints
Don’t chase device sizes: there are too many. Resize the browser, and add a breakpoint where the content starts to look bad, such as when a line of text gets too long, or cards get squeezed. Use rem for the numbers. In a media query, rem and em both resolve against the browser’s default font size, not your own html { font-size }, so a user who has enlarged their text gets the narrow layout sooner. A breakpoint in px ignores that setting.
Beyond width
Media queries also ask about the device. Two worth knowing:
@media (hover: hover)is true when the primary input can hover, which is the right condition for hover-only effects. Touch screens sayhover: none.@media (pointer: coarse)is true for imprecise pointers like fingers, which is when to make tap targets larger.
And the prefers- queries ask about the user’s own settings. Those are the subject of a later lesson.
What the video covers
The embed is a clip, 1:27 to 12:05, from Kevin Powell’s “Learn how to use Media queries & Container queries” (January 2024, about thirty-four minutes). The chapters in that range are: media query basics, updating custom properties with media queries, creating ranges, the new syntax, and media queries being for more than only the size of the viewport. The rest of the video, from 12:07, is about container queries and belongs to the next lesson. The video does not have a chapter on mobile-first as a strategy, so that section and the viewport tag are the prose’s own. I could not read a transcript, so I do not describe the demos beyond the chapter titles.
Where this fits
Media queries are the oldest tool in responsive design, and the rest of this group works around their limits. Container queries respond to a component’s own width, viewport units scale with the screen, and intrinsic layouts avoid breakpoints altogether. For the shorter first look, see the frontend roadmap’s responsive design and media queries.
Check yourself
What does `@media (400px <= width <= 700px)` match, and how would you write it the older way?
It matches viewports from 400px to 700px wide, inclusive. The older form is `@media (min-width: 400px) and (max-width: 700px)`. In a browser at 800px wide, the range `(width >= 600px)` is true and `(400px <= width <= 700px)` is false.
A page looks right on desktop but is tiny and zoomed out on a phone, and media queries never trigger. What is probably missing?
The viewport meta tag, `<meta name="viewport" content="width=device-width, initial-scale=1">`. Without it, mobile browsers render a wide virtual viewport (around 980px) and scale it down, so a `max-width: 600px` query never matches.
Why is mobile-first called the default approach?
The base styles describe the simplest layout, usually a single column, and each `min-width` query adds to it as space increases. A phone only needs the base rules, and there are no desktop styles to override or reset. The reverse (desktop-first with `max-width` queries) works but means undoing layout as space shrinks.
You set a breakpoint with `@media (min-width: 768px)`. A user raises their browser's default font size and the layout stays cramped. How could you have avoided it?
Use relative units: `@media (min-width: 48rem)`. In media queries, `rem` and `em` are based on the browser's default font size, so the breakpoint moves up when the user's text gets bigger, and the layout switches to the single-column version sooner.
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.