Media Queries: Media Types, Range Syntax, and User-Preference Features
Writing media queries well: syntax, media types, and, or and not, the range syntax for widths, input features like hover and pointer, user-preference features for dark mode, reduced motion, contrast and forced colors, and matchMedia().
kvi5hnuCode Sprout team member7 min readOn this page
A media query applies CSS only when a condition about the environment is true: the viewport is at least a certain width, the device has a mouse, the user prefers dark mode, the page is being printed. Article 75 used them for layout breakpoints; this article covers the full syntax and the features that matter most, especially the user-preference features that let a site respect accessibility settings.
Syntax
@media (width >= 48em) { … } /* a feature in parentheses */
@media screen and (orientation: landscape) { … } /* a media type, then features */
@media print { … } /* a type alone */
<link rel="stylesheet" href="print.css" media="print">
<source media="(width >= 60em)" srcset="wide.avif">
The same conditions work in @import … screen, the media attribute of <link>, <style> and <source>, and matchMedia() in JavaScript.
Media types
Only three are useful: screen, print (article 81) and all, the default. Older types like handheld, tv and projection are deprecated and match nothing. Tested in Chrome: matchMedia('handheld').matches is false. A type must come first: (width > 30em) and print is invalid, and Chrome turns it into not all.
Combining conditions
| Syntax | Meaning |
|---|---|
(a) and (b) | both are true |
(a), (b) | either is true: a comma-separated list, where each query stands alone |
(a) or (b) | either is true, within one query |
not (a) | the condition is false |
only screen | an old trick to hide queries from very old browsers; unnecessary today |
Tested in Chrome: (min-width: 400px) or (max-width: 100px) and not (hover: hover) both parse and evaluate. An unknown feature such as (foo: bar) is false, but doesn't invalidate the rest of a comma-separated list.
Range syntax
The old min- and max- prefixes are inclusive and awkward for ranges. The range syntax uses comparison operators, and works in all browsers since 2023:
@media (width >= 48em) { … } /* same as (min-width: 48em) */
@media (width < 48em) { … } /* below, without the 47.99em trick */
@media (30em <= width < 60em) { … } /* a band between two breakpoints */
@media (aspect-ratio > 16 / 9) { … } /* wider than 16:9 */
< and > make it easy to write breakpoints that neither overlap nor leave a gap: (width < 48em) and (width >= 48em) cover every width exactly once. Values can use calc(): tested in Chrome, (width >= calc(20em + 10px)) works.

