Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
Is it possible to affect a CSS media query from JS?

I have a block with some media query rule applied to it, something like this:

<style>
@media (prefers-color-scheme: dark) {
  div{ background:  black; color: white; }
}

@media (prefers-color-scheme: light) {
  div { background: white; color: black; }
}
</style>
<div>
some content
</div>

And I need to toggle the media query from JS, somehow like that:

document.querySelector('div').media['prefers-color-scheme'] = 'light'; // or 'dark'

Is there such a DOM API? Is this possible?

UPD: I don't want to change the rule, I want to "emulate" changing external conditions.

Thank you.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!