CSS Reference ยท At-rules
The @media at-rule
Reference for the CSS @media at-rule: media types, width and range queries, user preference features, logical operators, syntax and live examples.
The @media at-rule wraps a block of CSS in a condition. The rules inside apply only while the condition is true, and the browser re-checks it whenever the situation changes: the window is resized, the device is rotated, the user switches to dark mode or starts printing.
A condition is a media query. It can name a media type (screen, print), one or more media features (width, orientation, prefers-color-scheme), and combine them with and, not, only and commas.
@media (min-width: 40rem) { .sidebar { display: block; } }
Try it
Syntax
@media <media-query-list> { /* rules */ } /* a media query */ [ not | only ]? <media-type> [ and <condition> ]? <condition> /* media type omitted means "all" */ /* range syntax */ @media (width >= 600px) and (width < 1000px) { } @media (400px <= width <= 700px) { }
- Media types:
all,screen,print. Older types such astvorhandheldare deprecated and never match. - Logical operators:
and,not(negates the entire query),only(hides the rule from very old browsers), and a comma, which means "or". - Nesting:
@mediacan sit inside style rules (CSS nesting) and inside other conditional at-rules such as@supportsor@layer.
Values
| Syntax and arguments | Description |
|---|---|
(width), (min-width: 40rem) |
Viewport width; min- and max- prefixes are inclusive. |
(width >= 40rem) |
Range syntax with <, <=, >, >=, =. |
(height), (aspect-ratio: 16/9) |
Viewport height and shape. |
(orientation: portrait | landscape) |
Taller than wide, or the reverse. |
(prefers-color-scheme: dark) |
The user chose a dark theme. |
(prefers-reduced-motion: reduce) |
The user asked for less animation. |
(prefers-contrast: more) |
The user asked for stronger contrast. |
(hover: hover), (pointer: fine) |
The primary input can hover or is precise. |
(resolution >= 2dppx) |
High-density screens. |
print |
Applies when printing or in print preview. |
not print, screen and (...) |
Combinations with type and operators. |
Usage notes
- Use
remoremfor width breakpoints so the layout respects a user's larger default text size. Both are based on the browser's initial font size inside media queries, not on the element's. - Mobile first. Write the small-screen styles unconditionally, then add
min-widthqueries to enlarge the layout. See the mobile-first lesson. - Do not target devices. Pick breakpoints where your content breaks, not at the width of a particular phone.
- Respect user preferences. Wrap large animations in
@media (prefers-reduced-motion: no-preference)or switch them off underreduce. - Media queries see the viewport, not the component. For components that live in different slots, use
@containerinstead. - Comma means or.
@media (width < 400px), printmatches either case. Writeandbetween features that must both hold. - Mixing range and
min-syntax in one query is allowed but confusing; stay with one style. - Linking sheets. The same query works in HTML:
<link rel="stylesheet" media="print" href="print.css">.
Browser support
Media queries work everywhere; the range syntax (width >= 600px) is Baseline since 2023, and the user preference features are widely available in current browsers.
Related
@containerโ component-level queries@supportsโ feature queries@importโ can attach a media query to an imported sheet- Lesson: Media Queries
- Lesson: Mobile First
- Lesson: Dark Mode