Support0

CSS light-dark() playground

Choose a background color and image for each color scheme. CSS selects the first argument in light mode and the second in dark mode. Color and image variants have independent support.

Ask AI

CSS light-dark() playground

Response generated with openai/gpt-5.4-nano. AI can make mistakes. Always review the result.

light-dark()

CSS · color-scheme

This browser does not support light-dark() with colors. The background color is not applied.

HTML
<article class="light-dark-demo">
  <div class="demo-image" aria-hidden="true"></div>
  <h2>light-dark()</h2>
  <p>CSS · color-scheme</p>
</article>
Tailwind CSS
Examples for Tailwind CSS 4.3 with the default theme. Custom values keep arbitrary classes when there is no equivalent utility. Changing the theme can change utility values.
<article class="scheme-light-dark text-[CanvasText] bg-[light-dark(#fff5e6,#182449)] p-4">
  <div class="min-block-40 bg-none bg-cover bg-center supports-[background-image:light-dark(linear-gradient(135deg,_#fff1cc,_#ff9566),_linear-gradient(135deg,_#182449,_#7854ad))]:bg-[image:light-dark(linear-gradient(135deg,_#fff1cc,_#ff9566),_linear-gradient(135deg,_#182449,_#7854ad))]" aria-hidden="true"></div>
  <h2>light-dark()</h2>
  <p>CSS · color-scheme</p>
