Sub topic 8
Keep your design consistent with custom properties
This chapter assumes you can write CSS rules with selectors and declarations, and that you have set fonts and colors on text. You will replace repeated values with named ones, so a change in one place updates the whole page.
Why repeat values less
Suppose the accent color #0b5cad appears in link rules, buttons and borders. To change it, you must find every copy. Miss one and the page looks inconsistent.
A custom property is a named value that you define in CSS and reuse. Its name starts with two hyphens, such as --color-accent. You read it back with the var() function. Custom properties are also called CSS variables.
Define and use properties
Define properties inside a rule. The :root selector matches the html element, so properties defined there are available to every element on the page.
:root {
--color-text: #1f2933;
--color-bg: #ffffff;
--color-accent: #0b5cad;
--space: 1rem;
}
body {
margin: 0;
font-family: system-ui, sans-serif;
line-height: 1.6;
color: var(--color-text);
background: var(--color-bg);
}
a {
color: var(--color-accent);
}Custom properties inherit, like color and font-family. A property defined on :root is therefore visible in every descendant.
You can also combine a property with arithmetic. Inside calc(), var(--space) works like a number with a unit:
main {
max-width: 60rem;
margin: 0 auto;
padding: calc(var(--space) * 2) var(--space);
}Here the top and bottom padding is twice the base space, and the side padding equals it. If you change --space to 1.25rem, the whole spacing scale changes with it.
Choose names by role
Name a property for its job, not its value. --color-accent stays correct if the color changes from blue to green. --blue would then be misleading.
| Poor name | Better name | Reason |
|---|---|---|
| `--blue` | `--color-accent` | Describes the role, not the hue |
| `--16px` | `--space` | Survives a change of size |
| `--c1` | `--color-text` | Readable without a lookup |
Provide a fallback
var() accepts a second argument, used when the property is not defined:
.note {
border-left: 4px solid var(--color-note, #b45309);
}If --color-note does not exist, the border uses #b45309. A fallback is useful for optional values, but do not add one to every call. A missing core property usually signals a typo you want to notice.
Override a property in a smaller scope
A property can be redefined on any selector. Elements inside that selector use the new value, and the rest of the page is unchanged.
footer {
--color-text: #52606d;
--color-accent: #084a8c;
}Links and text inside the footer now use the footer's values. This is the main difference from a preprocessor variable, which is fixed when the stylesheet is built. A custom property is resolved by the browser, so it follows the cascade and inheritance you met earlier.
Common errors
- Typing one hyphen.
-color-accentis not a custom property. You need two hyphens. - Wrong case. Names are case-sensitive, so
--Color-Accentand--color-accentare different properties. - Defining on a narrow selector. A property defined on
.cardis not available infooter. Define shared values on:root. - Using a property as a name.
var()can supply a value only. You cannot writevar(--side): 1rem. - Putting
var()in a media query condition.@media (min-width: var(--bp))does not work. Write the actual value there.
Exercise
A page defines --color-accent: #0b5cad; on .card only. A link in the footer uses color: var(--color-accent);. What color does the link get, and how do you fix it so the footer link uses the accent color?
Show answer
The link does not receive the accent color. .card is not an ancestor of the footer, so the property is undefined there and the declaration is invalid at computed-value time. The link then falls back to its inherited color. To fix it, define --color-accent on :root so every element inherits it.
Exercise
Rewrite padding: 2rem 1rem; using a --space: 1rem; property so that the vertical padding is double the horizontal padding.
Show answer
padding: calc(var(--space) * 2) var(--space);