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

Show parent comments

14

u/rabidhamster Aug 22 '18

Which is not even close to what was asked of you. The only thing that matches is the "min-width: 400px". The rest of the syntax is entirely different.

Try this in your CSS:

if (window.matchMedia("(min-width: 400px)").matches) {
  console.log('Larger than 399px');
} else {
  console.log('Less than 400px');
}

See how media queries based in JavaScript might have some utility and use-cases beyond what CSS can provide?

-16

u/StoneCypher Aug 22 '18

I think that you are confusing "I can use css from Javascript" with "this must not be css"

8

u/crobison Aug 22 '18

Don’t be obtuse.

-8

u/StoneCypher Aug 22 '18

Insults won't change my opinion. Neither will mass downvoting.

If the code is defined by the css spec, it's css.

9

u/crobison Aug 22 '18

You are an idiot.