Sign in

System DARK mode detection in browser

CSS detection


.box {
width: 100px;
height: 100px;
.system-color {
background-color: white;
@media (prefers-color-scheme: dark) {
.system-color {
background-color: black;
<link rel="stylesheet" href="./dark.css" media="(prefers-color-scheme: dark)">

Javascript detection

In JavaScript you can use matchMedia(docs) method

//return boolean
window.matchMedia('(prefers-color-scheme: dark)').matches
const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)');

function handleSystemThemeChange(evt) {
if (evt.matches) {
console.log('dark mode');
} else {
console.log('light mode');
handleSystemThemeChange(mediaQueryList)// dark mode

// Add the callback function as a listener to the query list.