Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

light-dark(): choose a value for the used color scheme

Select a light or dark value through color-scheme, rather than through a theme class alone.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

color-scheme: light dark; color: light-dark(#0a141e, #e6f0fa);

light-dark() chooses its first value in a light color scheme and its second in a dark color scheme. The decision follows the element's used color-scheme, which can be influenced by inherited declarations and user preferences. A class called dark has no special meaning to this function unless the class also changes the relevant CSS.

The established form accepts two colors. The current CSS Color Level 5 draft additionally defines an image form with two image-or-none alternatives. These forms are distinct: mixing a color directly with an image is invalid. Test support for the form you need; accepting the color form does not by itself establish image-form support in another browser.

The example explicitly sets light and dark schemes, so it does not depend on the operating system theme. The color branches resolve to RGB 10,20,30 and 230,240,250. Chromium 157 also accepts the tested image pair and paints red or blue according to the scheme, while rejecting the mixed color/image expression. Appropriate foreground/background contrast still has to be designed.

Examples

const sample=async style=>{const svg='<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8"><rect width="8" height="8" fill="white"/><foreignObject width="8" height="8"><div xmlns="http://www.w3.org/1999/xhtml" style="width:8px;height:8px;'+style.replaceAll('&','&amp;').replaceAll('"','&quot;')+'"></div></foreignObject></svg>';const image=new Image();image.src='data:image/svg+xml;charset=utf-8,'+encodeURIComponent(svg);await image.decode();const canvas=document.createElement('canvas');canvas.width=canvas.height=8;const context=canvas.getContext('2d');context.drawImage(image,0,0);return [...context.getImageData(4,4,1,1).data].slice(0,3).join(',')};
const colors=[];
for(const scheme of ['light','dark']){const element=document.createElement('div');element.style.colorScheme=scheme;element.style.color='light-dark(rgb(10 20 30), rgb(230 240 250))';document.body.append(element);colors.push(scheme+'='+getComputedStyle(element).color)}
const imageValue='light-dark(linear-gradient(red,red), linear-gradient(blue,blue))';
const imageSupport=CSS.supports('background-image',imageValue);
const lightImage=await sample('color-scheme:light;background-image:'+imageValue);
const darkImage=await sample('color-scheme:dark;background-image:'+imageValue);
console.log(colors.join('; ')+'; image-form='+imageSupport+'; image-pixels='+lightImage+'/'+darkImage+'; mixed='+CSS.supports('background-image','light-dark(red,linear-gradient(blue,blue))'));

Result

light=rgb(10, 20, 30); dark=rgb(230, 240, 250); image-form=true; image-pixels=255,0,0/0,0,255; mixed=false

This test verifies both color selection and the newer image form in Chromium 157; it is not a universal compatibility claim.

Sources