Keywords: CSS
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.