Light and Dark Themes: color-scheme, light-dark(), System Colors and accent-color
Light and dark themes in CSS: the color-scheme property and meta tag, prefers-color-scheme, light-dark() for colors and images, theme toggles that override the system, system colors like Canvas and CanvasText, accent-color, and good dark palettes.
kvi5hnuCode Sprout team member9 min readOn this page
Most operating systems let people choose a light or a dark appearance, and many expect websites to follow it. CSS has several pieces that work together for this: color-scheme tells the browser which schemes the page supports, prefers-color-scheme detects the user's choice, light-dark() picks a color for the active scheme, and system colors give you the browser's own colors for that scheme.
color-scheme: telling the browser
Without any CSS, a page is light: white background, black text, light form controls and scrollbars. Even if your CSS paints a dark background, inputs, checkboxes and scrollbars stay light, unless you declare that the page supports dark:
:root { color-scheme: light dark; }
| Value | Meaning |
|---|---|
normal (initial) | the page knows nothing about schemes: it's rendered light, unless the meta tag declares schemes |
light | the page supports light only |
dark | dark only |
light dark | both: use the user's preference |
only light | light, and the browser must not auto-darken the page |
With light dark, the browser renders the canvas (the page background), default text, form controls, scrollbars and spelling underlines in the user's scheme, and resolves light-dark() and system colors to match. color-scheme inherits, so it can also be set on part of a page: .code-sample { color-scheme: dark; } gives that section dark controls and scrollbars.

