Sub topic 11
Range syntax for media queries
This chapter assumes you can write min-width and max-width media queries, combine conditions with and, and read em and rem values. Range syntax is a newer way to write the same width conditions. It comes from Media Queries Level 4. It replaces the min- and max- prefixes with comparison operators such as <= and >, so a query reads like a math inequality.
Why a new syntax exists
The older form expresses a range with two prefixed features:
@media (min-width: 30em) and (max-width: 59.99em) {
.sidebar {
display: none;
}
}This works, but it has two weaknesses.
- Both bounds are inclusive.
min-widthmeans "greater than or equal to" andmax-widthmeans "less than or equal to". There is no way to say "strictly below 60em". - The
59.99emworkaround is fragile. To stop a range just before the next breakpoint, authors subtract a small amount. A viewport can have a fractional width, for example on a zoomed page or a high-density display. A width of 59.995em would then match neithermax-width: 59.99emnormin-width: 60em, so neither rule applies and a layout gap appears.
Range syntax removes both problems because it supports strict comparisons.
The operators
Range syntax uses five comparison operators.
| Operator | Meaning |
|---|---|
| `<` | less than |
| `<=` | less than or equal to |
| `>` | greater than |
| `>=` | greater than or equal to |
| `=` | equal to |
Write the feature name, an operator, and a value inside parentheses:
@media (width >= 40em) {
.nav {
flex-direction: row;
}
}This is equivalent to (min-width: 40em). The feature name is width, with no prefix.
The value can also go on the left. Then you read the inequality from the value's side:
@media (40em <= width) {
.nav {
flex-direction: row;
}
}Both forms mean "the viewport is at least 40em wide". Pick the one that reads more naturally. Most teams use the feature-first form for single bounds.
Writing a range with two bounds
The main benefit appears when you need both a lower and an upper bound. Put the feature in the middle and a bound on each side:
@media (400px <= width <= 700px) {
.banner {
padding: 0.5rem;
}
}This rule applies when the viewport is between 400px and 700px wide, inclusive at both ends. The two operators must point the same way, so the values run from smallest to largest, left to right.
You can mix inclusive and exclusive bounds freely:
@media (40em <= width < 64em) {
.cards {
grid-template-columns: repeat(2, 1fr);
}
}The range starts at 40em, includes it, and stops just before 64em. At exactly 64em this rule no longer applies, so a rule for (width >= 64em) takes over with no overlap and no gap. This replaces the 63.99em workaround and has no gap at fractional widths.
Translating between the two syntaxes
| Older syntax | Range syntax |
|---|---|
| `(min-width: 600px)` | `(width >= 600px)` |
| `(max-width: 600px)` | `(width <= 600px)` |
| `(min-width: 30em) and (max-width: 60em)` | `(30em <= width <= 60em)` |
| `(min-width: 30em) and (max-width: 59.99em)` | `(30em <= width < 60em)` |
| `(width: 600px)` | `(width = 600px)` |
Exact-width matching with = is rarely a good idea. A viewport is seldom exactly 600px wide, and the rule silently fails everywhere else.
Features that accept ranges
Range syntax works only with features that take a continuous value. These include:
widthandheightaspect-ratioresolutioncolorandcolor-index
It does not work with discrete features, which have a fixed set of keyword values. hover, pointer, prefers-color-scheme, and prefers-reduced-motion all stay in their colon form, for example (prefers-color-scheme: dark).
Here are range queries on other features:
@media (height < 500px) {
.site-header {
position: static;
}
}
@media (aspect-ratio > 16/9) {
.hero {
max-height: 60vh;
}
}
@media (resolution >= 2dppx) {
.logo {
background-image: url("[email protected]");
}
}Each of these would otherwise need a min- or max- prefix, such as min-height or min-resolution.
Combining range queries with logic
Range conditions are ordinary media conditions, so the operators from the earlier chapter still apply. Join them with and, separate alternatives with a comma, and negate with not:
@media screen and (width >= 40em) and (orientation: landscape) {
.gallery {
columns: 3;
}
}
@media (width < 30em), (height < 400px) {
.toolbar {
font-size: 0.875rem;
}
}
@media not (width >= 60em) {
.wide-only-note {
display: none;
}
}With not, the condition must be wrapped in parentheses. Write not (width >= 60em), not not width >= 60em. The last rule above is equivalent to (width < 60em), and the direct form is usually clearer. Reserve not for conditions that have no simple opposite.
Level 4 also allows or between conditions. Support for or arrived at the same time as range syntax, but the comma form works everywhere and needs no new knowledge.
A complete example
This page uses range syntax for a three-step card grid. Save it as an HTML file and resize the window to see the columns change.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Range syntax demo</title>
<style>
.cards {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
}
.card {
padding: 1rem;
background: #e8eefc;
border-radius: 0.5rem;
}
@media (40em <= width < 64em) {
.cards {
grid-template-columns: repeat(2, 1fr);
}
}
@media (width >= 64em) {
.cards {
grid-template-columns: repeat(3, 1fr);
}
}
</style>
</head>
<body>
<div class="cards">
<div class="card">One</div>
<div class="card">Two</div>
<div class="card">Three</div>
<div class="card">Four</div>
<div class="card">Five</div>
<div class="card">Six</div>
</div>
</body>
</html>The base styles give one column. This keeps the mobile-first approach: the unqualified rules are the small-screen layout, and queries add complexity. Range syntax is only a different way to write each condition. The two queries cannot overlap, because the first stops strictly below 64em and the second starts at 64em.
Units inside range queries
Relative units behave the same in range syntax as in min-width. In a media query, em and rem both refer to the browser's initial font size, usually 16px. They do not depend on any element's font size or on a font-size set on html. A breakpoint of 40em is therefore 640px by default, and it grows if the user raises the browser's default font size. This is a reason to prefer em over px for breakpoints.
Values must be constants. You can use calc() with fixed numbers, but you cannot use var(--bp). Custom properties are not available inside media queries, which is a common source of confusion.
Browser support and fallbacks
Range syntax is supported in current Chrome, Edge, Firefox, and Safari. Chrome and Edge added it in version 104, Firefox in 63, and Safari in 16.4. Check a compatibility table before relying on it for older audiences.
A browser that does not understand a media query treats it as false, so it skips the whole block. There is no partial application. If a missing rule would break your layout, take one of these options:
- Keep essential styles in the base layer, outside any query, so an old browser still gets a usable single-column page.
- Use the
min-widthandmax-widthforms for any rule that must work everywhere. - Use a build step such as a CSS post-processor that rewrites range syntax into prefixed form.
Common mistakes
- Opposing operators.
(400px < width > 300px)is invalid. Both operators must point in the same direction. - Reversed bounds.
(700px >= width >= 400px)is valid, but(700px <= width <= 400px)is valid syntax that can never match, so the rule never applies. - Using
=in a two-sided range.(400px = width = 700px)is invalid. - Range syntax on keyword features.
(hover >= hover)is meaningless. Use(hover: hover). - Missing parentheses. Every condition needs its own parentheses, and
notrequires them around the condition that follows it. - Missing
widthname.(>= 40em)is invalid. The feature name is required.
Exercise
Rewrite @media (min-width: 30em) and (max-width: 59.99em) using range syntax, so that a viewport exactly 60em wide is not matched and fractional widths just under 60em are.
Show answer
@media (30em <= width < 60em). The strict < on the upper bound excludes exactly 60em and includes every fractional width below it, so no 59.99em workaround is needed.
Exercise
Which of these is invalid, and why? (a) (width >= 40em) (b) (40em <= width < 64em) (c) (40em <= width > 20em) (d) (prefers-color-scheme: dark)
Show answer
(c) is invalid because the two operators point in opposite directions. Both operators in a two-sided range must point the same way. (a) and (b) are valid range queries. (d) is valid, and it uses the colon form because prefers-color-scheme is a discrete keyword feature, not a range feature.