CSS - Light and Dark Mode

Article Information

Article Content

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.

Our Mission

At Wildlink, our mission is to deliver the best business solutions we can to enable our clients to gather and unlock the answers in their data.
We are committed to fostering long-term partnerships built on trust, integrity, and a relentless pursuit of innovation.