DEV Community

Discussion on: Implement Dark Mode On Your Website.

Collapse
 
icanswiftabit profile image
Błażej Wdowikowski • Edited

👋 Interesting approach but why not leave theme detection to browser?

:root {
    color-scheme: light dark;
    --special-text-color: #000
}

@media (prefers-color-scheme: dark) {
    :root {
        --special-text-color: #FFF;
    }
}

body, html {
    height: 100%;
    color-scheme: light dark;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mattmarquise profile image
Matthew Marquise

This is also a great way to do it!

Collapse
 
jonosellier profile image
jonosellier

Why not both?

@media (prefers-color-scheme: dark) {
    html.system {
        /* dark mode vars */
    }
}
html.dark {
    /* dark mode vars */
}
Enter fullscreen mode Exit fullscreen mode

And do the same check in the post but for a string via a switch. This also opens up the ability for different themes (maybe a dark mode that's actually light enough to blend in with the browser UI in dark mode, an amoled dark mode, a solarized mode, a few color-blind modes)