Favorites

light-dark() CSS Function

light-dark()
CSS FunctionThis function is supported by all modern browsers.
Updated 21st November 2026

The light-dark() function returns one of two colors, depending on whether the element uses a light or a dark color scheme.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

Description

The light-dark() CSS function returns one of two colors depending on whether the element is being displayed with a light or a dark color scheme. It takes two arguments: the first color is used for a light scheme and the second for a dark scheme. This makes it easy to support both light and dark themes from a single declaration, without writing separate @media (prefers-color-scheme: dark) blocks for every color.

For light-dark() to work, the element must have a color-scheme that allows both modes, usually set once on the root with color-scheme: light dark. The browser then follows the user's operating system or browser preference. If color-scheme is set to only light or only dark on part of the page, light-dark() inside that part always picks the matching color, which is a convenient way to force a section into one theme.

:root {
  color-scheme: light dark;
}

body {
  background-color: light-dark(#ffffff, #0f172a);
  color: light-dark(#111827, #e5e7eb);
}

.card {
  border: 1px solid light-dark(#e5e7eb, #334155);
}

/* Force one panel to always use the dark colors */
.promo {
  color-scheme: dark;
}

light-dark() currently accepts only <color> values. It works well together with custom properties, so you can define a theme once, for example --surface: light-dark(white, black);, and reuse it across a stylesheet.

Interactive Demo

background-color: light-dark(#e7ecff, #1b1f3b) and color: light-dark(#1b1f3b, #ffd166).
Demo Code
HTML Code
<div class="demo-element">background-color: light-dark(#e7ecff, #1b1f3b) and color: light-dark(#1b1f3b, #ffd166).</div>
CSS Code
.demo-element {
	width: 260px;
	padding: 18px;
	border-radius: 8px;
	font-size: 16px;
	background-color: light-dark(#e7ecff, #1b1f3b);
	color: light-dark(#1b1f3b, #ffd166);
	border: 3px solid light-dark(#4361ee, #f72585);
	color-scheme: light;
}

Syntax

light-dark() = light-dark( <color>, <color> )

Values

  • light colorThe <color> used when the element's used color scheme is light (or unknown).
  • dark colorThe <color> used when the element's used color scheme is dark.

Example

<div class="wrap">
<h1>CSS light-dark() examples</h1>
<div class="grid">
<div class="card">
<div class="panel force-light">
<strong>Light scheme</strong><br>
<span class="button">Button</span>
</div>
<div class="label">color-scheme: light</div>
</div>
<div class="card">
<div class="panel force-dark">
<strong>Dark scheme</strong><br>
<span class="button">Button</span>
</div>
<div class="label">color-scheme: dark</div>
</div>
<div class="card">
<div class="panel">
<strong>Follows your system</strong><br>
<span class="button">Button</span>
</div>
<div class="label">color-scheme: light dark</div>
</div>
</div>
<p class="note">All three panels use the same light-dark() declarations.</p>
</div>
/* Base demo layout */
* { box-sizing: border-box; }

body {
margin: 24px;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
background: #f5f7fb;
color: #102a43;
}

.wrap {
max-width: 860px;
margin: 0 auto;
}

h1 {
font-size: 20px;
margin: 0 0 16px;
}

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}

.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.label {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
font-size: 12px;
color: #486581;
margin-top: 10px;
word-break: break-word;
}

.note {
font-size: 13px;
color: #486581;
margin-top: 14px;
}

/* light-dark() examples */
:root {
color-scheme: light dark;
}

.panel {
border-radius: 12px;
padding: 20px;
background-color: light-dark(#ffffff, #0f172a);
color: light-dark(#0f172a, #e2e8f0);
border: 1px solid light-dark(#cbd5e1, #334155);
}

.panel .button {
display: inline-block;
margin-top: 10px;
padding: 8px 14px;
border-radius: 8px;
background-color: light-dark(#2563eb, #60a5fa);
color: light-dark(#ffffff, #0f172a);
font-weight: 600;
}

.force-light { color-scheme: light; }
.force-dark { color-scheme: dark; }
Open in editor

Browser Support

The following information will show you the current browser support for the CSS light-dark() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.