Notice that (width < 48em) and (width >= 48em) never match together, and exactly one of them matches in each viewport. hover and pointer describe the device, not the viewport, so they're the same in both.
Viewport and display features
| Feature | Use |
|---|---|
width, height | the viewport size; width is the main one for layout |
aspect-ratio, orientation | portrait or landscape; orientation is only a rough hint (with interactive-widget=resizes-content, an on-screen keyboard can flip it), so prefer widths |
resolution | pixel density: (resolution >= 2x); prefer srcset or image-set() for images (article 53) |
color-gamut | srgb, p3, rec2020: whether wide-gamut colors can be shown |
dynamic-range | high on HDR screens |
display-mode | standalone when a web app is installed and opened from the home screen |
update | none for print, slow for e-ink: avoid animations |
scripting | none when JavaScript is disabled |
Input features: hover and pointer
These describe the primary input device:
| Feature | Values |
|---|---|
hover | hover if the primary input can hover, like a mouse; none for touch |
pointer | fine (mouse, stylus), coarse (finger), none |
any-hover, any-pointer | the same, true if any available input matches |
@media (hover: hover) {
.card:hover { translate: 0 -2px; box-shadow: 0 6px 16px rgb(0 0 0 / 0.15); }
}
@media (pointer: coarse) {
.toolbar button { min-block-size: 2.75rem; } /* bigger targets for fingers */
}
Don't treat these as "mobile" or "desktop" detection: laptops have touch screens, tablets have keyboards and trackpads. Use them for what they describe, hover effects and target size, and make sure everything still works without hover.
User-preference features
Operating systems let people set preferences for their needs, and these features expose them to CSS. Respecting them is a large part of accessible design.
| Feature | Values | Use |
|---|---|---|
prefers-color-scheme | light, dark | dark mode (article 34) |
prefers-reduced-motion | no-preference, reduce | remove or soften animations (article 112) |
prefers-contrast | no-preference, more, less, custom | stronger borders and text contrast |
forced-colors | none, active | Windows high-contrast modes, where the user's colors replace yours |
prefers-reduced-transparency | no-preference, reduce | solid backgrounds instead of translucent ones; Chromium only (Firefox behind a flag) |
inverted-colors | none, inverted | Safari only (Firefox behind a flag) |
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; }
}
@media (prefers-contrast: more) {
:root { --text-muted: #333; --border: #000; }
}
@media (forced-colors: active) {
.button { border: 1px solid ButtonText; } /* borders stay visible when backgrounds are removed */
}
(prefers-reduced-motion) with no value is true for any value other than no-preference, so it's a short way to write reduce. Forced colors and system colors are covered with color contrast in article 35.
prefers-reduced-data, for saving bandwidth, isn't supported in any browser by default (Chromium has it behind a flag); don't rely on it.
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
.report { list-style: none; padding: 0; display: grid; gap: 0.3rem; }
.report li { display: flex; justify-content: space-between; padding: 0.35rem 0.6rem; border-radius: 6px; background: #f4efe6; }
.report li.on { background: #dff0e5; font-weight: 600; }
.hint { color: #555; }

Only the custom properties change inside the prefers-color-scheme: dark query; color-scheme: light dark makes the browser switch the button, the input and the scrollbars to match.
Media queries in JavaScript
matchMedia() evaluates a query and can watch for changes:
const reduce = matchMedia('(prefers-reduced-motion: reduce)');
if (!reduce.matches) startCarousel();
reduce.addEventListener('change', e => e.matches ? stopCarousel() : startCarousel());
Use it for behavior that CSS can't express, like whether to autoplay something; keep styling in CSS.
Media queries and custom properties
var() doesn't work inside a media query condition: @media (width >= var(--bp)) is kept as an unknown condition that never matches, because media queries don't depend on any element and custom properties belong to elements (article 27). Tested in Chrome with --bp: 1px on the root: neither the query nor its not version matched. Breakpoints have to be repeated as literal values, or generated by a preprocessor. Custom media queries solve this: @custom-media --wide (width >= 60em); then @media (--wide) { … }. They're in Firefox 148 so far, not in Chrome or Safari, so use them only with a build step that compiles them away.
What's current
- Range syntax: all browsers since 2023.
prefers-color-scheme,prefers-reduced-motion,prefers-contrastandforced-colorswork in all browsers.prefers-reduced-transparency: Chromium only (Firefox behind a flag);scripting: all browsers.- Container queries (article 78) respond to a component's container instead of the viewport, which is often what layout needs.
Common questions
What's the difference between , and or?
They mean the same thing logically. A comma separates complete queries, each with its own media type; or combines conditions inside one query.
Why doesn't @media (width >= var(--bp)) work?
Custom properties can't be used in media query conditions.
How do I detect a touch device?
Don't detect devices; query what matters. (hover: none) for no hover, (pointer: coarse) for imprecise pointing.
Should I use min-width or the range syntax?
The range syntax: it's clearer and avoids overlapping breakpoints. Both work.
Summary
- Media queries combine an optional media type (
screen,print,all) with features in parentheses, usingand,or,notand commas. - The range syntax,
(width >= 48em)and(30em <= width < 60em), replacesmin-andmax-. hoverandpointerdescribe input capabilities, not device types.- User-preference features (
prefers-color-scheme,prefers-reduced-motion,prefers-contrast,forced-colors) let a site respect accessibility settings. matchMedia()evaluates and watches queries in JavaScript;var()can't be used in conditions.
Practice questions
Rewrite these queries with the range syntax.
@media (min-width: 30em) and (max-width: 59.99em) { … }
Answer
@media (30em <= width < 60em) { … }
Write CSS that adds a hover lift to cards only on devices that can hover.
Answer
@media (hover: hover) {
.card:hover { translate: 0 -2px; }
}
Why is this query invalid, and how do you fix it?
@media (width > 40em) and print { … }
Answer
The media type must come first. Write @media print and (width > 40em).
Which feature should a site check to replace translucent, blurred panels with solid ones for users who asked for it?
Answer
prefers-reduced-transparency: reduce. It's supported in Chromium browsers; Firefox has it behind a flag, and Safari doesn't support it, so the translucent design must stay readable without it.
Previous: Responsive Design: Mobile First, the Viewport Meta Tag and Breakpoints

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.