Sub topic 9
Keeping styles consistent with custom properties
This chapter assumes you know basic HTML elements, CSS selectors and properties, and how font-size, font-weight, and color work. Earlier chapters built a type scale and styled headings, body text, captions, and labels. Here you move those values into one place so the hierarchy stays consistent as the page grows.
The problem: values scattered through a stylesheet
Suppose the caption color #595959 appears in six rules, and a designer asks for a slightly darker gray. You must find every copy and change it. Miss one and two captions on the same page no longer match.
A type scale only works if every rule uses the same sizes. Writing 1.25rem by hand in many places invites small drift, such as 1.2rem in one rule and 1.3rem in another.
Custom properties as type tokens
A CSS custom property is a named value that you define yourself. Its name starts with two dashes, and you read it back with var(). A design token is a custom property that stores one design decision, such as "the size of an h2".
Define tokens on :root, which matches the html element. Every element inherits custom properties from its ancestors, so tokens defined there are available on the whole page.
:root {
--text-sm: 0.875rem;
--text-base: 1rem;
--text-lg: 1.25rem;
--text-xl: 1.5rem;
--text-2xl: 2rem;
--leading-body: 1.5;
--leading-heading: 1.2;
--color-text: #1a1a1a;
--color-text-muted: #595959;
}
body {
font-size: var(--text-base);
line-height: var(--leading-body);
color: var(--color-text);
}
h1 { font-size: var(--text-2xl); line-height: var(--leading-heading); }
h2 { font-size: var(--text-xl); line-height: var(--leading-heading); }
h3 { font-size: var(--text-lg); line-height: var(--leading-heading); }
figcaption {
font-size: var(--text-sm);
color: var(--color-text-muted);
}
label {
display: block;
font-size: var(--text-base);
font-weight: 600;
margin-bottom: 0.25rem;
}Now the caption color lives in one line. Change --color-text-muted and every secondary text element updates together.
Name tokens by role or by size
Tokens in this chapter use two naming styles, and each has a purpose.
- Size names such as
--text-lgdescribe a step on the scale. They suit the scale itself. - Role names such as
--color-text-muteddescribe what the value is for. They survive redesigns, because a muted color can change from gray to blue-gray without the name becoming wrong.
Avoid names that encode the value, like --gray-595959. If the value changes, the name lies.
Fallback values and overrides
var() accepts a fallback as its second argument. The browser uses it if the property is not defined.
.note {
font-size: var(--text-sm, 0.875rem);
}Use fallbacks sparingly. If you define every token on :root, they add noise. They help most in components that may be copied into a page that lacks your tokens.
Custom properties follow the cascade, so you can override one inside a specific element. All descendants then pick up the new value.
.sidebar {
--text-base: 0.9375rem;
--color-text-muted: #4d4d4d;
}
.sidebar p {
font-size: var(--text-base);
}Only elements inside .sidebar change. The rest of the page keeps the root values. This is useful for dark panels, where you might override the text and muted colors, but do it deliberately. Each override is a place where the hierarchy can drift.
Common mistakes
- Using
pxin the tokens. Pixel values ignore the user's browser font-size setting. Keepremfor sizes, as in the scale above. - Misspelling a token name.
var(--text-bsae)does not raise an error. The declaration becomes invalid at computed-value time, and the property falls back to its inherited or initial value. If a size looks wrong, check the name in your browser's developer tools. - Defining too many tokens. A token for every one-off value defeats the purpose. Add a token when a value is used in several rules or expresses a real design decision.
- Defining tokens on a narrow selector. A token defined on
.cardis not available outside.card. Put shared tokens on:root.
How the pieces connect
The tokens do not change how the page looks by themselves. They make the choices you already made easy to keep and easy to change.
Exercise
A stylesheet sets figcaption { font-size: var(--text-sm); color: var(--color-text-muted); }, but you notice captions inside a .gallery element are larger than elsewhere. Which kind of rule would you search for in the stylesheet, and why?
Show answer
Search for a rule that redefines --text-sm (or sets font-size on figcaption) inside .gallery. Custom properties follow the cascade, so an override on .gallery changes the value for every descendant that reads --text-sm. Removing or correcting that override restores the shared value.
Exercise
Rewrite this rule so it uses tokens from this chapter instead of fixed values: p { font-size: 1rem; line-height: 1.5; color: #1a1a1a; }
Show answer
p { font-size: var(--text-base); line-height: var(--leading-body); color: var(--color-text); }