How to Build CSS Dark Mode (2026)
CSS dark mode with tokens, prefers-color-scheme, color-scheme and a toggle that works both ways. We tested the toggle patterns in Chromium (the commonly copied nested version does nothing), and summarize what the research says about battery and reading.
Start With Color Tokens
Define colors once, as custom properties named by role rather than appearance (--bg, --surface, --text, --text-muted, --border, --accent), and use only those in components. A theme is then a second set of values for the same names. Most sites get by with eight to a dozen color tokens.
Follow the System Setting
:root {
color-scheme: light dark; /* form controls, scrollbars, canvas */
--bg: #ffffff; --text: #1a1a1a;
}
@media (prefers-color-scheme: dark) {
:root { --bg: #121212; --text: rgb(255 255 255 / 0.87); }
}
Pure CSS, so there is no flash of the wrong theme. The color-scheme line matters more than it looks: in our test with the system set to dark, a plain text input stayed white (rgb 255, 255, 255) until color-scheme: light dark was declared, after which Chromium drew it dark (rgb 59, 59, 59) and switched the default text color to white.
Adding a Manual Toggle That Works Both Ways
A toggle sets data-theme="dark" or "light" on <html>. Where the CSS goes matters, and so does handling the light choice. We tested three versions in Chromium with the system preference emulated (October 13, 2026; script in our test folder):
| CSS for the toggle | System light + toggle dark | System dark + toggle light |
|---|---|---|
[data-theme="dark"] { :root { โฆ } } (nested, as this guide used to show) | stays light: rule never matches | stays dark |
:root[data-theme="dark"] { โฆ } only | dark | stays dark |
:root[data-theme="dark"] and :root[data-theme="light"] | dark | light |
The nested version selects a :root inside the html element, which does not exist, so the toggle silently does nothing. And without a light rule, someone whose system is dark can never choose light. The working pattern:
:root[data-theme="dark"] { color-scheme: dark; --bg: #121212; --text: rgb(255 255 255 / 0.87); }
:root[data-theme="light"] { color-scheme: light; --bg: #ffffff; --text: #1a1a1a; }
Store the choice in localStorage and apply it with a small inline script in <head>, before the stylesheet paints, so a returning visitor never sees the other theme first. With no stored choice, set nothing and let the media query decide.
Choosing Dark Colors
- Background. Material Design's dark theme uses #121212 rather than black, which leaves room to show elevation with lighter surfaces.
- Text. Material's 87% and 60% white for primary and secondary text give 14.19:1 and 7.16:1 on #121212 (our calculation with the WCAG formula); 38% white, sometimes used for disabled text, drops to 3.57:1, below the 4.5:1 that WCAG 1.4.3 asks of body text.
- Accents need re-checking, not just desaturating. Blue #2563EB passes on white (5.17:1) but fails on #121212 (3.62:1); the lighter #60A5FA reaches 7.37:1 on #121212 and fails on white (2.54:1). Every accent needs a value per theme, checked in both.
What Dark Mode Does and Does Not Do
- Battery. Purdue researchers measured OLED phones (MobiSys 2021): switching to dark mode saved 3-9% of power on average at 30-50% brightness and 39-47% at 100%. On LCD screens, where the backlight is always on, dark pixels save little.
- Reading. Lab studies favor dark text on a light background for legibility: in PubMed-indexed experiments by Piepenbrock and colleagues, positive polarity gave better visual acuity and proofreading for younger and older adults (Ergonomics, 2013), and the advantage grew as characters got smaller (Human Factors, 2014). That is a reason to offer dark mode as a choice and default to the system setting, not to force it.
The Dark Mode Generator builds a light and dark token set from your colors and shows contrast for each pair; check accents in both themes before shipping.
Frequently Asked Questions
Should I design light or dark mode first?
How do I handle images in dark mode?
What about user preference persistence?
Does dark mode save battery?
- [1]MDN, prefers-color-scheme, checked Oct 13, 2026 โ
- [2]MDN, color-scheme, checked Oct 13, 2026 โ
- [3]Material Design 2, Dark theme (surface #121212, text emphasis) โ
- [4]Purdue University, Dark mode may not save your phone's battery life as much as you think (MobiSys 2021 study), checked Oct 13, 2026 โ
- [5]Piepenbrock et al., Positive display polarity is advantageous for both younger and older adults, Ergonomics 56(7) (2013), via PubMed โ
- [6]Piepenbrock et al., Positive display polarity is particularly advantageous for small character sizes, Human Factors 56(5) (2014), via PubMed โ
- [7]W3C, Understanding WCAG 2.2 SC 1.4.3 Contrast (Minimum) โ