Utilize custom properties to define your color scheme here.
(Remember, you'll need a variety of colors beyond this simple example)
body {
--main-bg-color: yellow;
--main-fg-color: black;
}
To create a dark mode version, override these properties in a separate stylesheet:
@media (prefers-color-scheme: dark) {
body {
--main-bg-color: black;
--main-fg-color: yellow;
}
}
If you want users to have the option to switch between light and dark modes manually, define new sets of properties with classes:
body.lightMode {
--main-bg-color: yellow;
--main-fg-color: black;
}
body.darkMode {
--main-bg-color: black;
--main-fg-color: yellow;
}
Just reference these variables when specifying color names:
h1 {
color: var(--main-bg-color);
}
To enable UI-triggered style changes, use an event handler function:
function toggleTheme() {
const body = document.body;
if (body.classList.contains("darkMode")) {
body.classList.remove("darkMode");
body.classList.add("lightMode");
} else {
body.classList.remove("lightMode");
body.classList.add("darkMode");
}
};