In a media query you write a condition such as "if the screen is at least 768 pixels wide" or "if the user prefers dark mode", and the browser uses the CSS inside it only while that condition holds. Media queries are what let one HTML page show a single column on a phone and three columns on a laptop, which is the core of responsive design.
What a media query looks like
Most media queries live in a @media rule in your stylesheet:
/* Base styles: phones first */
.products { display: grid; grid-template-columns: 1fr; }
/* From 768px wide: two columns */
@media (width >= 768px) {
.products { grid-template-columns: 1fr 1fr; }
}
/* From 1200px wide: three columns */
@media (width >= 1200px) {
.products { grid-template-columns: repeat(3, 1fr); }
}
Writing the small-screen styles first and adding media queries for wider screens is called mobile-first CSS. Each query only adds what changes.
The width >= 768px form is the newer range syntax. The older form, (min-width: 768px), means the same thing and still works. All major browsers support the range syntax since Safari 16.4 in March 2023 (MDN).
The parts of a media query
A query can combine three things (MDN):
- A media type, optional:
screen,printorall(the default). The other types from older specs, liketvorhandheld, are deprecated. - Media features in parentheses, which test one characteristic:
(orientation: landscape),(hover: hover). - Logical operators to combine them:
and,not,or, and commas, which work like "or" between whole queries. The oldonlykeyword has no effect in modern browsers.
@media screen and (width >= 30em) and (orientation: landscape) { … }
@media print { nav, .ads { display: none; } }
Media features worth knowing
| Feature | What it tests | Typical use |
|---|---|---|
width, height |
The size of the viewport | Layout breakpoints |
orientation |
Portrait or landscape | Rearranging wide content |
hover, pointer |
Whether the main input can hover, and how precise it is | Bigger touch targets, no hover-only menus on phones |
prefers-color-scheme |
Whether the user asked for a light or dark theme | Dark mode |
prefers-reduced-motion |
Whether the user turned on "reduce motion" on their device | Switching off parallax and large animations |
All of these work in every current major browser (MDN). One you may read about, prefers-reduced-data, is still experimental and missing from widely used browsers, so don't rely on it yet.
Don't forget the viewport meta tag
Media queries for width only work as expected on phones if your page has this line in the <head>:
<meta name="viewport" content="width=device-width, initial-scale=1">
Without it, many mobile browsers lay the page out in a wider virtual window, for example 980 pixels, and shrink the result to fit the screen (MDN). Your phone styles never kick in, and the text comes out tiny. See viewport.
Media queries outside CSS
The same conditions appear in other places. The media attribute on <link> loads a stylesheet only for some media, and on <source> inside <picture> it picks a different image for different screens (see responsive images). In JavaScript, window.matchMedia() tests a query and can react when the result changes.
Why media queries matter for SEO
Google indexes and ranks the mobile version of your pages, which is called mobile-first indexing. Of the ways to build a mobile site, Google recommends responsive design, because the same HTML on the same URL is the easiest to build and maintain (Google Search Central). Media queries are how that one page adapts.
Two cautions follow. First, keep the mobile content equivalent to the desktop content. Putting text in tabs or accordions on small screens is fine; leaving important content out of the mobile layout isn't, because the mobile version is what Google uses. Second, test the result on a real phone or a narrow browser window. A layout that overflows sideways or hides the main button frustrates visitors whatever your rankings are.
Media queries and accessibility
Media queries also make accessibility rules easier to meet. WCAG 2.2's Reflow criterion (Level AA) asks that content work without scrolling in two directions at a width equivalent to 320 CSS pixels (W3C), which is what a zoomed-in desktop browser looks like. A responsive layout handles that for free. Respecting prefers-reduced-motion helps people who feel ill from movement on screen. More in accessibility.
Media queries or container queries?
Media queries look at the whole viewport or device. Container queries, a newer CSS feature, look at the size of the element's container instead. They suit components that appear in different places, such as a product card that sits in a narrow sidebar on one page and a wide grid on another. Use media queries for page-level layout and user preferences, container queries for components that need to fit wherever you put them.