A dark-mode bug is often a pairing bug. On the right canvas, both of our blues work as link text. Put the dark-mode blue on the light canvas and its contrast falls to 2.43:1. A theme should switch surfaces and their matching ink together, rather than swapping a few isolated accent colors.
The browser chooses a scheme; tokens follow it
CSS Color 5 defines the color form of light-dark() in terms of the element’s used color scheme. color-scheme: light dark lets the browser choose among both. An explicit color-scheme: dark selects the second color; light selects the first. The color form has shipped across the major engines since May 2024; web.dev’s compatibility note covers that form, not separate support for images.
| Token | Light scheme | Dark scheme |
|---|---|---|
| --canvas | #F8FAFC | #0F172A |
| --ink | #1E293B | #F1F5F9 |
| --link | #1D4ED8 | #60A5FA |
| --muted | #475569 | #94A3B8 |
Try System, Light and Dark in one HTML file
Save the example as an HTML file and pick a mode from the select. System removes the manual override; Light and Dark set it explicitly. The media query and plain HEX tokens cover the fallback. Feature detection checks the actual color property before putting light-dark() into custom properties. The example does not persist a manual choice across reloads.
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Light and dark token example</title>
<style>
:root {
color-scheme: light dark;
--canvas: #F8FAFC;
--ink: #1E293B;
--link: #1D4ED8;
--muted: #475569;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--canvas: #0F172A;
--ink: #F1F5F9;
--link: #60A5FA;
--muted: #94A3B8;
}
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] {
color-scheme: dark;
--canvas: #0F172A;
--ink: #F1F5F9;
--link: #60A5FA;
--muted: #94A3B8;
}
@supports (color: light-dark(#000, #fff)) {
:root, :root[data-theme], :root:not([data-theme="light"]) {
--canvas: light-dark(#F8FAFC, #0F172A);
--ink: light-dark(#1E293B, #F1F5F9);
--link: light-dark(#1D4ED8, #60A5FA);
--muted: light-dark(#475569, #94A3B8);
}
}
body { background: var(--canvas); color: var(--ink); font: 1rem system-ui; padding: 1rem; }
a { color: var(--link); text-decoration: underline; }
:focus-visible { outline: 2px solid var(--link); outline-offset: 3px; }
.caption { color: var(--muted); }
</style>
<main>
<h1>Reading theme</h1>
<label for="theme">Appearance</label>
<select id="theme">
<option value="system">System</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
<p>Try each mode, then <a href="#contrast">check the text pairs</a>.</p>
<p class="caption" id="contrast">Caption colors change with the canvas.</p>
</main>
<script>
const picker = document.querySelector('#theme');
function syncTheme() {
if (picker.value === 'system') delete document.documentElement.dataset.theme;
else document.documentElement.dataset.theme = picker.value;
}
picker.addEventListener('change', syncTheme);
syncTheme();
</script>
</html>With System selected, toggle the browser’s or OS’s color preference. Then choose the opposite theme in the select; the explicit choice should win. Tab through the select and the link to inspect focus. To test the fallback, temporarily remove the feature-query block and repeat all three choices.
Measure the pairs that can actually occur
| Foreground / background | Contrast | Normal text |
|---|---|---|
| #1E293B / #F8FAFC | 13.98:1 | AAA |
| #F1F5F9 / #0F172A | 16.30:1 | AAA |
| #475569 / #F8FAFC | 7.24:1 | AAA |
| #94A3B8 / #0F172A | 6.96:1 | AA, not AAA |
| #1D4ED8 / #F8FAFC | 6.41:1 | AA, not AAA |
| #60A5FA / #0F172A | 7.02:1 | AAA |
| #1D4ED8 / #0F172A | 2.66:1 | Fails AA |
| #60A5FA / #F8FAFC | 2.43:1 | Fails AA |
WCAG text contrast requires at least 4.5:1 for most text at AA. Check nested surfaces separately: a dark dialog can sit inside a light shell, or a badge can use a different fill than the canvas. Underline inline links and check required non-text indicators, including focus visibility, against their actual adjacent colors. Test your sRGB values in the Contrast Checker before calling the theme ready.
Frequently asked questions
How does CSS light-dark() choose a color?
It uses the element’s used color scheme. Opt in with color-scheme: light dark for the browser preference, or set color-scheme: light or dark to force the corresponding branch.
Does light-dark() replace a theme toggle?
No. It chooses values, but it does not provide a control or remember a choice. A toggle or select can set a data attribute, and the stylesheet can use it to set the color scheme.
Do I still need to check contrast in both themes?
Yes. A pair that passes in light mode may fail on a dark canvas. Test each actual text, control and state against its adjacent surface, not just the theme’s swatch list.