light-dark()
CSS · color-scheme
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.
CSS · color-scheme
This browser does not support light-dark() with colors. The background color is not applied.
This browser does not support this image combination in light-dark(). The background is shown without an image as a fallback.
<article class="light-dark-demo">
<div class="demo-image" aria-hidden="true"></div>
<h2>light-dark()</h2>
<p>CSS · color-scheme</p>
</article><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>.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));
}
}.light-dark-demo {
color-scheme: light dark;
color: CanvasText;
background-color: light-dark(#fff5e6, #182449);
padding: 1rem;
.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))) {
background-image: light-dark(linear-gradient(135deg, #fff1cc, #ff9566), linear-gradient(135deg, #182449, #7854ad));
}
}
}Educational guide to light-dark()
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.
Two arguments, one choice; there is no interpolation between them.
The system or browser expresses a theme preference.
color-scheme declares which modes are supported; it can be inherited or set locally.
light-dark() selects the argument matching the scheme in use.
Declare coherent background and text colors for each mode. The browser adapts native controls and system colors; your brand colors need their own rules.
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.
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.
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.
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.
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: 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.
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.
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.
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.
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.
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.
Light · light
This combination is unsupported: background without an image.
This combination is unsupported: background without an image.
Dark · dark
This combination is unsupported: background without an image.
This combination is unsupported: background without an image.
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.
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.
Text and surface change together; preference decides automatic mode.
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.
Diagnose what decides the scheme and where each value is computed.
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.