Detect if Dark Mode is Actually Active (not preference)

There are various strategies for applying “darkmode” css to an app. It could be hardcoded or it could be based on the user’s “system” preference or it could be toggled via JavaScript.

There are many posts online about how to detect the browser/system preference but how can we know if darkmode is actually currently active (regardless the preference)?

I don’t see anything in the DOM, classname or otherwise called “dark” which we could query.

One way to detect if dark mode is actually active is to monitor the matchMedia API. However, to check if an element is rendered in dark mode regardless of preference, you might need to look at computed styles for specific elements.

Thank you, this is exactly what I wanted!

Example: using the console I query matchMedia in dark mode and then switch the browser to light mode:

Developers, Chrome’s DEV Tools under Elements → Styles lets you define the browser light/dark mode: