Sub topic 7
Designing components with custom property hooks
This chapter assumes you know how custom properties are declared, how var() reads them, how fallbacks work, and how inheritance scopes a value to an element and its descendants. Here you use those pieces to give a component a small, deliberate styling API.
The idea: properties as hooks
A component's CSS usually has two audiences. The author wants internal rules that stay consistent. The consumer wants to adjust a few things, such as color or spacing, without overriding internal selectors or fighting specificity.
A custom property hook solves this. The component reads a custom property in its rules. A consumer sets that property on the component, or on any ancestor, to change the result. The property name is the public contract. The selectors inside the component stay private.
A component with a public hook and a private default
Start with the pattern from the topic: the component reads its own property, and a modifier changes the value in that element's scope only.
:root {
--brand-color: #0b5fff;
}
.btn {
--btn-bg: var(--brand-color);
background: var(--btn-bg);
color: white;
}
.btn.danger {
--btn-bg: crimson;
}The .btn rule never changes its background declaration. Variants only change --btn-bg. Because custom properties inherit, any child of .btn can also read --btn-bg, for example to draw an icon in a matching color.
There is a limit to this version. .btn sets --btn-bg itself, so a consumer's value set on an ancestor is overridden by the component's own declaration. Only a rule that targets .btn directly can change it. That is often fine for variants. For a hook that consumers set from outside, you need a different shape.
Letting outside values win with fallbacks
Move the default into the var() fallback instead of declaring the property on the component.
.card {
background: var(--card-bg, white);
color: var(--card-color, #222);
padding: var(--card-padding, 1rem);
border: 1px solid var(--card-border, var(--brand-color, gray));
}Now .card declares nothing. If no ancestor or rule defines --card-bg, the fallback white applies. If something does, that value wins. A consumer can set the hook in three places:
/* On one instance */
.sidebar .card {
--card-bg: #f4f6fa;
}
/* On a whole region */
.dark-region {
--card-bg: #1b1f27;
--card-color: #eee;
}<!-- Inline, for a single element -->
<div class="card" style="--card-padding: 2rem">Roomy card</div>The nested fallback in --card-border shows a chain of defaults: the card hook, then the site-wide --brand-color, then gray.
Choosing between the two patterns
| Pattern | Where the default lives | Who can override it | Best for |
|---|---|---|---|
| Declare on the component (`--btn-bg: ...`) | A declaration on the component | Rules that target the component itself | Variants and states you control |
| Fallback in `var()` (`var(--card-bg, white)`) | The fallback argument | Any ancestor or the component's own rules | Hooks consumers set from outside |
You can combine them. Use an internal property for derived values, and a public hook that feeds it:
.badge {
--_bg: var(--badge-bg, #e8eefc);
background: var(--_bg);
border: 1px solid color-mix(in srgb, var(--_bg), black 20%);
}The leading underscore in --_bg is only a naming convention that signals "private". CSS does not enforce it. The benefit is that the border is computed from whichever background the consumer chose.
Deriving values with calc()
Hooks work well with calc(). Expose one number and derive the rest from it, so consumers cannot create inconsistent combinations.
.stack {
--stack-gap: 0.5rem;
display: grid;
gap: var(--stack-gap);
padding: calc(var(--stack-gap) * 2);
}A consumer who sets --stack-gap: 1rem gets both a larger gap and proportionally larger padding.
flowchart TDDesign guidelines
- Prefix hook names with the component name, such as
--card-bg, to avoid collisions with other components. - Expose a small set of hooks. Each one is a promise you must keep when refactoring.
- Document each hook with its expected type and default.
- Put defaults in the fallback when consumers should set values from outside.
- Use hooks for values, not for structure. A hook that switches
displaybetween very different layouts is usually a sign you need a separate variant class.
Exercise
You wrote .alert { --alert-bg: #fde8e8; background: var(--alert-bg); }. A teammate sets --alert-bg: navy on body, but alerts stay pale red. Why, and how do you change .alert so the teammate's value works while keeping the pale red as the default?
Show answer
The .alert rule declares --alert-bg on the element itself, and a declaration on an element beats the value it would inherit from body. Remove that declaration and use a fallback: .alert { background: var(--alert-bg, #fde8e8); }. Now navy from body is inherited and used, and alerts fall back to pale red when nothing sets the hook.
Exercise
Write a .tag component with a public hook --tag-bg (default #eee) and a border that is always 20% darker than the chosen background, using one private property.
Show answer
.tag { --bg: var(--tag-bg, #eee); background: var(--bg); border: 1px solid color-mix(in srgb, var(--_bg), black 20%); }