Detecting media query support in CSS and JavaScript

Kilian Valkhof recently needed to check if prefers-reduced-data was supported by the browser or not 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 wouldn’t understand the media query) or if it was supported but the user wanted to preserve data. …


This content originally appeared on Bram.us and was authored by Bramus!

Kilian Valkhof recently needed to check if prefers-reduced-data was supported by the browser or not

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 wouldn’t 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 its value. It turns out we can do that by using the or notation in media queries.

The result is a pretty mind-bending at-rule:

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

I don’t fully grasp it but if Kilian says it works, I trust him on that. Thankfully the JS version is a bit easier (and that one I do understand entirely).

Detecting media query support in CSS and JavaScript →


This content originally appeared on Bram.us and was authored by Bramus!


Print Share Comment Cite Upload Translate
APA
Bramus! | Sciencx (2023-04-02T03:16:20+00:00) » Detecting media query support in CSS and JavaScript. Retrieved from https://www.scien.cx/2021/07/21/detecting-media-query-support-in-css-and-javascript-2/.
MLA
" » Detecting media query support in CSS and JavaScript." Bramus! | Sciencx - Wednesday July 21, 2021, https://www.scien.cx/2021/07/21/detecting-media-query-support-in-css-and-javascript-2/
HARVARD
Bramus! | Sciencx Wednesday July 21, 2021 » Detecting media query support in CSS and JavaScript., viewed 2023-04-02T03:16:20+00:00,<https://www.scien.cx/2021/07/21/detecting-media-query-support-in-css-and-javascript-2/>
VANCOUVER
Bramus! | Sciencx - » Detecting media query support in CSS and JavaScript. [Internet]. [Accessed 2023-04-02T03:16:20+00:00]. Available from: https://www.scien.cx/2021/07/21/detecting-media-query-support-in-css-and-javascript-2/
CHICAGO
" » Detecting media query support in CSS and JavaScript." Bramus! | Sciencx - Accessed 2023-04-02T03:16:20+00:00. https://www.scien.cx/2021/07/21/detecting-media-query-support-in-css-and-javascript-2/
IEEE
" » Detecting media query support in CSS and JavaScript." Bramus! | Sciencx [Online]. Available: https://www.scien.cx/2021/07/21/detecting-media-query-support-in-css-and-javascript-2/. [Accessed: 2023-04-02T03:16:20+00:00]
rf:citation
» Detecting media query support in CSS and JavaScript | Bramus! | Sciencx | https://www.scien.cx/2021/07/21/detecting-media-query-support-in-css-and-javascript-2/ | 2023-04-02T03:16:20+00:00
https://github.com/addpipe/simple-recorderjs-demo