Product · Guides
Dark mode and light mode with CSS variables: a dark-first design system.
How to build a dark-first design with a proper light theme using CSS custom properties and a data-theme attribute. Tokens, contrast checks and per-page accents.
Key takeaways
- Define colours, spacing and type as CSS custom properties, then override the colour set per theme.
- Treat both themes as first-class and check contrast in each one.
- Respect the system preference and let the visitor override it, remembering the choice.
- Accent colours often need darker shades in light mode.
Tokens first
The foundation is a set of design tokens defined once as CSS custom properties on :root: colours, spacing steps, border radii and font sizes. Components use the tokens and never hard-code a colour. This site is dark-first, so the dark values are the default:
:root {
--bg-page: #101012;
--text-primary: #ededed;
--accent: #f5654a;
}
The light theme is a small set of overrides:
:root[data-theme="light"] {
--bg-page: #fdfdfc;
--text-primary: #19191c;
--accent: #c24028;
}
Because every component reads the tokens, a new page gets both themes with no extra work.
Respect the system, let the visitor decide
Use prefers-color-scheme so a visitor's operating-system setting applies by default:
@media (prefers-color-scheme: light) {
:root:not([data-theme="dark"]) { /* light values */ }
}
The :not([data-theme="dark"]) part lets a manual choice win over the system preference. A small toggle sets data-theme to light or dark and saves it in localStorage. To avoid a flash of the wrong theme on load, apply the saved choice with a tiny inline script in the <head>. See how to allow that inline script under a strict CSP.
Light mode is not just inverted dark mode
The most common failure is a light theme that was an afterthought. Things to check:
- Accent colours. A coral that glows on near-black can look washed out on white. In my light theme the accent is darker so that links and buttons keep enough contrast.
- Shadows. Dark UIs often use glows or borders; light UIs need real shadows.
- Text contrast. Aim for at least 4.5:1 for body text. Check muted text, which tends to fail first.
- Images. Logos and illustrations may need a different version, or a background, on the opposite theme.
- Borders. A subtle border on dark may disappear on light.
Checking contrast in both themes is part of finishing a page, not an afterthought.
Per-page accents
Each project page on this site loads a tiny stylesheet that redefines only the accent variables. That is how a hospital system, a parking app and a creator tool feel different while sharing every layout rule. The same idea works for brands in a multi-tenant app. See why every project page has its own mood.
:root { --accent:#25b26a; --accent-hover:#1c8f54; --accent-soft:#25b26a1f; }
Remember to provide the light-theme version of each accent, since a bright green or orange often needs darkening.
Accessibility and motion
- Do not rely on colour alone to convey meaning.
- Keep focus outlines visible in both themes.
- Respect
prefers-reduced-motionfor animations. - Set the
theme-colormeta tag for mobile browsers.
A short checklist
- Define tokens for colour, spacing, radius and type.
- Build components with tokens only.
- Add the light overrides and the system-preference media query.
- Add a toggle and persist the choice.
- Avoid the flash with an early inline script.
- Test contrast and every component in both themes.
Frequently asked questions
Should dark mode be the default?
It depends on your audience and content. Following the system preference is the safest default, with a clear manual toggle.
Do I need two sets of images?
Not usually. SVG icons with currentColor adapt automatically. Photos rarely need changing.
How do I test contrast quickly?
Browser developer tools show contrast ratios for text, and there are free checkers that take two hex values.
Want a design system that holds up across themes?
Get in touch