Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

How do media queries work, including range syntax and prefers-* features?

Questions in this chapter (6)
  • Draft
  • easy
  • Askedvery often
  • Verified October 2026

Answer

A media query is a condition on the environment the document renders in, such as screen and (width >= 48em) and (hover: hover). It appears in @media, <link media>, <source media> and matchMedia().

  • Media types: all, screen, print. The others (tv, handheld) are deprecated and never match.
  • Features sit in parentheses and combine with and, or and not. A comma-separated list matches if any query matches.
  • A leading not negates the whole query, type included: not screen and (color) means "not (a colour screen)". only exists to hide a query from pre-media-query browsers and does nothing today.

Range syntax (Media Queries 4) compares with <, <=, >, >= and can bound both sides: (400px <= width < 768px). The old min-width/max-width prefixes mean >= and <=, so both ends are inclusive. It's supported since Chrome 104, Firefox 102 and Safari 16.4.

Key features:

  • width/height: the viewport, including any scrollbar. device-width (the screen) is deprecated.
  • orientation, aspect-ratio, resolution.
  • hover and pointer (none/coarse/fine) describe the primary input; any-hover/any-pointer match if any input qualifies.
  • User preferences: prefers-reduced-motion, prefers-color-scheme, prefers-contrast, forced-colors.

Relative units use initial values: em is the user's default font size, ignoring page styles.

In JavaScript, matchMedia(query) returns a MediaQueryList with .matches and a change event, so scripts react to the same conditions as CSS.

Example

// A 600 × 800 px viewport with a mouse
const test = (query) => matchMedia(query).matches;

test("(min-width: 600px)");
// → true
test("(width > 600px)");
// → false
test("(400px <= width <= 700px)");
// → true
test("screen and (orientation: portrait)");
// → true
test("print, (width >= 600px)");
// → true
test("not screen and (width > 600px)");
// → true
test("(hover: hover) and (pointer: fine)");
// → true
test("(width >= 37.5em)");
// → true
test("(colour: red)");
// → false
test("not (colour: red)");
// → false

Chrome 156 and Firefox 158 agreed. colour isn't a feature, so it evaluates to unknown, and negating unknown is still unknown, which counts as false.

Pitfalls

  • Pairing max-width: 767px with min-width: 768px. The spec warns that fractional viewport widths (zoom, non-integer pixel ratios) fall between them and neither applies; width < 768px / width >= 768px leaves no gap.
  • Using hover: hover to detect "desktop". It describes only the primary input: the spec says a touchscreen device with an optional mouse should match hover: none. Use any-hover if any hover-capable input will do.
  • Expecting html { font-size: 62.5% } to change em or rem breakpoints. Media queries resolve them against the initial font size.
  • Writing a typo'd or unsupported feature inside not (…) as a fallback. It doesn't flip to true; the whole query is false.

Follow-ups

How do you react to a preference change in JavaScript?
Listen for change on the MediaQueryList. In Chrome 156, emulating prefers-reduced-motion: reduce fired change with matches: true, and a @media rule setting transition-duration: 0s applied at the same time.
What does prefers-reduced-motion: reduce require of you?
That the user asked the system to minimise non-essential motion, so remove or replace motion-based animation that can cause discomfort or distraction. The media query only reports the setting; which animations to change is an accessibility decision.
Why prefer width to device-width?
device-width is the screen's size, not the space the document has. It ignores window size and split screens, and the spec deprecates it.

Exercises

Negation and unknown features

// A 600 px wide viewport
matchMedia("not screen and (width < 600px)").matches;
matchMedia("(min-orientation: portrait)").matches;
matchMedia("not (min-orientation: portrait)").matches;

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.