about - tech blog -
wildlink.com

Wildlink's Technology Blog
An occasionally updated list of informative articles.
Pulled from our internal wiki.
CSS - Light and Dark Mode
2023-02-04

Keywords: CSS

One way to add Light and Dark mode support to a page

Not all people bother with turning on Dark mode, but for those that do, it would be nice if a site's theme would match the user's settings.

The simplest way to implement light and dark mode is to add color-scheme to your css :root declaration:

:root {
    color-scheme: light dark;
}

But for more control, define your colors like normal in your :root declaration then add a @media block to override.

Something like this:

:root {
    --color-bg: #fafafa;
}
@media (prefers-color-scheme: dark) {
    :root {
        --color-bg: #222222;
    }
}
body {
    background-color: var(--color-bg);
}

In the @media block you can specify all the colors you want and they will only override the default :root if the user has dark model enabled.

Back to the Tech Blog
Blog engine: 1.4.0