Sub topic 5
Telling the browser about your themes with color-scheme
This chapter assumes you know CSS custom properties, the prefers-color-scheme media query, and the idea of semantic color tokens from earlier chapters. You should also be comfortable reading basic HTML.
The problem custom properties do not solve
Custom properties let you restyle your own elements. They do nothing for the parts of the page the browser draws itself. Form controls, scrollbars, the default page background, and the default text color all come from the browser's built-in stylesheet. If you switch your tokens to dark colors and leave those defaults alone, you get a dark page with a bright white scrollbar and a glaring <select> menu.
The color-scheme property fixes this. It tells the browser which color schemes your page supports, so the browser can render its own UI in a matching scheme.
Declaring supported schemes
Set the property on the root element:
:root {
color-scheme: light dark;
}The value is a list of schemes. Here are the common ones.
| Value | Meaning |
|---|---|
| `normal` | No scheme declared. The page is treated as light. |
| `light` | The page supports only light. |
| `dark` | The page supports only dark. |
| `light dark` | The page supports both. The browser follows the user's system setting. |
With light dark, the browser picks the scheme that matches the operating system. In a dark scheme, it changes these things:
- Form controls, such as inputs, buttons, and select menus, get dark styling.
- Scrollbars become dark.
- The default canvas background becomes dark, and default text becomes light.
The list order is a preference order. If the system setting matches none of the listed schemes, the browser uses the first one.
Using the meta tag
CSS is not available until the stylesheet loads. Until then, the browser may paint a white page. You can declare the schemes in HTML so the browser knows from the start:
<meta name="color-scheme" content="light dark">Put it in the <head>, before any stylesheet link. This reduces the white flash while a dark-mode page is loading. Keep the meta tag and the CSS property in agreement. If they differ, the CSS wins once it applies.
Combining it with your tokens
The property works alongside the tokens you built earlier. Your custom properties set your colors, and color-scheme sets the browser's colors.
:root {
color-scheme: light dark;
--bg: #ffffff;
--text: #1a1a1a;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #e6e6e6;
}
}
body {
background: var(--bg);
color: var(--text);
}Now the page body follows your tokens, and the scrollbar and form controls follow the browser's dark scheme. Both switch together when the system setting changes.
Forcing one scheme on part of a page
You can set color-scheme on any element, not only the root. This is useful for a dark code panel inside a light page:
.code-panel {
color-scheme: dark;
background: #1e1e1e;
color: #e6e6e6;
}Form controls inside .code-panel now render dark, even when the rest of the page is light.
Common errors
- Forgetting the property. The page looks right, but the scrollbar and inputs stay light. Add
color-scheme: light darkto:root. - Declaring
darkonly and expecting a light theme. A value ofdarklocks the browser UI to dark. Uselight darkif the page supports both. - Setting it only inside the media query. The browser then treats the page as light-only for users on the light setting, and the declaration is not available to other features. Declare it once at the top level.
- Assuming it recolors your elements. It changes only the browser's defaults. A
divwith a hard-coded white background stays white.
Exercise
A page defines dark custom properties inside a prefers-color-scheme: dark media query. In dark mode, the text and background are correct, but the scrollbar and a <select> menu are still bright white. What single declaration fixes this, and where should it go?
Show answer
Add color-scheme: light dark; to the :root rule, outside the media query. It tells the browser the page supports both schemes, so the browser renders its own UI, such as scrollbars and form controls, in the scheme that matches the system setting.
Exercise
You have a light page with a code block that should always look dark, including any form controls inside it. How do you set that up?
Show answer
Set color-scheme: dark; on the code block's selector, along with its own dark background and text colors. The property applies to that element and its descendants, so controls inside it render in the dark scheme.