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

1

u/rendermouse Aug 22 '18

You can also use CSS properties and read them from script. For example, set the z-index of the <body> to 1 for desktop, 2 for tablet, 3 for mobile using media queries. Then if your JS needs to know the current media query, just check the z-index style prop of the body tag. Yes, it's also a workaround...I was not aware of this matchMedia() method.