r/javascript • • Aug 21 '18

Effectively supported in all browsers, JavaScript has its own form of media queries just like CSS.

https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
465 Upvotes

114 comments sorted by

View all comments

45

u/SalemBeats Aug 22 '18

This is one of those things that, when someone brings it up, I think "yeah, I was aware of that". But it's still so obscure that it never comes to mind in the few situations it would actually be useful.

1

u/laichejl Aug 25 '18

It's extremely useful if working with CSS-in-JS.

3

u/SalemBeats Aug 25 '18

I can't tell whether you're referring to a specific library, or just the general tendency to prefer styling elements directly via Javascript and avoiding static external stylesheets.

1

u/laichejl Aug 25 '18

More-so the rise of libs over the past couple years that let you style with JavaScript. At my place of work, we have been using emotion quite a bit, and have created some responsive helper components that use matchMedia to render certain components at corresponding breakpoints. The nice thing is, we declare all our breakpoints and stuff just right there in our JS , so we are able to both style our components based on breakpoint (standard media query css) and also render them based on breakpoint, all referencing the same variables!