· 1 min read
Dark mode with light-dark()
Rewrote each token in tokens.css as light-dark(light value, dark value).
What we did
- Rewrote each token in
tokens.cssaslight-dark(light value, dark value). - Set
color-scheme: light darkon:root, plus[data-theme='light']and[data-theme='dark']overrides for the coming toggle. - Nothing outside
tokens.csschanged.
What I learned
color-schemedoes two jobs. It driveslight-dark(), and it makes the browser's own UI (scrollbars, form controls) follow the theme.light-dark()keeps both values on one line, instead of repeating every token inside@media (prefers-color-scheme: dark).- With no
data-themeattribute the site follows the OS. Setting the attribute forces a theme. - Lightning CSS (Vite's minifier) replaced
light-dark()with a fallback for older browsers. The[data-theme]selectors are more specific than the generated media-query:root, so a forced theme still beats the OS. - Test in DevTools with Ctrl+Shift+P → "Emulate CSS prefers-color-scheme: dark".
Links
Next
- Theme toggle with
$stateandlocalStorage, and an inline script inapp.htmlso the page doesn't flash the wrong theme.