2

Detecting media query support in CSS and JavaScript

 2 years ago
source link: https://kilianvalkhof.com/2021/web/detecting-media-query-support-in-css-and-javascript/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Detecting media query support in CSS and JavaScript

Kilian Valkhof

Building tools that make developers awesome.

Search term

Detecting media query support in CSS and JavaScript

CSS & HTML, Javascript, Web, 13 July 2021, 4 minute read

Recently I needed a way to detect support for a media query in CSS and Javascript. To detect if a browser supports a certain CSS feature, you can use @supports () { ... }, but that doesn’t work for media queries. In this article I’ll show you how you can detect support for media queries regardless of if they are turned on.

Why I needed this

For a presentation I did on prefers-reduced-data I wanted to apply something in one of two situations:

  • There was no support for prefers-reduced-data at all.
  • There was support for prefers-reduced-data and the value was “no-preference”.

For this, I couldn’t use just @media (prefers-reduced-data: no-preference) because that would be false if either there was no support (since the browser wouldnt understand the media query) or if it was supported but the user wanted to preserve data.

What I needed was a test for the media feature regardless of it’s value. It turns out we can do that by using the or notation in media queries.

Detecting media query support in CSS

To detect if a media query is supported in CSS at all, you can use the following CSS:

@media not all and (prefers-reduced-data), (prefers-reduced-data) {
  ...
}

That looks like a bit of weird, so lets dissect what it actually says. Firstly, let’s split the two media features and begin with the second one:

(prefers-reduced-data)

This one looks straightforward but there’s something weird: the media feature is missing a value! Usually, media features come with a value, like “min-width: 400px”, but this one doesn’t have a value.

That’s because some media features have a “shorthand” when they only have two options. Prefers-reduced-data has “no-preference” (off) and “reduce” (on). When you omit the value, it tests for it being on.

So here’s how this will resolve:

  • No preference: false
  • Reduce: true

But if the browser doesn’t support a media feature, it will automatically change to “not all”, which resolves to false, so we end with this:

  • No support: false
  • No preference: false
  • Reduce: true

not all and (prefers-reduced-data)

The notable thing here is not all and. “All” is the default media type, and it applies to both screen and print. You can omit it (and likely you usually do), but if you add it you need to add “and” in between it and the media feature (which is the part between parentheses).

not is how you can negate a media query. For example, @media not print {...} would apply everywhere except print.

With all being the default, what we’re really checking here for is “not (prefers-reduced-data)”. Unfortunately that’s invalid notation until supports for Media Queries level 4 lands, so we need to add the “all and” here.

Here’s how this resolves:

  • No support: false, since the browser still doesn’t understand it.
  • Support but off: true (its the negation of it being on).
  • Support but on: false

Combined

When the browser combines these values using the OR, only one of them has to be true for the media declaration to be applied:

No support:

  • not all and (prefers-reduced-data): false
  • (prefers-reduced-data): false

Combined: false

Support, but off:

  • not all and (prefers-reduced-data): true
  • (prefers-reduced-data): false

Combined: true

Support, but off:

  • not all and (prefers-reduced-data): false
  • (prefers-reduced-data): true

Combined: true

Anything in the media query will now be applied if the feature is supported, regardless of what its value is.

Detecting media query support in JavaScript

We can use the same media query in JavaScript using the window.matchMedia API:

const isSupported = window.matchMedia(
  `not all and (prefers-reduced-data), (prefers-reduced-data)`
  ).matches;

window.matchMedia returns an object with a “matches” boolean property that is either true or false. For more on the api, check out the using media queries in JavaScript section of my guide on media queries.

After I shared the above out on Twitter, Mathias pointed out a different method.

const query = '(prefers-reduced-data)';
const resolvedMediaQuery = window.matchMedia(query).media;

const isSupported = query === resolvedMediaQuery;

The window.matchMedia api also returns a “media” property, which is the normalized and resolved string representation of the query you tested. If matchMedia encounters something it doesn’t understand, that changes to not all, and if it does support the query it will return the query, regardless of if it matches (you can use the matches property for that).

So by comparing your input to the media, you either get:

No support:
'(prefers-reduced-data)' === 'not all' which is false.

Support:
'(prefers-reduced-data)' === '(prefers-reduced-data)' which is true.

Which one to use?

What I like about the first option, with the complex media query, is that all the logic happens inside CSS. I also like how you get a boolean, and don’t have to do string comparison.

The second can be a little bit easier to understand at a glance, but you need to make sure that your query input is the same as the browser normalizes it.

For example, if you test (prefers-reduced-data ) (notice the space), that would resolve “matches” to true in supported browsers because the white space is not important, but comparing the normalized media query would return false, since that normalization has removed that extra space. So string comparison can be tricky depending on your input.

When to use this?

We’re set to get a whole lot of new media features in the coming years, like prefers-reduced-data, prefers-contrast, screen-spanning and more.

While transitioning to all browsers supporting this, you’ll often want to turn on extra features for browsers that support it without causing issues in older browsers since the new default might not always be the best experience in older browsers.

With this media query you can split the behavior in older browsers without support and newer browsers with support.

Hi, I'm Kilian. I make Polypane, the browser for responsive web development and design. If you're reading this site, that's probably interesting to you. Try it out!

Related Posts

I want my own media queries in browsers
19 October 2020, 4 minute read

We’re on the verge of a whole lot of new CSS media queries becoming available. But if you’re a developer that wants to get a head start and try them out, you’re out of luck. Browsers are prototyping them one by one before making them available and we as developers have to wait.

Increasing access to your website with “prefers-reduced-data”
12 July 2021, < 1 minute read

The upcoming “prefers-reduced-data” media query will make your site more accessible in the “more people can now enter the building” meaning of accessibility. In this recording of my talk given at Shortstack conference you will learn strategies to start implementing this feature now and make your site available to even more people.

The complete guide to CSS Media Queries
15 June 2020, < 1 minute read

Media queries are what make modern responsive design possible. With them you can set different styling based on things like a users screen size, device capabilities or user preferences. But how do they work, which ones are there and which ones should you use?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK