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,orandnot. A comma-separated list matches if any query matches. - A leading
notnegates the whole query, type included:not screen and (color)means "not (a colour screen)".onlyexists 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.hoverandpointer(none/coarse/fine) describe the primary input;any-hover/any-pointermatch 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.