CSSApr 15, 2026 ยท Updated Oct 13, 2026 ยท 4 min read

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.

๐ŸŒ™
Try the Dark Mode Generator
Free, no signup
โ†’
DG
Derek Giordano
Designer & Developer
In this guide
01Start With Color Tokens02Follow the System Setting03Adding a Manual Toggle That Works Both Ways04Choosing Dark Colors05What Dark Mode Does and Does Not Do

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 toggleSystem light + toggle darkSystem dark + toggle light
[data-theme="dark"] { :root { โ€ฆ } } (nested, as this guide used to show)stays light: rule never matchesstays dark
:root[data-theme="dark"] { โ€ฆ } onlydarkstays dark
:root[data-theme="dark"] and :root[data-theme="light"]darklight

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

What Dark Mode Does and Does Not Do

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?+
Design light mode first (it is the web default), then derive dark mode by adjusting your token values. Starting with dark and going to light is harder because dark mode has more color constraints.
How do I handle images in dark mode?+
Keep photos as they are; for diagrams or screenshots with white backgrounds, serve a dark version with <picture> and a prefers-color-scheme media attribute, or reduce glare slightly with filter: brightness(0.9). Check that any text inside images still has enough contrast.
What about user preference persistence?+
Store the toggle state in localStorage. On page load, apply the stored theme before rendering to avoid a flash of the wrong theme.
Does dark mode save battery?+
On OLED screens, depending on brightness. Purdue researchers (MobiSys 2021) measured average savings of 3-9% at 30-50% brightness and 39-47% at 100%. On LCD screens the backlight stays on and the saving is small.
Try it yourself

Generate dark mode CSS tokens โ€” free, no signup.

โšก Open Dark Mode Generator
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.