Sub topic 11
Range syntax for media queries
This chapter assumes you can write a basic @media rule with min-width and max-width, and that you know the and operator. It builds on the width-based queries covered earlier.
The problem with min-width and max-width
To target a band of widths, the older syntax needs two conditions joined by and:
@media (min-width: 30em) and (max-width: 59.99em) {
.sidebar {
display: none;
}
}The 59.99em is a workaround. max-width is inclusive, so max-width: 60em would overlap a rule that starts at min-width: 60em. Subtracting a small amount avoids the overlap, but it leaves a gap for fractional widths, which can occur on high-density screens or with browser zoom.
Comparison operators
Media Queries Level 4 lets you write the condition as a comparison. The feature name goes on one side and the value on the other:
@media (width >= 600px) {
.nav {
flex-direction: row;
}
}
@media (width < 600px) {
.nav {
flex-direction: column;
}
}The operators are <, <=, >, >=, and =. The strict operators < and > remove the need for the 59.99em trick. The two rules above cover every width exactly once, with no overlap and no gap.
Writing a range
Put the feature between two comparisons to express a band in one condition:
@media (400px <= width <= 700px) {
.banner {
padding: 0.5rem;
}
}
@media (30em <= width < 60em) {
.sidebar {
display: none;
}
}The second query replaces the min-width/max-width pair from the start of this chapter. The upper bound is exclusive, so a viewport of exactly 60em falls into the next range.
The table shows how the two syntaxes map to each other.
| Range syntax | Older equivalent |
|---|---|
| `(width >= 40em)` | `(min-width: 40em)` |
| `(width <= 40em)` | `(max-width: 40em)` |
| `(width < 40em)` | no exact equivalent (`max-width: 39.99em` is an approximation) |
| `(30em <= width <= 60em)` | `(min-width: 30em) and (max-width: 60em)` |
Rules and common errors
- Both operators in a two-sided range must point the same way.
(400px <= width >= 700px)is invalid, and the browser drops the whole query. - Do not mix the styles in one condition.
(min-width <= 600px)is invalid. Use eithermin-width: 600pxorwidth >= 600px. - Range syntax works with other features that take a range of values, such as
heightandaspect-ratio, not onlywidth. - Units behave as before. In media queries,
emandremboth refer to the browser's initial font size, not to the font size of any element. - Range syntax is supported in current versions of Chrome, Edge, Firefox, and Safari (Safari from 16.4). Browsers that do not understand it ignore the entire rule. If you must support older browsers, keep
min-widthandmax-width.
Combining with other queries
A range condition is a normal media condition. You can join it with and, a comma, or not:
@media (30em <= width < 60em) and (orientation: landscape) {
.gallery {
grid-template-columns: repeat(2, 1fr);
}
}Exercise
Rewrite @media screen and (min-width: 48em) and (max-width: 63.99em) using range syntax so that a viewport of exactly 64em is not included. Why is the new version more accurate?
Show answer
@media screen and (48em <= width < 64em). The strict < operator excludes exactly 64em without a hack value. The old version leaves a gap for widths between 63.99em and 64em, such as 63.995em, which match neither this rule nor a min-width: 64em rule.