The middle panel is the common mistake: painting the page dark yourself leaves every native control, and the default link color, in light mode.
Also add the meta tag, so the browser knows before your CSS loads, which avoids a white flash when a dark page loads:
<meta name="color-scheme" content="light dark">
prefers-color-scheme
The media query detects the user's setting:
:root { --bg: #fffdf8; --text: #2b2118; --accent: #8a3b12; }
@media (prefers-color-scheme: dark) {
:root { --bg: #1b1714; --text: #ece4d8; --accent: #f0a46c; }
}
body { background: var(--bg); color: var(--text); }
a { color: var(--accent); }
That's the classic approach: themes as sets of custom properties (article 27), swapped by a media query. It works everywhere.
light-dark()
light-dark(light-value, dark-value) picks its first argument when the element's used color scheme is light and its second when it's dark. It keeps both values in one place:
:root {
color-scheme: light dark;
--bg: light-dark(#fffdf8, #1b1714);
--text: light-dark(#2b2118, #ece4d8);
--accent: light-dark(#8a3b12, #f0a46c);
}
- It needs a declared color scheme, from the
color-schemeproperty or the meta tag. Without either, the scheme is light, solight-dark()always picks the first value. - It follows
color-scheme, not the media query directly. That makes theme toggles simple, as shown below. - It works in any property that takes a color. Since September 2026, it also accepts images:
background-image: light-dark(url(day.svg), url(night.svg)). - An inherited
coloris resolved on the element that declared it. If a dark section declarescolor: light-dark(...), a light child inherits the dark result. Declarelight-dark()colors on the elements that need them, or in custom properties, which resolve where they're used.
light-dark() is Baseline widely available since November 2026.
.theme { color-scheme: light; }
.theme.dark { color-scheme: dark; }
.card {
background: light-dark(#fffdf8, #24201c);
color: light-dark(#2b2118, #ece4d8);
border: 1px solid light-dark(#e3d9c9, #3b342d);
border-radius: 10px; padding: 1rem; font-family: system-ui, sans-serif;
}
.card h3 { margin: 0 0 0.5rem; color: light-dark(#8a3b12, #f0a46c); }
body { display: grid; grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr)); gap: 1rem; margin: 1rem; }
input[placeholder] { width: 5rem; }
The two cards share every rule. Only color-scheme differs, and the form controls switch too.
A theme toggle
Many sites offer a switch with three options: light, dark, and "system". With light-dark(), the toggle only needs to change color-scheme:
:root { color-scheme: light dark; } /* system: follow the user's setting */
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }
const saved = localStorage.getItem("theme"); // "light", "dark" or null
if (saved) document.documentElement.dataset.theme = saved;
function setTheme(choice) {
if (choice === "system") {
delete document.documentElement.dataset.theme;
localStorage.removeItem("theme");
} else {
document.documentElement.dataset.theme = choice;
localStorage.setItem("theme", choice);
}
}
Run the first lines in a script in the <head>, before the page renders, so the saved theme applies without a flash. Without light-dark(), you'd need the custom-property swap twice: once in the media query, and again for the data-theme attribute.
A CSS-only toggle is also possible with :has(): :root:has(#dark-mode:checked) { color-scheme: dark; }. It doesn't remember the choice between visits, though.
System colors
System colors are keywords for the browser's own palette. They follow the used color scheme automatically, and the user's settings in forced colors mode (article 35):
| Keyword | Used for |
|---|---|
Canvas, CanvasText | page background and text |
LinkText, VisitedText, ActiveText | links |
ButtonFace, ButtonText, ButtonBorder | buttons |
Field, FieldText | text inputs |
Highlight, HighlightText | selected text |
SelectedItem, SelectedItemText | selected items in lists and menus |
Mark, MarkText | <mark> and search highlights |
GrayText | disabled text |
AccentColor, AccentColorText | the user's or browser's accent color |
.dialog { background: Canvas; color: CanvasText; border: 1px solid GrayText; }

The same keywords give different colors in each scheme, so a component built with them needs no dark-mode rules. accent-color changes only the highlight parts of the controls.
They're useful for components that should look native, and essential in forced colors mode. Older keywords such as ButtonHighlight and ThreeDShadow are deprecated aliases. AccentColor and AccentColorText reached all desktop browsers only in June 2026 (Chrome 149), and Chrome for Android doesn't support them yet. ButtonBorder, Mark and MarkText aren't in Safari yet.
accent-color
accent-color sets the highlight color of checkboxes, radio buttons, range sliders and progress bars, without rebuilding them:
:root { accent-color: #8a3b12; }
@media (prefers-color-scheme: dark) { :root { accent-color: #f0a46c; } }
The browser keeps the controls accessible: it picks a contrasting checkmark color, and may adjust the accent if it would be unreadable. Safari supported only part of this until version 26.2. Article 102 covers styling form controls further.
Designing a dark theme
A dark theme isn't an inverted light theme. Some guidelines:
- Avoid pure black and pure white. A very dark gray background, such as
oklch(20% 0.01 50), with off-white text, is easier on the eyes and leaves room to show elevation. - Show elevation with lighter surfaces, not shadows: cards and menus are slightly lighter than the page, because shadows are hard to see on dark backgrounds.
- Lower the chroma of accents and raise their lightness: vivid saturated colors vibrate on dark backgrounds.
oklch()(article 32) makes this easy: keep the hue, change L and C. - Check contrast in both themes (article 35). A brand color that works on white is often too dark on black.
- Images: offer different images with
light-dark()or<picture>with<source media="(prefers-color-scheme: dark)">, or dim photos a little:img { filter: brightness(0.9); }in the dark theme. - Use two tiers of tokens. Keep raw palette steps, such as
--gray-90, and map them to role tokens, such as--surface: var(--gray-90). A dark theme then swaps only the role tokens. - Don't force dark on users who chose light, and test both themes on every component.
Some browsers can automatically darken pages that don't declare a dark scheme (for example Chrome's "Auto Dark Mode" setting). Declaring color-scheme: light dark and providing real dark styles, or only light if the page must stay light, keeps you in control.
What's current
color-scheme, the meta tag andprefers-color-schemework everywhere.light-dark()for colors is Baseline widely available since November 2026. Image values inlight-dark()became Baseline in September 2026.accent-coloris in all browsers, with full support in Safari since 26.2.AccentColorandAccentColorTextare in all desktop browsers since Chrome 149 (June 2026), but not yet Baseline: Chrome for Android lacks them.
Common questions
Why does light-dark() always give me the light color?
The element's color-scheme is normal or light, and no meta tag declares a dark scheme. Set color-scheme: light dark on :root, or dark on the part of the page that should be dark.
My page is dark, but inputs and scrollbars are still white. Why?
The browser doesn't know the page is dark. Add color-scheme: dark (or light dark) so it renders controls and scrollbars in the dark scheme.
Do I still need prefers-color-scheme if I use light-dark()?
Not for colors. You may still need it for things light-dark() can't switch, such as different shadows, filters or font weights.
How do I stop a white flash when my dark page loads?
Add <meta name="color-scheme" content="light dark">, and set a saved theme in a script in the <head>, before the body renders.
Are system colors safe to use in a design? They're safe and accessible, but they look different in each browser and operating system. Use them where a native look is wanted, and for forced colors mode.
Summary
color-scheme: light darktells the browser the page supports both schemes. It changes the canvas, controls and scrollbars, and enableslight-dark().- The meta tag applies the scheme before CSS loads.
only lightprevents automatic darkening. prefers-color-schemedetects the user's preference, typically to swap custom properties.light-dark(a, b)picks a value by the used color scheme, for colors and now images. A theme toggle only needs to changecolor-scheme.- System colors like
CanvasandCanvasTextfollow the scheme and forced colors. accent-colorthemes checkboxes, radios, ranges and progress bars.- Good dark themes use dark grays, lighter surfaces for elevation, softer accents and checked contrast.
Practice questions
Write CSS that makes the page follow the user's light or dark preference, with a background of #fffdf8 or #1b1714 and text of #2b2118 or #ece4d8.
Answer
:root {
color-scheme: light dark;
background: light-dark(#fffdf8, #1b1714);
color: light-dark(#2b2118, #ece4d8);
}
A code sample block on a light page should have a dark background with dark scrollbars. Which property makes the scrollbars dark?
Answer
color-scheme: dark on the block. Setting a dark background color alone doesn't change scrollbars or form controls.
Your toggle sets data-theme="dark" on <html>, and the CSS uses light-dark() everywhere. What's the only rule the toggle needs?
Answer
:root[data-theme="dark"] { color-scheme: dark; }
Every light-dark() then picks its dark value, whatever the operating system setting.
With the rules below, what color is the text of .note, and why?
.dark-panel {
color-scheme: dark;
color: light-dark(black, white);
}
.dark-panel .note {
color-scheme: light; /* no color of its own */
}
Answer
White. light-dark() is resolved on .dark-panel, where the scheme is dark, and .note inherits that computed white. Give .note its own color: light-dark(black, white) to pick the light value.
Previous: Mixing and Deriving Colors: color-mix() and Relative Color Syntax
Next: Color Contrast and Accessibility: WCAG Ratios, contrast-color() and Forced Colors

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.