</article>
CSS
.light-dark-demo {
  color-scheme: light dark;
  color: CanvasText;
  background-color: light-dark(#fff5e6, #182449);
  padding: 1rem;
}

.light-dark-demo .demo-image {
  min-block-size: 10rem;
  background-image: none;
  background-size: cover;
  background-position: center;
}

@supports (background-image: light-dark(linear-gradient(135deg, #fff1cc, #ff9566), linear-gradient(135deg, #182449, #7854ad))) {
  .light-dark-demo .demo-image {
    background-image: light-dark(linear-gradient(135deg, #fff1cc, #ff9566), linear-gradient(135deg, #182449, #7854ad));
  }
}

Educational guide to light-dark()

CSS colors and themes

Define two alternatives and let CSS choose according to the color scheme used by the element. Learn to coordinate text, backgrounds, controls, and local themes while keeping preference and appearance distinct.

Mental model

Two arguments, one choice; there is no interpolation between them.

  1. Preference

    The system or browser expresses a theme preference.

  2. Element scheme

    color-scheme declares which modes are supported; it can be inherited or set locally.

  3. Selected alternative

    light-dark() selects the argument matching the scheme in use.

Static conceptual diagramWith light dark, preference participates in the choice. With a single mode, the element uses that scheme. The following samples test real CSS.

Design a complete pair

Declare coherent background and text colors for each mode. The browser adapts native controls and system colors; your brand colors need their own rules.

Scope is local

A section can use another scheme. The property does not create a global theme selector or change the preference queried by @media in that document.

light-dark(light, dark) returns the first alternative in a light scheme and the second in a dark one. It does not inspect background brightness, transform colors, or check contrast. A black background alone does not activate the second argument.

Schemes and native controls

Compare automatic mode with two samples pinned for this demonstration.

color-scheme: light dark allows both modes and normally follows the user’s preference. light or dark restricts the element to one mode. The property inherits and affects native controls and system colors, but does not rewrite an arbitrary author palette.

The same pair in three contexts

Check and uncheck the boxes to compare how a native control looks in each scheme. Only their checked state changes; color-scheme determines each card’s theme.

Automatic · light dark

Light · light

Dark · dark

No support for light-dark() colors: this sample keeps its fallback palette.

The first card follows the system or browser preference, even when you change the BaselineLab theme. The others keep their scheme. The checkboxes are operable native controls; their appearance depends on the platform.
The same pair in three contexts
<div class="ld-guide-schemes">
  <div class="auto">
    <p>Automatic · light dark</p>
    <label><input type="checkbox"> Try native checkbox</label>
  </div>
  <div class="light">
    <p>Light · light</p>
    <label><input type="checkbox"> Try native checkbox</label>
  </div>
  <div class="dark">
    <p>Dark · dark</p>
    <label><input type="checkbox"> Try native checkbox</label>
  </div>
  <p class="unsupported">No support for light-dark() colors: this sample keeps its fallback palette.</p>
</div>
.ld-guide-schemes {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;

  > div {
    flex: 1 1 12rem;
    min-inline-size: 0;
    padding: min(1rem, 5vi);
    border: 1px solid currentColor;
    border-radius: 16px;
    corner-shape: squircle;
    font: 1rem/1.6 system-ui, sans-serif;
    overflow-wrap: anywhere;
    color: #172554;
    background-color: #fff5e6;
  }

  p {
    margin: 0;
  }

  label {
    display: block;
    margin-block-start: 0.5rem;
  }

  input {
    font: inherit;
  }

  .auto {
    color-scheme: light dark;
  }

  .light {
    color-scheme: light;
  }

  .dark {
    color-scheme: dark;
  }

  .unsupported {
    color: light-dark(oklch(0.22 0.01 95), oklch(0.95 0.01 95));
  }

  @supports (color: light-dark(black, white)) {
    > div {
      color: light-dark(#172554, #f8fafc);
      background-color: light-dark(#fff5e6, #182449);
    }

    .unsupported {
      display: none;
    }
  }
}

For a page, declare the scheme on :root. A meta name="color-scheme" in head, before stylesheets, informs the browser early and may reduce loading flashes. Reserve local exceptions for contexts that need them and respect the user’s choice.

Tokens and inheritance in local themes

Inheriting an expression differs from inheriting a computed color.

An unregistered custom property can carry light-dark() to where var() is used. In contrast, color inherits the value already computed on the parent. When a child changes scheme, assign color: var(--card-text) and its background again; changing color-scheme alone can leave text and surface in different modes.

Light parent, dark inner card

Light parent: the tokens are defined here.

Dark child: color and background-color are reapplied here.

No support for light-dark() colors: this sample keeps its fallback palette.

The outside stays light. The inside sets dark and reapplies text and background from the same tokens. Without support for the function, both surfaces keep light fallback colors.
Light parent, dark inner card
<div class="ld-guide-tokens">
  <p>Light parent: the tokens are defined here.</p>
  <div class="local-dark">
    <p>Dark child: color and background-color are reapplied here.</p>
  </div>
  <p class="unsupported">No support for light-dark() colors: this sample keeps its fallback palette.</p>
</div>
.ld-guide-tokens {
  color-scheme: light;
  --card-text: #172554;
  --card-bg: #fff5e6;
  padding: min(1rem, 5vi);
  border: 1px solid currentColor;
  border-radius: 16px;
  corner-shape: squircle;
  font: 1rem/1.6 system-ui, sans-serif;
  overflow-wrap: anywhere;
  color: var(--card-text);
  background-color: var(--card-bg);

  p {
    margin: 0;
  }

  .local-dark {
    color-scheme: dark;
    margin-block: 1rem;
    padding: min(1rem, 5vi);
    border: 1px solid currentColor;
    border-radius: 16px;
    corner-shape: squircle;
    font: 1rem/1.6 system-ui, sans-serif;
    overflow-wrap: anywhere;
    /* Reapply color where the local scheme changes. */
    color: var(--card-text);
    background-color: var(--card-bg);
  }

  @supports (color: light-dark(black, white)) {
    --card-text: light-dark(#172554, #f8fafc);
    --card-bg: light-dark(#fff5e6, #182449);

    .unsupported {
      display: none;
    }
  }
}

This recipe’s tokens do not use @property. Registering an inheritable token with syntax: "<color>" makes it compute as a color before inheritance; that can help preserve the parent’s color, but changes this local reevaluation pattern. Also review any other inheritable color properties you use.

Preference versus local theme

A component’s scheme does not change a document media query.

prefers-color-scheme queries a user preference; light-dark() responds to the element’s scheme. Setting dark on a card does not make @media (prefers-color-scheme: dark) match. That is why a media query alone cannot reproduce every local theme.

Two decisions inside a dark context

light-dark() · local dark scheme

@media · user preference

The dark media query does not match: the light palette is used.

The dark media query matches: the dark palette is used.

No support for light-dark() colors: this sample keeps its fallback palette.

The first sample always uses the function’s dark branch when supported. The second follows the media query and states the current preference in text. With a light preference, the surfaces differ even though they share color-scheme: dark.
Two decisions inside a dark context
<div class="ld-guide-preferences">
  <div class="local"><p>light-dark() · local dark scheme</p></div>
  <div class="preference">
    <p>@media · user preference</p>
    <p class="system-light">The dark media query does not match: the light palette is used.</p>
    <p class="system-dark">The dark media query matches: the dark palette is used.</p>
  </div>
  <p class="unsupported">No support for light-dark() colors: this sample keeps its fallback palette.</p>
</div>
.ld-guide-preferences {
  color-scheme: dark;
  display: grid;
  gap: 1rem;
  padding: min(1rem, 5vi);
  border: 1px solid currentColor;
  border-radius: 16px;
  corner-shape: squircle;
  font: 1rem/1.6 system-ui, sans-serif;
  overflow-wrap: anywhere;
  color: CanvasText;
  background-color: Canvas;

  > div {
    padding: min(1rem, 5vi);
    border: 1px solid currentColor;
    border-radius: 16px;
    corner-shape: squircle;
    font: 1rem/1.6 system-ui, sans-serif;
    overflow-wrap: anywhere;
    color: #f8fafc;
    background-color: #182449;
  }

  p {
    margin: 0;
  }

  @supports (color: light-dark(black, white)) {
    .local {
      color: light-dark(#172554, #f8fafc);
      background-color: light-dark(#fff5e6, #182449);
    }

    .unsupported {
      display: none;
    }
  }

  .preference {
    color: #172554;
    background-color: #fff5e6;

    .system-dark {
      display: none;
    }

    @media (prefers-color-scheme: dark) {
      color: #f8fafc;
      background-color: #182449;

      .system-light {
        display: none;
      }

      .system-dark {
        display: block;
      }
    }
  }
}

For a manual theme choice, an application can set color-scheme from its state. This guide does not install a global switch or persist preferences: demonstrations are isolated from the theme and playground controls.

Colors, gradients, and images

The same function has two forms with independent compatibility.

A gradient is a CSS image, not a color. Use two <color> values for background-color; use two <image> values or the keyword none for background-image. Do not mix a color and an image in one function. You can also use light-dark() colors inside gradient stops without selecting two complete images.

Switch the whole image and select none

Light · light

Two gradients · first or second image

This combination is unsupported: background without an image.

url() in light · none in dark

This combination is unsupported: background without an image.

Dark · dark

Two gradients · first or second image

This combination is unsupported: background without an image.

url() in light · none in dark

This combination is unsupported: background without an image.

When the expression is supported, the gradient is warm in light mode and nocturnal in dark mode. The second row displays the sun SVG in light mode and no image in dark mode. Each combination has its own @supports and notice; text stays outside the image.
Switch the whole image and select none
<div class="ld-guide-images">
  <div class="light">
    <p>Light · light</p>
    <figure class="gradient">
      <div class="paint" aria-hidden="true"></div>
      <figcaption>Two gradients · first or second image</figcaption>
      <p class="unsupported">This combination is unsupported: background without an image.</p>
    </figure>
    <figure class="image">
      <div class="paint" aria-hidden="true"></div>
      <figcaption>url() in light · none in dark</figcaption>
      <p class="unsupported">This combination is unsupported: background without an image.</p>
    </figure>
  </div>
  <div class="dark">
    <p>Dark · dark</p>
    <figure class="gradient">
      <div class="paint" aria-hidden="true"></div>
      <figcaption>Two gradients · first or second image</figcaption>
      <p class="unsupported">This combination is unsupported: background without an image.</p>
    </figure>
    <figure class="image">
      <div class="paint" aria-hidden="true"></div>
      <figcaption>url() in light · none in dark</figcaption>
      <p class="unsupported">This combination is unsupported: background without an image.</p>
    </figure>
  </div>
</div>
.ld-guide-images {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;

  > div {
    flex: 1 1 12rem;
    min-inline-size: 0;
    padding: min(1rem, 5vi);
    border: 1px solid currentColor;
    border-radius: 16px;
    corner-shape: squircle;
    font: 1rem/1.6 system-ui, sans-serif;
    overflow-wrap: anywhere;
    color: CanvasText;
    background-color: Canvas;
  }

  .light {
    color-scheme: light;
  }

  .dark {
    color-scheme: dark;
  }

  p, figure {
    margin: 0;
  }

  .light figure figcaption, .dark figure figcaption {
    color: inherit;
    font: inherit;
  }

  figure {
    margin-block-start: 1rem;
  }

  .paint {
    min-block-size: 6rem;
    border: 1px solid currentColor;
    background-color: Canvas;
    background-image: none;
    background-size: cover;
    background-position: center;
  }

  .image .paint {
    background-size: 4rem 4rem;
    background-repeat: no-repeat;
  }

  @supports (background-image: light-dark(linear-gradient(135deg, #fff1cc, #ff9566), linear-gradient(135deg, #182449, #7854ad))) {
    .gradient .paint {
      background-image: light-dark(linear-gradient(135deg, #fff1cc, #ff9566), linear-gradient(135deg, #182449, #7854ad));
    }

    .gradient .unsupported {
      display: none;
    }
  }

  @supports (background-image: light-dark(url("/assets/light-dark/sun.svg"), none)) {
    .image .paint {
      background-image: light-dark(url("/assets/light-dark/sun.svg"), none);
    }

    .image .unsupported {
      display: none;
    }
  }

  @media (forced-colors: active) {
    .paint {
      background-image: none;
    }
  }
}

This recipe’s backgrounds are decorative. For images that convey content, use HTML with a text alternative and an appropriate resource strategy. @supports checks syntax, not file loading or content: replace the example URL with your own resource and keep a readable background if loading fails.

A recipe with a fallback

Keep valid values before enabling the function.

The base defines a light pair and a media query adds the dark one. Only inside @supports do the tokens contain light-dark(). A browser that accepts custom properties but does not know the function therefore avoids receiving a token that invalidates color or background-color when var() is substituted.

Automatic palette with progressive enhancement

Text and surface change together; preference decides automatic mode.

The card follows preference in both paths. With support it uses light-dark(); otherwise, the media query selects colors. It needs no JavaScript and keeps a readable pair in forced colors.
Automatic palette with progressive enhancement
<div class="ld-guide-fallback"><p>Text and surface change together; preference decides automatic mode.</p></div>
.ld-guide-fallback {
  color-scheme: light dark;
  --card-text: #172554;
  --card-bg: #fff5e6;
  padding: min(1rem, 5vi);
  border: 1px solid currentColor;
  border-radius: 16px;
  corner-shape: squircle;
  font: 1rem/1.6 system-ui, sans-serif;
  overflow-wrap: anywhere;
  color: var(--card-text);
  background-color: var(--card-bg);

  p {
    margin: 0;
  }

  @media (prefers-color-scheme: dark) {
    --card-text: #f8fafc;
    --card-bg: #182449;
  }

  @supports (color: light-dark(black, white)) {
    --card-text: light-dark(#172554, #f8fafc);
    --card-bg: light-dark(#fff5e6, #182449);
  }

  @media (forced-colors: active) {
    color: CanvasText;
    background-color: Canvas;
  }
}

The media query fallback covers this automatic recipe. If your app allows a theme different from the preference or local subtrees, apply those states to the fallback tokens too. var(--token, color) does not detect that a defined token’s content is invalid for a property.

Common pitfalls

Diagnose what decides the scheme and where each value is computed.

The first color always appears

Expected result
The pair changes when dark mode is preferred.
Cause
The element does not support dark or inherits a scheme pinned to light.
Solution
Inspect color-scheme on the element and its ancestors; allow light dark where it should follow preference.

Dark background with the light parent’s text

Expected result
Text and background belong to the same local mode.
Cause
color inherits a computed value; changing only the scheme does not reevaluate the parent’s value.
Solution
Reapply the text property from the token at the new scheme boundary and define its background.

The media query ignores the dark card

Expected result
The component keeps its mode even when the system prefers light.
Cause
prefers-color-scheme does not query that card’s color-scheme.
Solution
Use light-dark() for local colors and reflect the component’s explicit state in the fallback.

Colors work, but the image does not

Expected result
The image for each mode or an explicit fallback is selected.
Cause
The image form has independent support; the resource can also fail.
Solution
Query @supports with background-image and the actual expression, check the URL, and keep a valid background.

The var() fallback does not apply

Expected result
An older browser keeps readable colors.
Cause
The token exists but contains an unsupported function when used as a color.
Solution
Define valid base tokens and replace them inside @supports; do not rely on var()’s second argument to validate color syntax.

Support and compatibility

Separate catalog data from what each example demonstrates.

As of the review date, Web Platform DX records the color form as Baseline Newly Available since May 13, 2024 and the image form since September 14, 2026. Do not infer image support from a color test: light-dark(white, black).

The cards query the compatibility catalog; sample notices depend on @supports for their actual expression. Accepting syntax does not certify rendering without bugs, image loading, or sufficient contrast. Check both modes, control states, transparent backgrounds, and forced colors in your target browsers.