Sub topic 3
Storing Colors in Custom Properties
This chapter assumes you know CSS selectors and the cascade, and that you can read hex and rgb color values. It also assumes you have seen the prefers-color-scheme media query in the previous chapter. Here you move the color values out of your rules and into named variables, so a dark theme only has to change a few lines.
What a custom property is
A custom property is a CSS variable whose name you choose. The name starts with two dashes, such as --bg. You read its value with the var() function.
:root {
--bg: #ffffff;
--text: #1a1a1a;
--link: #0b5fff;
}
body {
background: var(--bg);
color: var(--text);
}
a {
color: var(--link);
}:root matches the html element. Properties defined there are available to every element on the page. Browsers do not interpret the value of a custom property when you declare it. They store it as text and substitute it wherever var() appears.
Why this helps dark mode
Without variables, a dark theme means repeating every color rule inside a media query. With variables, the rules stay the same and only the values change.
:root {
--bg: #ffffff;
--text: #1a1a1a;
--link: #0b5fff;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #e6e6e6;
--link: #8ab4ff;
}
}The body and a rules from the first example are unchanged. When the system is set to dark, the second :root block wins, because it comes later and has the same specificity. Every var() then resolves to the dark value.
How inheritance and scope work
Custom properties inherit, like color or font-family. An element uses the value defined on itself, or else the nearest ancestor that defines it.
flowchart TDThe diagram below shows how one override reaches only part of the page.
This is useful for sections that should keep one theme, such as a code block that is always dark:
.always-dark {
--bg: #121212;
--text: #e6e6e6;
background: var(--bg);
color: var(--text);
}Fallback values
var() accepts a second argument that is used when the property is not defined.
.badge {
color: var(--badge-text, #333333);
}Use fallbacks for optional properties. Do not add them to every theme color. A fallback can hide a misspelled name, and the page then looks right in one theme and wrong in the other.
Common errors
- Declaring on the wrong element. If you define
--bgon.cardand read it onbody, the value is not found. Properties flow down the tree, never up. Define theme colors on:root. - Misspelled names. Custom property names are case-sensitive, so
--Bgand--bgare different properties. An undefined variable with no fallback makes the declaration invalid at computed-value time. The property then falls back to its inherited or initial value, with no error message. - Using
var()in a media query.@media (min-width: var(--bp))does not work. Variables are not allowed in media conditions. Put the media query around the rule that sets the variable, as in the dark example above. - Overriding in the wrong order. The dark block must come after the light defaults, or it must be more specific. If it comes first, the light defaults win.
- Changing only some values. If you override
--bgand forget--text, you get dark text on a dark background. Keep the light and dark lists in the same order so you can compare them.
Exercise
A page defines --bg: white on :root and sets body { background: var(--bag); }. The page background is transparent instead of white. Why, and what is the fix?
Show answer
--bag is a misspelling of --bg. Because no property named --bag exists and there is no fallback, the background declaration is invalid at computed-value time and falls back to its initial value, which is transparent. The browser shows no error. Fix the name to var(--bg).
Exercise
Write a rule that keeps a .banner element dark in both themes, using the properties --bg and --text.
Show answer
Redefine the properties on the element itself, then use them:
.banner {
--bg: #121212;
--text: #e6e6e6;
background: var(--bg);
color: var(--text);
}
Children of .banner inherit these values, whatever the page theme is.