CSS Light-Dark Function: Adaptive Colors Without Media Query Duplication

light-dark(): one declaration per color for light and dark mode

A dark mode in a theme usually means every color twice: once in :root, once more inside @media (prefers-color-scheme: dark), and a third time for a manual switch with a [data-theme="dark"] selector. The CSS function light-dark() takes both values in one place and picks the one that matches the element’s color-scheme. A manual switch then only changes color-scheme.

Problem: A theme’s dark mode is spread over three blocks of custom properties that must be kept in sync. A visitor’s manual choice has to override the system setting, and the browser’s own form controls and scrollbars stay light in dark mode.

Solution: Declare color-scheme: light dark on the root, define each color as light-dark( light, dark ), and implement the manual switch by setting color-scheme to light or dark on the root element, stored in localStorage and applied before the first paint.

The colors

:root {
    color-scheme: light dark;

    --ha-bg:      light-dark(#ffffff, #121417);
    --ha-text:    light-dark(#1f2328, #e6e8eb);
    --ha-muted:   light-dark(#59636e, #9aa4af);
    --ha-border:  light-dark(#d1d9e0, #30363d);
    --ha-accent:  light-dark(#0b5cad, #6cb6ff);
}

body {
    background: var(--ha-bg);
    color: var(--ha-text);
}

color-scheme: light dark says the page supports both and lets the browser follow the system setting. light-dark() returns its first value when the used color scheme is light and the second when it is dark. The same declaration also switches the browser’s own UI: form fields, scrollbars and the default canvas color become dark, which per-property media queries never did.

The manual switch

:root[data-color-scheme="light"] { color-scheme: light; }
:root[data-color-scheme="dark"]  { color-scheme: dark; }
<script>
    // inline in <head>, before the stylesheets render: no flash of the wrong theme
    try {
        const saved = localStorage.getItem( 'haColorScheme' );
        if ( saved ) document.documentElement.dataset.colorScheme = saved;
    } catch ( e ) {}
</script>
document.querySelector( '#ha-scheme-toggle' ).addEventListener( 'click', () => {
    const root = document.documentElement;
    const isDark = getComputedStyle( root ).colorScheme === 'dark'
        || ( ! root.dataset.colorScheme && matchMedia( '(prefers-color-scheme: dark)' ).matches );
    const next = isDark ? 'light' : 'dark';
    root.dataset.colorScheme = next;
    try { localStorage.setItem( 'haColorScheme', next ); } catch ( e ) {}
} );

The switch changes only color-scheme; every light-dark() follows. Without a saved choice, the system setting applies. The inline script in the head is printed with wp_print_inline_script_tag() on wp_head at an early priority, so it runs before the body is painted.

In theme.json

{
    "version": 3,
    "settings": {
        "color": {
            "palette": [
                { "slug": "base", "name": "Base", "color": "light-dark(#ffffff, #121417)" },
                { "slug": "contrast", "name": "Contrast", "color": "light-dark(#1f2328, #e6e8eb)" }
            ]
        }
    },
    "styles": {
        "css": ":root { color-scheme: light dark; }"
    }
}

The palette values are output as custom properties, and blocks that use “Base” and “Contrast” switch with the scheme. The color picker in the editor shows such values as a plain text value, not a swatch with a hex code, which editors should know.

Limits

light-dark() accepts only colors, not images or lengths. For a different logo or a different shadow in dark mode, keep a media query or the [data-color-scheme] selector for those few rules. Derived colors work well with it: color-mix() and OKLCH.

NOTE: light-dark() works in Chrome 123, Firefox 120 and Safari 17.5 (May 2024). For older browsers, the custom properties are invalid at computed time and fall back to inherited or initial values, so provide plain fallbacks inside @supports not (color: light-dark(white, black)) for the base colors.