Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Functions

The light-dark() function

Reference for the CSS light-dark() function: pick one color for light mode and another for dark mode, how color-scheme controls it, and live examples.

The light-dark() function takes two colors and returns the first when the element uses a light color scheme and the second when it uses a dark one. It lets you write a theme-aware color in a single declaration instead of repeating it inside a prefers-color-scheme media query.

It only works when the element's color-scheme allows both schemes. Without that, it always picks the light value.

CSS
:root {
  color-scheme: light dark;
}
body {
  background: light-dark(#ffffff, #111827);
  color: light-dark(#1f2937, #e5e7eb);
}

Try it

Switch the scheme on a card

The first two cards force a scheme; the third follows your system preference.

Syntax

CSS
light-dark(<color>, <color>)

Syntax and arguments

Argument Description
First <color> Used when the element's used color scheme is light.
Second <color> Used when the used color scheme is dark.

Usage notes

  • Set color-scheme first. Put color-scheme: light dark on :root so the browser follows the user's preference. Setting only light or only dark makes the function always pick that side.
  • It follows the element, not the page. Because color-scheme inherits, a card that sets color-scheme: dark inside a light page gets the dark values.
  • Colors only (in practice). Use it for color values. For other properties, such as images or sizes, use a prefers-color-scheme media query or switch custom properties.
  • Replaces duplication. One declaration instead of a base value plus a media query override.
  • A theme toggle can set color-scheme on :root from a class or attribute, and every light-dark() color updates at once.
  • Form controls and scrollbars also follow color-scheme, so they match your colors.

Browser support

Supported in current versions of the major browsers; it is a fairly recent addition, so check a compatibility table and add a plain fallback color before it if you support older ones.