Modern web applications require cohesive theming that responds dynamically to user preferences (prefers-color-scheme) as well as context-specific element overrides. Historically, developers duplicated styling declarations inside separate @media (prefers-color-scheme: dark) blocks or assigned heavy CSS class overrides. The native CSS light-dark() function standardises dual-mode color assignments in a single readable declaration. This tutorial explains how to configure light-dark() and audit color-scheme states in Google Chrome.
The Mechanics of CSS light-dark()
The light-dark() functional notation accepts two color values: light-dark(<light-color>, <dark-color>). The browser rendering engine resolves the property based on the element’s computed color-scheme property. If color-scheme resolves to light, the first color is applied; if it resolves to dark, the second color is applied. For the function to operate, color-scheme: light dark must be declared on the root or an ancestor container.
Authoring Dual-Theme Design Tokens
Configure dynamic CSS custom properties using light-dark() across your design system:
:root {
/* Enable dual color-scheme evaluation */
color-scheme: light dark;
/* Theme tokens */
--color-canvas: light-dark(#ffffff, #0f172a);
--color-surface: light-dark(#f8fafc, #1e293b);
--color-text-primary: light-dark(#0f172a, #f8fafc);
--color-text-secondary: light-dark(#64748b, #94a3b8);
--color-border: light-dark(#e2e8f0, #334155);
--color-brand: light-dark(#2563eb, #3b82f6);
}
body {
background-color: var(--color-canvas);
color: var(--color-text-primary);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
transition: background-color 0.25s ease, color 0.25s ease;
}
.card {
background-color: var(--color-surface);
border: 1px solid var(--color-border);
padding: 1.5rem;
border-radius: 0.75rem;
}
/* Force dark mode on specific component container regardless of root */
.inverted-sidebar {
color-scheme: dark;
}
Emulating Light and Dark Themes in Chrome DevTools
- Launch Google Chrome and navigate to your web application.
- Press
F12orCtrl + Shift + Ito launch DevTools. - Open the Rendering tab. If not visible, press
Ctrl + Shift + P, typeShow Rendering, and press Enter. - Locate the Emulate CSS media feature prefers-color-scheme dropdown.
- Toggle between
prefers-color-scheme: lightandprefers-color-scheme: dark. - Observe how all properties declared with
light-dark()update instantaneously without page reloads.
Auditing Computed Values in the Styles Pane
- In the Elements panel, select an element using
light-dark()tokens. - Look at the Styles panel. Chrome DevTools highlights the currently active color candidate while dimming the inactive candidate.
- Inspect the Computed panel to verify that the final rendered hex or OKLCH color matches the target theme palette.