Sub topic 7
Adding a light, dark, and system toggle
This chapter assumes you can write CSS custom properties and a prefers-color-scheme media query, and that you can set DOM attributes from JavaScript. You should also know the color-scheme property from the previous chapters.
Why three choices
A two-state button (light or dark) forces the user to leave the system setting behind. A better control has three choices:
- System: follow the operating system. This is the default.
- Light: always light, whatever the system says.
- Dark: always dark, whatever the system says.
The approach is to let an attribute on the root element, data-theme, carry an explicit choice. When the attribute is absent, the media query decides.
The CSS
Light values are the default. The media query applies dark values only when the user has not forced light. An explicit data-theme="dark" rule applies them regardless of the system setting.
:root {
color-scheme: light dark;
--bg: #ffffff;
--text: #1a1a1a;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--bg: #121212;
--text: #e6e6e6;
}
}
:root[data-theme="light"] {
color-scheme: light;
}
:root[data-theme="dark"] {
color-scheme: dark;
--bg: #121212;
--text: #e6e6e6;
}
body {
background: var(--bg);
color: var(--text);
}Each block has a job:
color-scheme: light darkon:rootlets the browser theme built-in UI such as form controls and scrollbars for both schemes.- The
:not([data-theme="light"])selector keeps the automatic dark theme active unless the user chose light. - The two attribute blocks narrow
color-schemeto a single scheme, so built-in UI matches the forced theme.
The markup and script
A <select> is a simple way to offer three options. It is keyboard accessible and announced correctly by screen readers.
<label for="theme-select">Theme</label>
<select id="theme-select">
<option value="system">System</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>The script sets the attribute for light and dark, and removes it for system.
const root = document.documentElement;
const select = document.querySelector("#theme-select");
function applyTheme(choice) {
if (choice === "light" || choice === "dark") {
root.dataset.theme = choice;
} else {
root.removeAttribute("data-theme");
}
}
select.addEventListener("change", () => {
applyTheme(select.value);
});Removing the attribute is what makes "System" work. The page falls back to the media query, so it also follows live changes: if the user switches their operating system to dark while the page is open, the page updates with no extra code.
Common errors
- Dark values written twice. The dark colors appear in the media query and in the
[data-theme="dark"]block. If you edit one and forget the other, the "System" dark theme and the "Dark" choice will differ. Keep the two blocks identical, or uselight-dark()as described below. - Using
:root[data-theme="dark"]without the:not()guard on the media query. A user who picks "Light" on a dark system would still get dark colors, because the media query matches and nothing overrides it. - Forgetting to narrow
color-scheme. If it stayslight darkunder a forced theme, scrollbars and form controls follow the system instead of your choice. - Reading the current theme from the select. The select says "System" but not what the system resolves to. When you need the actual theme in JavaScript, check
window.matchMedia("(prefers-color-scheme: dark)").matches.
Reducing duplication with light-dark()
If you use light-dark() for colors, you only need color-scheme to change between themes:
:root {
color-scheme: light dark;
}
:root[data-theme="light"] {
color-scheme: light;
}
:root[data-theme="dark"] {
color-scheme: dark;
}
body {
background: light-dark(#ffffff, #121212);
color: light-dark(#1a1a1a, #e6e6e6);
}Forcing a scheme with color-scheme makes light-dark() choose the matching value. No media query and no repeated dark values are needed. The trade-off is that light-dark() accepts only colors, so values such as images or shadows still need the custom property approach.
Exercise
A user on a dark-mode system selects "Light" in your toggle. Which part of the CSS keeps the page light, and what does the script do when they then select "System"?
Show answer
The :not([data-theme="light"]) selector stops the dark media query from applying while data-theme="light" is set, so the default light values stay in effect. Selecting "System" calls removeAttribute("data-theme"). The attribute is gone, the :not() condition matches again, and the media query applies the dark values because the system prefers dark.