Detecting Media Query Support in CSS and JavaScript

You can’t just do @media (prefers-reduced-data: no-preference) alone because, as Kilian Valkhof says:

[…] 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


The post Detecting Media Query Support in CSS and JavaScript appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


This content originally appeared on CSS-Tricks and was authored by Chris Coyier

You can’t just do @media (prefers-reduced-data: no-preference) alone because, as Kilian Valkhof says:

[…] 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.

Usually @supports is the tool for this in CSS, but that doesn’t work with @media queries. Turns out there is a solution though:

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

This is a somewhat complex logic puzzle involving media query syntax and how browsers evaluate these things. It’s nice you can ultimately handle a no-support fallback situation in one expression.

Direct Link to ArticlePermalink


The post Detecting Media Query Support in CSS and JavaScript appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


This content originally appeared on CSS-Tricks and was authored by Chris Coyier


Print Share Comment Cite Upload Translate Updates
APA

Chris Coyier | Sciencx (2021-08-23T17:16:38+00:00) Detecting Media Query Support in CSS and JavaScript. Retrieved from https://www.scien.cx/2021/08/23/detecting-media-query-support-in-css-and-javascript-3/

MLA
" » Detecting Media Query Support in CSS and JavaScript." Chris Coyier | Sciencx - Monday August 23, 2021, https://www.scien.cx/2021/08/23/detecting-media-query-support-in-css-and-javascript-3/
HARVARD
Chris Coyier | Sciencx Monday August 23, 2021 » Detecting Media Query Support in CSS and JavaScript., viewed ,<https://www.scien.cx/2021/08/23/detecting-media-query-support-in-css-and-javascript-3/>
VANCOUVER
Chris Coyier | Sciencx - » Detecting Media Query Support in CSS and JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/23/detecting-media-query-support-in-css-and-javascript-3/
CHICAGO
" » Detecting Media Query Support in CSS and JavaScript." Chris Coyier | Sciencx - Accessed . https://www.scien.cx/2021/08/23/detecting-media-query-support-in-css-and-javascript-3/
IEEE
" » Detecting Media Query Support in CSS and JavaScript." Chris Coyier | Sciencx [Online]. Available: https://www.scien.cx/2021/08/23/detecting-media-query-support-in-css-and-javascript-3/. [Accessed: ]
rf:citation
» Detecting Media Query Support in CSS and JavaScript | Chris Coyier | Sciencx | https://www.scien.cx/2021/08/23/detecting-media-query-support-in-css-and-javascript-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.