Sub topic 8
Keep your design consistent with custom properties
CSS custom properties let you store a value once, such as a color or a spacing unit, and reuse it across the stylesheet. This chapter assumes you can write CSS rules with selectors and declarations, and that you know the cascade decides which rule wins. It also assumes you have seen colors and rem units in the typography chapter. By the end you will be able to define a small set of design values, scope them, override them, and avoid the usual mistakes.
What a custom property is
A custom property is a name that starts with two hyphens, such as --color-accent. You assign it a value like any other property. You read it back with the var() function.
:root {
--color-accent: #0b5cad;
}
a {
color: var(--color-accent);
}
button {
background: var(--color-accent);
}If you change the accent color, you edit one line instead of hunting for every occurrence. That is the main benefit: a single source of truth for each design decision.
Three details are worth knowing from the start:
- Names are case-sensitive.
--Color-Accentand--color-accentare two different properties. - A custom property can hold almost any value: a color, a length, a font stack, or a number.
- Custom properties inherit by default, like
colorandfont-family. A child element sees the value set on its parent.
Declare values on :root
:root matches the html element. Declaring properties there makes them available to every element on the page, because every element is a descendant of html. This is the usual place for site-wide values.
Here is a fuller version of the stylesheet used in this tutorial. It stores colors and spacing, then uses them in ordinary rules.
:root {
--color-text: #1f2933;
--color-bg: #ffffff;
--color-accent: #0b5cad;
--space: 1rem;
}
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
font-family: system-ui, sans-serif;
line-height: 1.6;
color: var(--color-text);
background: var(--color-bg);
}
main {
max-width: 60rem;
margin: 0 auto;
padding: calc(var(--space) * 2) var(--space);
}
a {
color: var(--color-accent);
}Look at the main rule. The padding is calc(var(--space) * 2), so the vertical padding is twice the base spacing and the horizontal padding equals it. If you later decide the base spacing should be 1.25rem, both values update together. You can build a whole spacing scale this way by multiplying one base value inside calc().
How the browser resolves var()
Custom properties are not like variables in a preprocessor such as Sass, where values are substituted before the file reaches the browser. The browser itself resolves var() while it styles the page, element by element. This is why the values can change at runtime and depend on where an element sits in the document.
The lookup follows inheritance. The nearest definition wins, which is what makes scoped overrides possible.
Scope and override
You can redefine a property on any selector. The new value applies to that element and everything inside it, and nothing else. This is useful for components that need a slightly different look.
:root {
--color-text: #1f2933;
--color-bg: #ffffff;
--color-accent: #0b5cad;
}
.project-card {
padding: 1rem;
color: var(--color-text);
background: var(--color-bg);
border: 2px solid var(--color-accent);
}
.project-card.featured {
--color-accent: #b45309;
}The .featured card gets an orange border, and any link inside it that uses var(--color-accent) turns orange too. The rest of the page keeps the blue. You did not repeat any of the card's rules.
A dark color scheme
The same idea gives you a dark theme with a few lines. Redefine the properties inside a media query, and every rule that uses them follows.
:root {
--color-text: #1f2933;
--color-bg: #ffffff;
--color-accent: #0b5cad;
}
@media (prefers-color-scheme: dark) {
:root {
--color-text: #e4e7eb;
--color-bg: #121820;
--color-accent: #7fb7f0;
}
}
body {
color: var(--color-text);
background: var(--color-bg);
}
a {
color: var(--color-accent);
}The prefers-color-scheme media feature reports the user's operating system setting. Check contrast again for the dark values. Light blue on a dark background and dark blue on a white one are separate pairs, and each has to pass on its own.
Fallback values
var() accepts a second argument, used only when the property is not defined.
.badge {
color: var(--badge-color, #444444);
padding: var(--badge-space, 0.25rem 0.5rem);
}If neither --badge-color nor --badge-space exists on the element or its ancestors, the fallbacks apply. Everything after the first comma is the fallback, so a value with commas, such as a font list, works without extra syntax. Fallbacks suit components that you may reuse in a context where the theme is missing.
Naming and organization
A short, consistent naming scheme keeps a growing stylesheet readable. A common approach is to name properties by role instead of appearance.
| Avoid | Prefer | Reason |
|---|---|---|
| `--blue` | `--color-accent` | The accent may change to orange later, and `--blue` would then be wrong. |
| `--margin-16` | `--space` | The name should not repeat the value it holds. |
| `--c1`, `--c2` | `--color-text`, `--color-bg` | A reader can tell what each one is for. |
Group related properties together at the top of the file with a comment. Keep the list short. A one-page site rarely needs more than a dozen values: a few colors, one or two spacing units, and the font stacks.
Common errors
Most problems come from a handful of causes.
| Symptom | Likely cause | Fix |
|---|---|---|
| The property has no effect and the element shows the browser default | The name is misspelled or has the wrong case in `var()` | Compare the declared name and the used name character by character. |
| A value works in plain CSS but fails through `var()` | The stored value is invalid for that property, for example `--space: red` used in `padding` | Declare a value that is valid where you use it. |
| `@media (min-width: var(--bp))` is ignored | `var()` does not work inside media query conditions | Write the length literally in the query. |
| A declaration is dropped and the earlier one is not restored | The declaration is invalid at computed-value time | Add a fallback in `var()`, and do not rely on an earlier rule as a backup. |
| `--space: 16` then `padding: var(--space)px` fails | You cannot glue a unit to a number after substitution | Store the unit, `--space: 16px`, or use `calc(var(--space) * 1px)`. |
The third row matters for the responsive chapter. Breakpoints in media queries stay as literal values, even though the rest of the design uses properties.
Putting it together
Combine the pieces on the tutorial's page. Define colors and spacing once on :root. Use them in the base rules. Override a property on a single component when it needs to differ. Add a dark scheme by redefining the same names in one media query. The accessibility chapter will ask you to check contrast, and with custom properties you can fix a failing color in one place.
Exercise
A page defines --color-accent: #0b5cad; on :root. A rule .note { --color-accent: #b45309; } is also present. Inside a .note element is a link styled with a { color: var(--color-accent); }. Which color does that link get, and why? What color do links outside .note get?
Show answer
The link inside .note is orange (#b45309). Custom properties inherit, and the nearest definition wins. The link's closest ancestor that defines --color-accent is .note. Links outside .note have only :root as a definition, so they stay blue (#0b5cad).
Exercise
You write @media (min-width: var(--breakpoint)) { main { padding-inline: 2rem; } } and the padding never changes. What is wrong, and what do you write instead?
Show answer
var() cannot be used inside a media query condition, so the condition is invalid and the block never applies. Write the length directly, for example @media (min-width: 48rem) { main { padding-inline: 2rem; } }.
Exercise
Add a dark color scheme to a stylesheet that already defines --color-text, --color-bg and --color-accent on :root. Do you need to edit any rule that reads those properties? Write the CSS you add.
Show answer
No existing rule needs editing, because they all read the properties through var(). Add this:
@media (prefers-color-scheme: dark) {
:root {
--color-text: #e4e7eb;
--color-bg: #121820;
--color-accent: #7fb7f0;
}
}
Then check contrast for the new color pairs.