Sub topic 5
Telling the browser about your themes with color-scheme
This chapter assumes you can write CSS custom properties and a prefers-color-scheme media query, and that you know the cascade and inheritance. Earlier chapters built a theme from your own colors. Here you tell the browser which themes the page supports, so it can theme the parts of the page that you did not style.
The problem color-scheme solves
Suppose you switch your page to a dark background with custom properties. Several things stay light:
- Form controls such as
<input>,<select>,<textarea>, and<button>keep their light appearance. - Scrollbars stay light.
- The default page background (the canvas) and default text color stay white and black wherever you did not set a color.
- Built-in UI such as the date picker popup, the
<dialog>backdrop, and spellcheck underlines does not follow your theme.
You could restyle every one of these by hand, but many have no CSS hook at all. The color-scheme property fixes this. It declares which color schemes an element supports. The browser then picks a matching rendering for everything it draws itself.
Syntax and values
color-scheme takes a list of schemes, or normal.
| Value | Meaning |
|---|---|
| `normal` | The page makes no claim about color schemes. The browser renders the light scheme. |
| `light` | The element supports only the light scheme. |
| `dark` | The element supports only the dark scheme. |
| `light dark` | The element supports both. The browser uses the user's system preference. |
| `only light` | Like `light`, and also opts out of browser-forced darkening. |
The most common declaration is this:
:root {
color-scheme: light dark;
}With this rule alone, a user whose system is set to dark gets a dark canvas, light default text, dark form controls, and dark scrollbars. You have not written a single color or media query. A user whose system is set to light sees the light versions.
The property is inherited, so setting it on :root covers the whole document. You can also set it on any element, which is useful for a region that must stay in one scheme.
.code-sample {
color-scheme: dark;
background: Canvas;
color: CanvasText;
}The .code-sample block is always dark, even on a light page. Its own form controls and scrollbars are dark too.
How the browser decides which scheme is used
The browser computes a used color scheme for each element. It reads the color-scheme list and compares it with the user's preference, which comes from the operating system or a browser setting.
- If the list contains only
lightor onlydark, the browser uses that scheme. - If the list contains both, the browser uses the one that matches the user's preference.
- If the value is
normal, the browser uses light.
The used scheme controls what the browser renders, and it also controls the value of the CSS system colors. Canvas is the default background color and CanvasText is the default text color. They change with the used scheme, as do LinkText, ButtonFace, and Field. You can use them directly:
body {
background: Canvas;
color: CanvasText;
}With color-scheme: light dark on the root, this body is white with black text in a light system and near-black with near-white text in a dark system. The exact shades are chosen by the browser.
Using it with custom properties
color-scheme and your own tokens do different jobs. The property themes the browser's built-in parts. Your custom properties theme everything you design. Use both. This is the earlier media-query pattern with one line added:
:root {
color-scheme: light dark;
--bg: #ffffff;
--text: #1a1a1a;
--link: #0b5fff;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #e6e6e6;
--link: #8ab4ff;
}
}
body {
background: var(--bg);
color: var(--text);
}
a {
color: var(--link);
}The extra declaration is easy to forget, and nothing fails visibly in your own colors when it is missing. The failures show up in the form controls and scrollbars, which stay light on your dark background.
Here is a complete page you can save and open to see the difference:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>color-scheme demo</title>
<style>
:root {
color-scheme: light dark;
}
body {
font-family: system-ui, sans-serif;
max-width: 40rem;
margin: 2rem auto;
padding: 0 1rem;
}
.log {
height: 6rem;
overflow: auto;
}
</style>
</head>
<body>
<h1>color-scheme demo</h1>
<p>No colors are set in this stylesheet.</p>
<form>
<label>Name <input type="text" value="Ada"></label>
<select>
<option>One</option>
<option>Two</option>
</select>
<textarea rows="2">Notes</textarea>
<button type="button">Save</button>
</form>
<div class="log">
<p>Line 1</p><p>Line 2</p><p>Line 3</p><p>Line 4</p>
<p>Line 5</p><p>Line 6</p><p>Line 7</p><p>Line 8</p>
</div>
</body>
</html>Switch your system between light and dark. The page background, text, inputs, button, and the scrollbar of .log all follow. Delete the :root rule and reload. In a dark system, the page returns to white.
The meta tag
You can also declare schemes in HTML:
<meta name="color-scheme" content="light dark">This is equivalent to color-scheme: light dark on the root, with one practical advantage. The browser reads the <meta> tag while parsing the head, before your stylesheet has downloaded. A dark-system user then sees a dark canvas from the first paint. Without it, the browser may paint a white canvas until the CSS arrives, which produces a white flash on slow connections.
Put the tag early in <head>, ideally right after the charset and viewport tags. Keep the CSS property too. The CSS property is what you change at runtime, and it overrides the meta tag when both are present.
The only keyword and forced darkening
Some browsers offer an automatic dark mode that darkens pages which do not declare support for it. This is sometimes called forced darkening. Examples include Chrome's "Auto Dark Mode for Web Contents" flag and some mobile browsers. These tools invert or remap your colors, and the results are often poor.
Declaring color-scheme: light dark tells the browser that your page handles dark itself, so it should not interfere. If your page is light-only and you want to stop the browser from altering it, write:
:root {
color-scheme: only light;
}The only keyword opts out of forced darkening. Use it for a page you do not intend to theme. Do not use it on a page that offers a dark theme.
Connecting color-scheme to a manual toggle
A toggle that lets users choose light or dark must change color-scheme as well as your tokens. If you force a dark palette while color-scheme is still light dark and the system is light, your page is dark but the scrollbars and form controls are light.
The solution is to set color-scheme to match each forced theme:
:root {
color-scheme: light dark;
--bg: #ffffff;
--text: #1a1a1a;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--bg: #121212;
--text: #e6e6e6;
}
}
:root[data-theme="light"] {
color-scheme: light;
}
:root[data-theme="dark"] {
color-scheme: dark;
--bg: #121212;
--text: #e6e6e6;
}
body {
background: var(--bg);
color: var(--text);
}There are three states here:
- System: no
data-themeattribute.color-schemeislight darkand the media query decides the palette. - Forced light:
color-scheme: lightmakes the browser stay light even if the system is dark. The:not([data-theme="light"])selector stops the dark media query from applying. - Forced dark:
color-scheme: darkmakes the browser dark even if the system is light, and the palette is set explicitly.
The toggle chapter builds the button and the JavaScript. The point here is that each forced state needs its own color-scheme value.
Writing both schemes in one declaration
A color-scheme that includes both light and dark also enables the light-dark() function, which takes two colors and returns the first in a light scheme and the second in a dark scheme:
:root {
color-scheme: light dark;
}
body {
background: light-dark(#ffffff, #121212);
color: light-dark(#1a1a1a, #e6e6e6);
}light-dark() reads the used color scheme of the element it is applied to. If color-scheme is normal or missing, the function always returns the first color, so the dark colors never appear. The next chapter covers the function in detail.
Gotchas
Elements can differ from the root
Because color-scheme is inherited and can be set on any element, light-dark() and system colors evaluate per element. A .code-sample with color-scheme: dark gets dark values from light-dark() even on a light page. This is useful, but it surprises people who expect one global theme.
Iframes
An embedded page has its own used color scheme. If the iframe's scheme differs from the scheme of the element that contains it, the browser gives the iframe an opaque background, usually white or black. Transparent iframes then stop being transparent. If your embedded content is yours, give both documents the same color-scheme declaration.
Setting colors you did not need to set
Once color-scheme is correct, default text and background colors are already right. Avoid hard-coding background: white on body in a base stylesheet. It overrides the system canvas and brings back the light theme for dark users.
Your own components
color-scheme does not change colors you set yourself. A card with background: #fff stays white. The property themes only browser-drawn parts and the values of system colors and light-dark().
Accent colors
Checkboxes, radio buttons, and range sliders use the browser's accent color. You can set it with accent-color: var(--link) so these controls match your palette in both schemes.
Summary
- Set
color-scheme: light darkon:rootso the browser themes form controls, scrollbars, and default colors. - Add the matching
<meta name="color-scheme">tag early in the head to avoid a white flash before your CSS loads. - Set
color-schemetolightordarkwhen you force a theme with a toggle. - Use
only lightto opt out of forced darkening on a page you do not theme. - Remember that it does not change colors you wrote yourself.
Exercise
A page sets --bg: #121212 and --text: #e6e6e6 when [data-theme="dark"] is on the root, but it never sets color-scheme. A user whose system is set to light clicks the toggle to choose dark. The page background is dark. What looks wrong, and what one rule fixes it?
Show answer
The browser still uses the light scheme, so form controls, scrollbars, and any default colors stay light on the dark background. Add :root[data-theme="dark"] { color-scheme: dark; }. A matching :root[data-theme="light"] { color-scheme: light; } rule keeps the forced light state correct when the system is dark.
Exercise
Why does the light-dark() function return only its first color on a page that has no color-scheme declaration, even when the user's system is dark?
Show answer
Without a declaration, color-scheme is normal, so the used scheme is light. light-dark() picks its color from the used scheme of the element, so it always returns the first (light) color. Declaring color-scheme: light dark lets the system preference select the dark color.
Exercise
You embed your own dark-themed widget in an iframe on a light page. The widget has a transparent background, but it appears with a solid background. What causes this, and how can you avoid it?
Show answer
The iframe's used color scheme differs from the scheme of the element that contains it, so the browser gives the iframe an opaque canvas. Declare the same color-scheme (for example light dark) in both the host page and the embedded document so the schemes match.