日本語

How to Write Width Ranges with Comparison Operators in CSS Media Queries

​​CSS​JavaScript,Playwright,responsive design

In CSS media queries, you can write a viewport width condition as (width >= 600px) instead of (min-width: 600px). A range such as "600px or more and less than 800px" can be written in one set of parentheses as (600px <= width < 800px). This is a way to write the breakpoints for responsive design, which switch layouts for smartphones, tablets, and PCs, in a form that reads like a mathematical expression.

Note

I tested on Ubuntu 24.04 with Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I checked the result of matchMedia() and whether the @media rules were applied, using pages loaded at different viewport widths and inside iframes. I did not test Firefox, Safari, or older browsers that do not support the range syntax. The browser version support below is based on the compatibility data in @mdn/browser-compat-data 8.1.8.

Rewriting min-width and max-width

As described on MDN's page on using media queries, min- and max- comparisons include the boundary value. The specification (Media Queries Level 4) also defines min- as the same as >= and max- as the same as <=. In the comparison syntax, you can also use < and >, which exclude the boundary value.

Previous syntax Comparison syntax
(min-width: 600px) (width >= 600px)
(max-width: 599px) (width < 600px) (how the boundary is handled is explained in a later section)
(min-width: 600px) and (max-width: 799px) (600px <= width < 800px)
(min-height: 300px) (height >= 300px)

If you write the narrow-screen styles first and add the wider-screen rules inside (width >= 600px), you get the following:

css
.cards {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

@media (width >= 600px) {
  .cards {
    grid-template-columns: 1fr 1fr;
  }
}

In my test environment, I loaded this page in iframes 599px and 600px wide. At 599px the cards were in one column, and at 600px they were in two columns. The media queries inside an iframe are evaluated against the iframe's width.

Two screenshots of the same page, one at 599px wide and one at 600px wide, stacked vertically. The 599px one shows a label reading "(width >= 600px): does not match (1 column)" (the label is in Japanese), and cards 1 to 4 are stacked in a single column. The 600px one shows a label reading "(width >= 600px): matches (2 columns)" (the label is in Japanese), and the cards are arranged in two columns.
Comparison of whether the (width >= 600px) rule is applied at 599px and 600px widths

I loaded iframes from 599px to 800px wide and compared the results of the previous syntax and the comparison syntax. All three pairs in the table gave the same results. (600px <= width < 800px) matched at 600px and did not match at 800px.

Ranges: keep the inequality directions consistent

When you write a range, put width in the middle and use the same direction for both inequality operators. The specification grammar only allows both operators to point the same way: either both < (<=) or both > (>=). I tested a 700px-wide viewport with the following results.

Condition Result
(400px <= width < 800px) Matched
(800px > width >= 400px) Matched
(600px <= width) Matched (the value can come first)
(width = 700px) Matched (exactly that width)
(400px < width > 800px) Did not match

The last row mixes directions, which the specification grammar does not allow. It is treated as an unreadable condition, and at 700px it did not match (how unreadable conditions are handled is explained in a later section).

Avoiding gaps at the boundary

When switching with max-width and min-width, you may write the values offset by one, such as (max-width: 599px) and (min-width: 600px), so that both rules never match at once. The specification uses (max-width: 320px) and (min-width: 321px) as an example and explains that, with high-resolution displays or zoom that produces fractional widths, neither rule applies between 320px and 321px. It then says that writing the same value with <= and >, such as (width <= 320px) and (width > 320px), is a better fit.

css
@media (width < 600px) {
  /* less than 600px */
}
@media (width >= 600px) {
  /* 600px or more */
}

In my test environment with Chromium, even when the iframe was 599.5px wide, the page's innerWidth was 600px. A width of 599.99px also gave 600px, and 600.5px gave 601px. So I could not reproduce the gap with fractional widths in this environment. With integer widths, (max-width: 599px) and (width < 600px) gave the same results.

A mistyped condition does not raise an error

A mistyped condition does not cause an error when the CSS loads. It remains as a condition that never matches at any width. I checked whether the @media rule was applied at a 700px-wide viewport.

Condition Result
(width>=600px) (no spaces) Matched
(width => 600px) Did not match
(width > = 600px) (space between > and =) Did not match
(width == 700px) Did not match
(min-width >= 600px) Did not match
(width >= 600) (no unit) Did not match
width >= 600px (no parentheses) Did not match (became not all)

Spaces around the inequality operators can be omitted, but you cannot put a space between the > and = in >=. The specification also says that no space is allowed between < or > and the following =. Values need a unit. (width >= 600) did not match.

Only a condition missing its parentheses was changed. When I read the stylesheet back through the CSSOM (sheet.cssRules), the condition text had been replaced with not all. In my test environment with Chromium, the other mistyped conditions kept their written text. Because reading back through the CSSOM does not show not all for them, you cannot detect those mistakes that way.

Warning

Adding not to a mistyped condition does not make it match. In my test environment, neither not (width => 600px) nor not (width >= 600) matched. The specification says an unreadable condition is treated as "unknown", that adding not leaves it "unknown", and that the final result is "does not match". So a rule meant for "when the width is not 600px or more" will not apply at any width.

If only part of a comma-separated list of conditions is mistyped, the other conditions decide the result. screen, (width => 600px) applied the rule because screen matched.

The base for em is not the html font-size

You can also write the width in em, as in (width >= 37.5em). The 1em here is not the font size set in your page's CSS. The specification says that em in media queries uses the initial value of font-size, which is determined by the browser and user settings.

In my test environment, I used a 700px-wide page with html { font-size: 20px; }. (width >= 43em) matched, and (width >= 44em) did not. If 1em is 16px, then 43em is 688px and 44em is 704px, which agrees with the results. If the base were 20px, 43em would be 860px, and a 700px viewport would not match. Using rem gave the same results.

The same syntax works with matchMedia() in JavaScript

When checking the screen width in JavaScript, you can pass the same condition string to matchMedia().

js
const mq = matchMedia('(width >= 600px)');
console.log(mq.matches);

mq.addEventListener('change', (e) => {
  console.log(e.matches ? '600px 以上になった' : '600px 未満になった');
});

In my test environment, I started at 700px (matches: true) and changed the width to 650px, 600px, 599px, 500px, and then 600px. The change event fired only twice. It fired with matches: false at 599px and with matches: true when the width returned to 600px. Width changes that did not change whether the query matched did not fire the event.

Browser support

According to the compatibility data in @mdn/browser-compat-data 8.1.8, the range syntax for media queries is available in the following versions.

Browser Version
Chrome, Edge 104
Firefox 102 (partial support in 63 to 101)
Safari (macOS, iOS) 16.4

The compatibility data notes that Firefox 63 to 101 supported only the form that puts the feature name first, such as (width > 500px). The form that puts the value first, such as (600px <= width < 800px), is supported from Firefox 102.

The specification explains that in Level 3 media queries, a syntax error made the entire media query not match. For pages that must still display correctly in browsers without range syntax support, such as Safari 16.3 and earlier, write the rules with min-width and max-width.

​Posted by 86