The light-dark()
CSS <color>
function enables setting two colors for a property - returning one of the two colors options by detecting if the developer has set a light or dark color scheme or the user has requested light or dark color theme - without needing to encase the theme colors within a prefers-color-scheme
media feature query. Users are able to indicate their color-scheme preference through their operating system settings (e.g. light or dark mode) or their user agent settings. The light-dark()
function enables providing two color values where any <color>
value is accepted. The light-dark()
CSS color function returns the first value if the user's preference is set to light
or if no preference is set and the second value if the user's preference is set to dark
.
To enable support for the light-dark()
color function, the color-scheme
must have a value of light dark
, usually set on the :root
pseudo-class.
:root { color-scheme: light dark; } body { color: light-dark(#333b3c, #efefec); background-color: light-dark(#efedea, #223a2c); }