Sub topic 3
Reusing colors with custom properties and currentColor
This chapter assumes you can read rgb() and hsl() values, know how selectors and the cascade decide which rule wins, and have a stylesheet linked to a page. It shows how to define a color once and reuse it everywhere. It also covers the keyword currentColor, which lets a property borrow the element's text color. The goal is a palette you can change in one place.
Custom properties: the working model
A custom property is a name you invent, starting with two hyphens, that holds a value. You read it back with var().
:root {
--brand: #0b5cad;
}
a {
color: var(--brand);
}
.button {
background-color: var(--brand);
}Three facts explain most of the behavior you will see.
- Custom properties follow the cascade and inherit. A value set on
:root(the<html>element) reaches every element unless something closer overrides it. - The browser substitutes the value at use time. Until
var()is evaluated, the browser treats the value as a plain string of tokens. It does not check that--brandis a color. - Names are case sensitive.
--Brandand--brandare different properties.
var() takes an optional fallback after a comma. The browser uses it only when the property is not defined.
.badge {
background-color: var(--badge-bg, #e5e7eb);
}Scoping a value to one component
Because custom properties inherit, you can override one on any element and affect only that element and its descendants. This is the basis of CSS theming.
:root {
--surface: #ffffff;
--text: #111827;
}
.panel {
background-color: var(--surface);
color: var(--text);
}
.panel.inverted {
--surface: #111827;
--text: #f9fafb;
}The .panel rule never changes. Adding the inverted class swaps the two values it reads.
Organizing a palette in layers
A flat list of color names is hard to maintain. A common approach uses two layers.
- Primitive tokens name the raw colors, such as
--blue-600or--gray-900. They say what the color is. - Semantic tokens name the job, such as
--color-textor--color-accent. They point at primitives.
Components use only semantic tokens.
:root {
--gray-50: #f9fafb;
--gray-900: #111827;
--blue-600: #1d4ed8;
--blue-300: #93c5fd;
--color-text: var(--gray-900);
--color-surface: var(--gray-50);
--color-accent: var(--blue-600);
}
body {
color: var(--color-text);
background-color: var(--color-surface);
}
a {
color: var(--color-accent);
}To add a dark theme, you change only the semantic layer.
:root {
color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
:root {
--color-text: var(--gray-50);
--color-surface: var(--gray-900);
--color-accent: var(--blue-300);
}
}The color-scheme property tells the browser that the page supports both schemes. Built-in widgets such as form controls and scrollbars then match the active scheme. To let users switch themes, you can scope the same overrides to an attribute selector like :root[data-theme="dark"]. A short script then sets that attribute.
Storing color channels for transparency
A hex value stored in a custom property cannot take an alpha value later. If you need the same color at several opacities, store only the channels and build the function at the use site.
:root {
--shadow-rgb: 0 0 0;
--accent-hsl: 220 90% 45%;
}
.card {
box-shadow: 0 2px 8px rgb(var(--shadow-rgb) / 0.15);
border: 1px solid hsl(var(--accent-hsl) / 0.4);
}
.card:hover {
border-color: hsl(var(--accent-hsl));
}This works because substitution happens before the color function is parsed. After substitution, the browser sees rgb(0 0 0 / 0.15), which is valid. The space-separated syntax with / for alpha is required here. A later chapter shows color-mix(), which can derive shades without splitting channels.
currentColor
currentColor is a keyword that means "the computed value of this element's color property." Use it anywhere a color is accepted.
.link {
color: #0b5cad;
border-bottom: 1px solid currentColor;
}If you later change color, the border follows. You do not repeat the value, and the two cannot drift apart.
Some properties already default to it:
| Property | Default behavior |
|---|---|
| `border-color` | `currentColor` when not set |
| `outline-color` | `currentColor` in modern browsers |
| `text-decoration-color` | `currentColor` |
| `box-shadow` color | `currentColor` when the color is omitted |
| `fill` and `stroke` on SVG | Not defaulted. Set them to `currentColor` yourself. |
The SVG case is the most useful. An inline icon can then match the surrounding text, including hover and disabled states.
.button {
color: var(--color-accent);
border: 2px solid;
display: inline-flex;
align-items: center;
gap: 0.5em;
}
.button svg {
width: 1.25em;
height: 1.25em;
fill: currentColor;
}
.button:hover {
color: var(--blue-300);
}The border has no color value, so it uses currentColor. On hover, a single declaration recolors the text, the border, and the icon.
Common errors
The derived-variable trap
A custom property that references another one is resolved where it is declared. Its result is then inherited as a finished value.
:root {
--base: #0b5cad;
--link: var(--base);
}
.dark-section {
--base: #7fb2ff;
}
a {
color: var(--link);
}Links inside .dark-section stay #0b5cad. The --link value was computed on :root, and descendants inherit that result. They do not re-evaluate it with the new --base. To fix this, redeclare --link: var(--base); inside .dark-section. You can also override --link directly.
Typos and invalid values
If var(--brnad) names a property that does not exist and has no fallback, the declaration is invalid at computed-value time. The browser does not skip it and keep an earlier rule. For an inherited property like color, it behaves as inherit. For a non-inherited property like background-color, it behaves as initial (transparent). Your element may lose its color with no error message. Check the Computed pane in your browser's dev tools.
Storing the wrong kind of value causes the same failure. --accent: 10px; is accepted, but color: var(--accent) is then invalid.
Defining on the wrong element
A variable defined on .card is not visible to body or to sibling elements. Put shared tokens on :root. Put overrides on the smallest element that needs them.
Variables in media queries
You cannot write @media (min-width: var(--bp)). Custom properties are not substituted in media conditions. Write the value directly.
Where currentColor resolves
If you store the keyword in a variable, such as --edge: currentColor, it is not locked to the element where you declared it. It resolves wherever you use var(--edge). This is usually what you want, but it surprises people who expect the declaring element's color.
Alpha on a fixed color
rgb(var(--hex) / 0.5) fails when --hex holds #0b5cad. Substitution is textual, and a hex token cannot sit inside the function. Store channels instead, as shown above.
Choosing between the two tools
| Need | Use |
|---|---|
| One brand color used by many unrelated rules | A custom property |
| A border, icon, or underline that matches its own text | `currentColor` |
| A theme that changes many colors at once | Semantic custom properties overridden on a parent |
| The same color at several opacities | Channel variables with `rgb(... / alpha)` |
Exercise
A page sets :root { --accent: #0b5cad; --link: var(--accent); } and then .sidebar { --accent: #c2410c; }. The rule a { color: var(--link); } applies everywhere. What color are links inside .sidebar, and how do you make them orange?
Show answer
They stay #0b5cad. --link was resolved on :root, and descendants inherit that finished value. To fix it, declare --link: var(--accent); inside .sidebar as well, or set --link directly there.
Exercise
You have an inline SVG icon inside a button. You want it to match the button's text color, including when the text color changes on hover. Which declaration do you add to the icon, and why does the hover rule need no extra change?
Show answer
Add fill: currentColor; to the icon. currentColor resolves to the element's computed color, so when the hover rule changes color on the button, the icon inherits it and its fill updates automatically.