Sub topic 3
Reusing Colors with Custom Properties and currentColor
This chapter assumes you know CSS selectors and the cascade, the box model, and how to write colors with hex, rgb(), and hsl(). Here you will define each color once and reuse it across a stylesheet.
The problem with repeated color values
A stylesheet that repeats #0b5cad in forty places is hard to change. A brand update means a search-and-replace, and one missed value leaves an inconsistent page. Custom properties and the currentColor keyword remove that repetition in two different ways.
Custom properties as a palette
A custom property is a name you define that starts with two dashes, such as --brand. You read it with var(). Custom properties follow the cascade and inherit, so where you declare one decides where it is available.
Declaring them on :root makes them available to the whole document.
:root {
--brand: oklch(55% 0.2 260);
--brand-light: color-mix(in oklch, var(--brand), white 30%);
}
.button {
background-color: var(--brand);
color: white;
}
.button:hover {
background-color: var(--brand-light);
}Here --brand is defined once, and --brand-light is derived from it with color-mix(). Change --brand and both colors update. The oklch() and color-mix() functions are covered in a later chapter. For now, treat them as ways to write a color.
You can also give var() a fallback value as a second argument. It is used if the property is not defined.
.note {
color: var(--note-text, #333333);
}Naming palettes
Name properties by role, not by appearance. A name like --blue becomes wrong when the brand turns green. A name like --brand or --text-muted stays correct.
Overriding a property in a scope
Because custom properties inherit, you can redefine one on a specific element. Every descendant that uses it picks up the new value.
:root {
--surface: #ffffff;
--text: #1a1a1a;
}
.dark-panel {
--surface: #14181f;
--text: #f2f4f8;
}
.panel,
.dark-panel {
background-color: var(--surface);
color: var(--text);
}The .panel and .dark-panel rules are identical. Only the property values differ. This is the basis for themes.
currentColor
currentColor is a keyword that means "the computed value of this element's color property." It lets another property follow the text color without a separate declaration.
.link {
color: #0b5cad;
border-bottom: 1px solid currentColor;
}The border is the same blue as the text. If you change color, for example in a :hover rule, the border changes with it.
currentColor is useful for inline SVG icons. Set fill: currentColor on the icon, and it matches the surrounding text. Note that border-color already defaults to currentColor when you do not set it, which is why a bare border: 1px solid takes the text color.
Choosing between the two
| Need | Use |
|---|---|
| One color shared by many unrelated rules | A custom property |
| A property that should match the element's own text color | `currentColor` |
| A value that changes inside a themed section | A custom property overridden in that scope |
Common errors
- Declaring the property in the wrong place. A property set on
.cardis not visible to a sibling or a parent. Put shared values on:root. - Missing
var(). Writingcolor: --brand;is invalid. Writecolor: var(--brand);. - Case differences. Custom property names are case-sensitive, so
--Brandand--brandare different properties. - Expecting a fallback for a bad value. The
var()fallback applies only when the property is undefined. If it is defined with an unusable value, the declaration becomes invalid at computed-value time and the property falls back to its inherited or initial value.
Exercise
A link has color: #0b5cad; and border-bottom: 1px solid currentColor;. You add .link:hover { color: #c2410c; }. What color is the border on hover, and why?
Show answer
The border is #c2410c. currentColor resolves to the element's computed color, so when hover changes color, the border color changes with it. No separate border rule is needed.
Exercise
You define --text: #1a1a1a; on :root and --text: #f2f4f8; on .dark-panel. A paragraph inside .dark-panel uses color: var(--text);. Which value does it get?
Show answer
It gets #f2f4f8. Custom properties inherit, and the nearest ancestor that defines --text supplies the value, which here is .dark-panel.