0

For example, the new Stack Overflow feature, Dark Mode has an option to use the system default between light mode and dark mode. How do they tell what the system default is with JavaScript? And if not with JavaScript, how do you find it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
yogurtsyum
  • 348
  • 1
  • 2
  • 17

1 Answers1

0

Use window.matchMedia to detect the user's preferred color scheme with JavaScript.

const result = window.matchMedia('(prefers-color-scheme: dark)');
console.log(result.matches); // TRUE if user prefers dark mode
mfluehr
  • 2,832
  • 2
  • 23
  • 31