| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/color_value/light-dark | [Back] [Original] |
Get to know MDN better
light-dark() CSS functionSince May 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
* Some parts of this feature may have varying levels of support.
The light-dark() CSS function accepts two colors or two images and returns a color or an image based on the active color scheme, without needing a prefers-color-scheme media feature.
/* Named color values */
light-dark(
black,
white
);
/* RGB color values */
light-dark(
rgb(0 0 0),
rgb(255 255 255)
);
/* image url values */
light-dark(
url("light-icon.png"),
url("dark-icon.png")
);
/* linear-gradient values */
light-dark(
linear-gradient(135deg, ghostwhite 20%, tomato),
linear-gradient(45deg, darkslategray 20%, gold)
);
/* Custom properties */
light-dark(
var(--light),
var(--dark)
);
The light-dark() function has two forms:
<color> values:
<color> (light)The <color> value used when the color-scheme is light or no preference is set.
<color> (dark)The <color> value used when the color-scheme is dark.
<image> values:
<image> (light)The <image> value used when the color-scheme is light or no preference is set.
<image> (dark)The <image> value used when the color-scheme is dark.
noneThe none keyword produces a fully transparent image with no natural size.
Users can 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 either two color values where any <color> value is accepted or two image values where any <image> value is accepted. The light-dark() function returns the first value if the used color scheme is light or if no preference is set and the second value if the used color scheme is 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);
}
<light-dark()> =This syntax reflects the latest standard as per CSS Color Module Level 5, CSS Images Module Level 4, CSS Images Module Level 5, Selectors Level 4. Not all browsers may have implemented every part. See Browser compatibility for support information.
<light-dark-color> |
<light-dark-image>
<light-dark-color> =
light-dark( <color> , <color> )
<light-dark-image> =
light-dark( [ <image> | none ] , [ <image> | none ] )
<image> =
<url> |
<image()> |
<image-set()> |
<cross-fade()> |
<element()> |
<gradient>
<image()> =
image( <image-tags>? [ <image-src>? , <color>? ]! )
<image-set()> =
image-set( <image-set-option># )
<cross-fade()> =
cross-fade( <cf-image># )
<element()> =
element( <id-selector> )
<image-tags> =
ltr |
rtl
<image-src> =
<url> |
<string>
<image-set-option> =
[ <image> | <string> ] [ <resolution> || type( <string> ) ]?
<cf-image> =
[ <image> | <color> ] &&
<percentage [0,100]>?
<id-selector> =
<hash-token>
By default, in supporting browsers, the color returned by the light-dark() color function depends on the user preference set through an operating system's settings (e.g., light or dark mode) or from a user agent setting. You can also change this setting in the browser's developer tools.
We include three sections to enable targeting light colors, dark colors, and the colors selected based on the user's preferred color scheme.
<h1><code>light-dark()</code> CSS function</h1>
<section>
<h2>Automatic</h2>
<p>This section will react to the users system or user agent setting.</p>
</section>
<section class="light">
<h2>Light</h2>
<p>
This section will be light due to the <code>color-scheme: light;</code>.
</p>
</section>
<section class="dark">
<h2>Dark</h2>
<p>This section will be dark due to the <code>color-scheme: dark;</code>.</p>
</section>
We include colors for both light and dark themes. We also define color-scheme for the document on the :root to enable the light-dark() color function for the entire document.
:root {
/* this has to be set to switch between light or dark */
color-scheme: light dark;
--light-bg: ghostwhite;
--light-color: darkslategray;
--light-code: tomato;
--dark-bg: darkslategray;
--dark-color: ghostwhite;
--dark-code: gold;
}
* {
background-color: light-dark(var(--light-bg), var(--dark-bg));
color: light-dark(var(--light-color), var(--dark-color));
}
code {
color: light-dark(var(--light-code), var(--dark-code));
}
In addition to enabling the light-dark() function, the color-scheme property enables overriding a user's color scheme for document sections. Forcing a page section to only use a light or dark color scheme can be done by setting the color-scheme property to light or dark.
Note: Generally this should not be done, we are using it here for demonstration purposes. If the user has made a preference, you generally should not override their preferences.
.light {
/* forces light color-scheme */
color-scheme: light;
}
.dark {
/* forces dark color-scheme */
color-scheme: dark;
}
section {
padding: 0.8rem;
}
This example uses the same HTML code as the previous example but includes a <div> instead of a <p>.
<h1><code>light-dark()</code> CSS function with images</h1>
<p class="supports">
Your browser does not support <code>light-dark()</code> with images.
</p>
<div class="wrapper">
<section>
<h2>Automatic</h2>
<div></div>
</section>
<section class="light">
<h2>Light</h2>
<div></div>
</section>
<section class="dark">
<h2>Dark</h2>
<div></div>
</section>
</div>
:root {
/* this has to be set to switch between light or dark */
color-scheme: light dark;
--light-bg: ghostwhite;
--light-color: darkslategray;
--light-code: tomato;
--dark-bg: darkslategray;
--dark-color: ghostwhite;
--dark-code: gold;
}
* {
background-color: light-dark(var(--light-bg), var(--dark-bg));
color: light-dark(var(--light-color), var(--dark-color));
}
.wrapper {
display: flex;
justify-content: space-around;
padding: 0.8rem;
}
.light {
/* forces light color-scheme */
color-scheme: light;
}
.dark {
/* forces dark color-scheme */
color-scheme: dark;
}
section {
width: 25%;
padding: 5px;
color: light-dark(
var(--light-code),
var(--dark-code)
);
border: 2px solid light-dark(var(--light-code), var(--dark-code));
}
@supports (background-image: light-dark(url("light.png"), url("dark.png"))) {
.supports {display:none;}
}
Firstly, we define light and dark linear-gradient() values as custom properties.
:root {
/* light dark gradients */
--light-grad: linear-gradient(135deg, var(--light-bg) 20%, var(--light-code));
--dark-grad: linear-gradient(45deg, var(--dark-bg) 30%, var(--dark-code));
}
section div {
width: 80%;
aspect-ratio: 1/1;
margin: auto;
border: 1px solid light-dark(var(--light-code), var(--dark-code));
}
@supports not (
background-image: light-dark(url("light.png"), url("dark.png"))
) {
section div {
width: 60%;
}
}
Then we use the custom properties with light-dark() to set the background-image property based on the active color scheme.
section div {
background-image: light-dark(
var(--light-grad),
var(--dark-grad)
);
}
| Specification |
|---|
| CSS Color Module Level 5 # light-dark |
color-scheme<color><image>prefers-contrast @media featurecontrast()var()This page was last modified on Apr 18, 2026 by MDN contributors.
-webkit-border-before-webkit-box-reflect-webkit-mask-box-image-webkit-mask-composite-webkit-mask-position-x-webkit-mask-position-y-webkit-mask-repeat-x-webkit-mask-repeat-y-webkit-tap-highlight-color-webkit-text-fill-color-webkit-text-security-webkit-text-stroke-webkit-text-stroke-color-webkit-text-stroke-width-webkit-touch-calloutCustom properties (--*): CSS variablesaccent-coloralignment-baselineallanchor-nameanchor-scopeanimationappearanceaspect-ratiobackdrop-filterbackface-visibilitybackgroundbaseline-shiftbaseline-sourceblock-sizeborder-blockborder-block-colorborder-block-endborder-block-end-colorborder-block-end-styleborder-block-end-widthborder-block-startborder-block-start-colorborder-block-start-styleborder-block-start-widthborder-block-styleborder-block-widthborder-bottomborder-bottom-colorborder-bottom-left-radiusborder-bottom-right-radiusborder-bottom-styleborder-bottom-widthborder-collapseborder-colorborder-end-end-radiusborder-end-start-radiusborder-imageborder-image-outsetborder-image-repeatborder-image-sliceborder-image-sourceborder-image-widthborder-inlineborder-inline-colorborder-inline-endborder-inline-end-colorborder-inline-end-styleborder-inline-end-widthborder-inline-startborder-inline-start-colorborder-inline-start-styleborder-inline-start-widthborder-inline-styleborder-inline-widthborder-leftborder-left-colorborder-left-styleborder-left-widthborder-radiusborder-rightborder-right-colorborder-right-styleborder-right-widthborder-shapeborder-spacingborder-start-end-radiusborder-start-start-radiusborder-styleborder-topborder-top-colorborder-top-left-radiusborder-top-right-radiusborder-top-styleborder-top-widthborder-widthborderbottomcaption-sidecaretclearclip-pathclip-ruleclipcolorcolumnscontaincontainer-namecontainer-typecontainercontent-visibilitycontentcorner-block-end-shapecorner-block-start-shapecorner-bottom-left-shapecorner-bottom-right-shapecorner-bottom-shapecorner-end-end-shapecorner-end-start-shapecorner-inline-end-shapecorner-inline-start-shapecorner-left-shapecorner-right-shapecorner-shapecorner-start-end-shapecorner-start-start-shapecorner-top-left-shapecorner-top-right-shapecorner-top-shapecursorcxcyddirectiondisplaydominant-baselinedynamic-range-limitempty-cellsfield-sizingfill-opacityfill-rulefillfilterflexfloatflood-colorflood-opacityfont-familyfont-feature-settingsfont-kerningfont-language-overridefont-optical-sizingfont-palettefont-sizefont-size-adjustfont-smoothfont-stretchfont-stylefont-synthesisfont-synthesis-positionfont-synthesis-small-capsfont-synthesis-stylefont-synthesis-weightfont-variantfont-variant-alternatesfont-variant-capsfont-variant-east-asianfont-variant-emojifont-variant-ligaturesfont-variant-numericfont-variant-positionfont-variation-settingsfont-weightfont-widthfontforced-color-adjustframe-sizinggapgridhanging-punctuationheighthyphenate-characterhyphenate-limit-charshyphensinitial-letterinline-sizeinsetinteractivityinterpolate-sizeisolationleftletter-spacinglighting-colorlink-parametersmarginmarkermaskmix-blend-modeoffsetopacityorderorphansoutlineoverflowoverlaypaddingpagepaint-orderpath-lengthperspective-originperspectivepointer-eventspositionprint-color-adjustquotesrreading-flowreading-orderresizerightrotaterulerxryscalescroll-behaviorscroll-initial-targetscroll-marginscroll-margin-blockscroll-margin-block-endscroll-margin-block-startscroll-margin-bottomscroll-margin-inlinescroll-margin-inline-endscroll-margin-inline-startscroll-margin-leftscroll-margin-rightscroll-margin-topscroll-marker-groupscroll-paddingscroll-padding-blockscroll-padding-block-endscroll-padding-block-startscroll-padding-bottomscroll-padding-inlinescroll-padding-inline-endscroll-padding-inline-startscroll-padding-leftscroll-padding-rightscroll-padding-topscroll-snap-alignscroll-snap-stopscroll-snap-typescroll-target-groupscroll-timelinescroll-timeline-axisscroll-timeline-namespeak-asstop-colorstop-opacitystroketab-sizetable-layouttext-aligntext-align-lasttext-anchortext-autospacetext-boxtext-box-edgetext-box-trimtext-combine-uprighttext-decorationtext-decoration-colortext-decoration-insettext-decoration-linetext-decoration-skiptext-decoration-skip-inktext-decoration-styletext-decoration-thicknesstext-emphasistext-emphasis-colortext-emphasis-positiontext-emphasis-styletext-indenttext-justifytext-orientationtext-overflowtext-renderingtext-shadowtext-size-adjusttext-spacing-trimtext-transformtext-underline-offsettext-underline-positiontext-wraptext-wrap-modetext-wrap-styletimeline-scopetoptouch-actiontransformtransitiontranslateunicode-bidiuser-modifyuser-selectvector-effectvertical-alignvisibilitywhite-spacewhite-space-collapsewidowswidthwill-changeword-breakword-spacingwriting-modexyz-indexzoom:active-view-transition:active-view-transition-type():active:any-link:autofill:blank:buffering:checked:current:default:defined:dir():disabled:empty:enabled:first-child:first-of-type:first:focus-visible:focus-within:focus:fullscreen:future:has-slotted:has():heading:heading():host-context():host:host():hover:in-range:indeterminate:interest-source:interest-target:invalid:is():lang():last-child:last-of-type:left:link:local-link:modal:muted:not():only-child:only-of-type:open:optional:out-of-range:past:paused:picture-in-picture:placeholder-shown:playing:popover-open:read-only:read-write:required:right:root:scope:seeking:stalled:state():target:user-invalid:user-valid:valid:visited:volume-locked:where():xr-overlay::-webkit-inner-spin-button::-webkit-meter-bar::-webkit-meter-even-less-good-value::-webkit-meter-inner-element::-webkit-meter-optimum-value::-webkit-meter-suboptimum-value::-webkit-progress-bar::-webkit-progress-inner-element::-webkit-progress-value::-webkit-scrollbar::-webkit-search-cancel-button::-webkit-search-results-button::-webkit-slider-runnable-track::-webkit-slider-thumb::after::backdrop::before::checkmark::column::cue::details-content::file-selector-button::first-letter::first-line::grammar-error::highlight()::marker::part()::picker-icon::picker()::placeholder::search-text::selection::slotted()::spelling-error::target-text@charset@color-profile@container@counter-style@custom-media@document@font-face@font-feature-values@font-palette-values@function@import@keyframes@layer@media-moz-device-pixel-ratio-webkit-animation-webkit-device-pixel-ratio-webkit-transform-2d-webkit-transform-3d-webkit-transitionany-hoverany-pointeraspect-ratiocolorcolor-gamutcolor-indexdevice-aspect-ratiodevice-heightdevice-posturedevice-widthdisplay-modedynamic-rangeforced-colorsgridheighthorizontal-viewport-segmentshoverinverted-colorsmonochromeorientationoverflow-blockoverflow-inlinepointerprefers-color-schemeprefers-contrastprefers-reduced-dataprefers-reduced-motionprefers-reduced-transparencyresolutionscanscriptingshapeupdatevertical-viewport-segmentsvideo-dynamic-rangewidth@namespace@page@position-try@property@scope@starting-style@supports@view-transition<absolute-size><alpha-value><angle-percentage><angle><axis><baseline-position><basic-shape><blend-mode><box-edge><calc-keyword><calc-sum><color-interpolation-method><color><content-distribution><content-position><corner-shape-value><custom-ident><dashed-function><dashed-ident><dimension><display-box><display-inside><display-internal><display-legacy><display-listitem><display-outside><easing-function><filter-function><flex><frequency-percentage><frequency><generic-family><gradient><hex-color><hue-interpolation-method><hue><ident><image><integer><length-percentage><length><line-style><line-width><named-color><number><overflow-position><overflow><percentage><position-area><position><ratio><relative-size><resolution><rule-list><self-position><shape><string><system-color><text-edge><time-percentage><time><timeline-range-name><transform-function><url>-moz-image-rect()abs()acos()alpha()anchor-size()anchor()asin()atan()atan2()attr()blur()brightness()calc-size()calc()circle()clamp()color-mix()color()conic-gradient()contrast-color()contrast()cos()counter()counters()cross-fade()cubic-bezier()device-cmyk()drop-shadow()dynamic-range-limit-mix()element()ellipse()env()exp()fit-content()grayscale()hsl()hue-rotate()hwb()hypot()if()image-set()image()inset()invert()lab()lch()light-dark()linear-gradient()linear()log()matrix()matrix3d()max()min()minmax()mod()oklab()oklch()opacity()paint()param()path()perspective()polygon()pow()progress()radial-gradient()random()ray()rect()rem()repeat()repeating-conic-gradient()repeating-linear-gradient()repeating-radial-gradient()rgb()rotate()rotate3d()rotateX()rotateY()rotateZ()round()saturate()scale()scale3d()scaleX()scaleY()scaleZ()sepia()shape()sibling-count()sibling-index()sign()sin()skew()skewX()skewY()sqrt()steps()superellipse()symbols()tan()translate()translate3d()translateX()translateY()translateZ()type()url()var()xywh()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |