Skip to content

The publication for web craftspeople Thursday, 26 November 2026

Design & UX

CSS light-dark(): Handling Light and Dark Themes Without Duplicating Variables

The CSS light-dark() function sets both the light and dark value of a property in a single declaration, with no duplicated variables and no extra media query. Supported by every major browser, it strips out a good chunk of the plumbing a…

Code CSS affiché sur un écran d'ordinateur en mode sombre
Photo : Bibek ghosh / Pexels

The CSS light-dark() function sets both the light and dark value of a property in a single declaration, with no duplicated variables and no separate media-query selector. Rated Baseline “widely available” since May 2024, it’s supported in Chrome and Edge 123, Firefox 120, and Safari 17.5, which covers nearly all of a professional site’s traffic today. It doesn’t replace a full design-token architecture, but it strips out a good chunk of the plumbing a light/dark theme used to require.

The Problem light-dark() Solves

The classic approach to a light/dark theme in CSS relies on two sets of variables: a :root block for light values, then a second block inside @media (prefers-color-scheme: dark) or under a [data-theme="dark"] selector that rewrites the same properties with dark values. For every color, two separate declarations end up maintained, sometimes dozens of lines apart in the stylesheet.

/* Before: two blocks to keep in sync */
:root {
  --color-bg: #ffffff;
  --color-text: #1a1a1a;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #111317;
    --color-text: #e8e8e8;
  }
}

light-dark() groups both values in the same spot: the light/dark pair lives in a single declaration, which makes the contrast between the two immediately visible when reading the code and avoids missing one of the two variants during an edit.

Setting It Up, Step by Step

The function only kicks in if the color-scheme property is set to light dark on the relevant element, usually :root. Without that declaration, light-dark() silently falls back to its first value, which is the single most common mistake caught in code review.

:root {
  color-scheme: light dark;

  --color-bg: light-dark(#ffffff, #111317);
  --color-text: light-dark(#1a1a1a, #e8e8e8);
  --color-border: light-dark(#e2e2e2, #2a2d33);
}

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

The browser picks which value to return based on the user’s system preference (prefers-color-scheme), with no media query anywhere in the stylesheet. The code reads more clearly, and the number of properties to keep in sync between the two themes is cut in half.

Respecting an Explicit Theme Choice (data-theme)

Most sites also offer a manual switch that should override the system preference. The toggle works by rewriting the color-scheme value itself, not the individual variables, which fits directly alongside the design-tokens method already covered for building a maintainable light/dark theme, whose low-level layer it simplifies.

:root {
  color-scheme: light dark; /* follows the system preference by default */
}

:root[data-theme="light"] {
  color-scheme: light; /* forces light, regardless of the system */
}

:root[data-theme="dark"] {
  color-scheme: dark; /* forces dark, regardless of the system */
}

The light-dark() declarations defined above don’t need to change at all: they react automatically to any change in color-scheme, whether it comes from the system or from an attribute set by a JavaScript toggle button. A theme switch that forgets the user’s choice on every reload isn’t worth much; persistence works by reading and writing that same attribute, with the color logic itself never leaving the stylesheet.

const toggle = document.querySelector('[data-theme-toggle]');
const root = document.documentElement;

function applyTheme(theme) {
  root.setAttribute('data-theme', theme);
  localStorage.setItem('theme', theme);
}

const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
  applyTheme(savedTheme);
}

toggle.addEventListener('click', () => {
  const next = root.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
  applyTheme(next);
});

The script only touches a single attribute: all the color logic stays declarative, in the CSS, where it’s easier to audit and evolve than logic scattered across several JavaScript files.

Browser Support and Fallback

BrowserMinimum versionNote
Chrome / Edge123full support since March 2024
Firefox120full support since November 2023
Safari17.5full support since May 2024

For a project that still needs to support older versions, a progressive fallback via @supports keeps the old media-query approach as a safety net, without duplicating the business logic.

@supports not (background: light-dark(#fff, #000)) {
  @media (prefers-color-scheme: dark) {
    :root {
      --color-bg: #111317;
      --color-text: #e8e8e8;
    }
  }
}

Images, Form Borders, and Contrast

The function accepts images as well as colors. A logo that needs to invert depending on the theme, or a background illustration too dark for light mode, can be handled with the exact same syntax as a color.

.site-logo {
  background-image: light-dark(
    url("/images/logo-dark-on-light.svg"),
    url("/images/logo-light-on-dark.svg")
  );
}

input, textarea {
  border-color: light-dark(#d0d0d5, #3a3d44);
  accent-color: light-dark(#2563eb, #60a5fa);
}

For form components like input or textarea, this approach specifically avoids the “white field on a dark background” effect left by an incomplete theme, a common visual defect on sites that only handled dark mode for text and the overall background. Grouping both values in a single declaration also makes contrast easier to check, since the two colors being compared sit side by side in the code instead of in two distant blocks.

PairContextRecommended minimum ratio
Body text / backgroundpage body4.5:1 (WCAG AA)
Large text / backgroundheadings, 18px bold or larger3:1 (WCAG AA)
UI element / backgroundfield borders, informational icons3:1 (WCAG AA)

Chrome, Firefox, and Safari’s dev tools all let you simulate prefers-color-scheme without touching system settings, which makes it possible to check both values of a single light-dark() declaration in a couple of clicks, without reloading the page or disabling light mode just to test dark mode.

Where the Function Isn’t Enough Yet

light-dark() applies to color and image values, but it doesn’t replace a full token architecture: spacing, typography, and shadows still follow their own theming logic, often independent of light/dark mode. It belongs to the same family of recent CSS tools that reduce reliance on JavaScript for visual behavior, alongside container queries for adaptive components: each closes a case that only a JavaScript library could handle as recently as two years ago.

light-dark() replaces in a single line what used to take two blocks of variables and a media query.

Most common mistake: forgetting color-scheme: light dark on :root. Without that line, light-dark() always returns its first value, and dark mode appears “broken” even though the syntax is correct.

What to Remember

light-dark() groups both the light and dark value of a property into a single declaration, provided color-scheme: light dark is set on the document root. A data-theme attribute can force a specific mode by rewriting only the color-scheme property, without touching the variables themselves. Browser support covers Chrome and Edge 123+, Firefox 120+, and Safari 17.5+, which allows direct adoption on most professional projects, with an @supports fallback for audiences that still need one.

On the projects where I’ve replaced the old variables-plus-media-query duo with light-dark(), the gain hasn’t been dramatic in performance, but it’s been very clear in maintainability: nobody has absent-mindedly added a color to only one of the two themes since. It’s the kind of CSS feature that changes nothing about what you can build, only how easy it stays to keep building it well six months later — Simon Janvier.

Further reading: the specification and detailed compatibility data are documented on MDN Web Docs.

Read next