CSS Reference ยท Media Queries and Features
The media query range syntax
Reference for the modern media query range syntax: compare features with >, <, >=, <= and =, write between-ranges like 400px <= width <= 800px, with examples.
The range syntax lets you write media queries for range features such as width, height, aspect-ratio and resolution with comparison operators instead of min- and max- prefixes. (width >= 600px) reads exactly like it sounds, and a two-sided query like (400px <= width <= 800px) replaces two conditions joined with and.
@media (width >= 600px) { /* ... */ } @media (400px <= width <= 800px) { /* ... */ }
Try it
The box changes color at three width thresholds. Resize the preview to see each one apply.
Syntax
@media (feature <op> value) { } @media (value <op> feature) { } @media (value <op> feature <op> value) { } /* <op> is one of: = < <= > >= */
The two operators in a double-sided range must point in the same direction: 400px <= width <= 800px is valid, 400px <= width >= 800px is not.
Values
| Syntax | Description |
|---|---|
(width >= 600px) |
Width is 600px or more. Same as (min-width: 600px). |
(width <= 600px) |
Width is 600px or less. Same as (max-width: 600px). |
(width < 600px) |
Width is strictly less than 600px. No prefix form for this. |
(width > 600px) |
Width is strictly greater than 600px. |
(width = 600px) |
Width is exactly 600px. Same as (width: 600px). |
(400px <= width <= 800px) |
Width is in the range, endpoints included. |
Usage notes
- Strict comparisons are the big win.
min-widthandmax-widthare always inclusive, so splitting at 600px with both means 600px matches both queries. With(width < 600px)and(width >= 600px)there is no overlap and no gap. - The old "0.02px" hack goes away. Authors used
max-width: 599.98pxto dodge the overlap. Use<instead. - Works with range features only. Features like
width,height,aspect-ratio,resolution,colorandmonochrome. Discrete features such asorientationorhoverstill use the colon form. - Mixed units are fine.
(width >= 40em)works, and em units are based on the browser's default font size, not the page's. - Same in container queries.
@container (inline-size > 30em)uses the identical syntax. - Can combine.
@media (width >= 600px) and (orientation: landscape)mixes both styles. - Keep one style in a stylesheet so readers do not have to translate between forms.
Browser support
Supported in all current major browsers; very old browsers do not understand it, so check a compatibility table if you need to support them, and keep min-width and max-width there.
Related
width and heightโ the most common range featurescontainer featuresโ the same syntax in@container@mediaโ the at-rule- Lessons: Media Queries, Responsive Design