Sub topic 1
CSS custom properties refresher
Styling hooks are built on one browser feature: CSS custom properties. This chapter reviews how they work, so the later chapters on component hooks (--slds-c-*) and global hooks (--slds-g-*) can focus on SLDS itself. You will see how to declare and read a custom property, how it inherits, and how fallback values behave. You will also see the mistakes that make a property seem to do nothing.
Prerequisites
This chapter assumes you already know the following:
- The basic structure of a Lightning web component: an HTML template, a JavaScript class, and a CSS file with the same name.
- CSS selectors, specificity, and the cascade.
- That some CSS properties, such as
colorandfont-family, inherit from a parent element, and others, such asmarginandborder, do not. - What SLDS and base components such as
lightning-buttonare, at a general level.
You do not need to know how shadow DOM works internally. The next chapter covers it.
Declaring and reading a custom property
A custom property is a CSS property whose name you choose. The name must start with two dashes. You declare it like any other property and read it with the var() function.
.card {
--card-padding: 1rem;
--card-accent: #0b5cab;
padding: var(--card-padding);
border-left: 4px solid var(--card-accent);
}Three points about this syntax matter later:
- The declaration
--card-padding: 1rem;stores a value. It does not style anything by itself. var(--card-padding)substitutes that value into a normal property. Only a normal property such aspaddingproduces a visual effect.- The value is a token sequence. The browser does not check it against any property type when you declare it. It checks the value only after substitution.
Names are case-sensitive. --Card-Accent and --card-accent are two different properties.
Custom properties are resolved by the browser
If you have used Sass or Less variables, the difference is important. A Sass variable is replaced at build time, and the browser never sees it. A custom property exists in the browser at runtime. It takes part in the cascade and inheritance, and you can change it with JavaScript.
// Change a custom property on one element at runtime
const card = document.querySelector('.card');
card.style.setProperty('--card-accent', '#c23934');
// Read the value that applies to that element
const value = getComputedStyle(card).getPropertyValue('--card-accent');
console.log(value.trim()); // "#c23934"This runtime behavior is what lets Salesforce expose styling hooks. The platform can't rewrite your CSS, but it can read whatever value reaches an element.
Inheritance
Custom properties inherit by default. A value set on an element is available to all of its descendants, unless something closer overrides it. Many ordinary properties such as margin do not behave this way, so this is a key feature.
The following page runs as shown. Save it as an .html file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Custom property inheritance</title>
<style>
.region {
--tile-background: #eef4ff;
}
.region.warning {
--tile-background: #fff3cd;
}
.tile {
background-color: var(--tile-background, #f3f3f3);
padding: 0.5rem 1rem;
margin: 0.5rem 0;
}
.tile.special {
--tile-background: #d4edda;
}
</style>
</head>
<body>
<div class="region">
<div class="tile">Inherits blue from .region</div>
<div class="tile special">Overrides to green on itself</div>
</div>
<div class="region warning">
<div class="tile">Inherits yellow from .region.warning</div>
</div>
<div class="tile">No ancestor sets it, so the fallback gray is used</div>
</body>
</html>The .tile rule never mentions a color directly. Each tile resolves --tile-background from the nearest element above it that sets it, or from itself. This pattern is how one rule on an ancestor can restyle many descendants. Styling hooks use the same pattern, as a later chapter shows.
Fallback values
var() accepts an optional second argument, which is used when the property is not defined.
.badge {
background-color: var(--badge-background, #f3f3f3);
color: var(--badge-color, var(--badge-default-color, #080707));
}Some details about fallbacks:
- The fallback is everything after the first comma. A fallback such as
var(--font, Arial, sans-serif)is valid, and the fallback value isArial, sans-serif. - Fallbacks can nest, as the
colorline above shows. The browser tries--badge-color, then--badge-default-color, then the literal#080707. - The fallback is used only when the property is missing or holds the guaranteed-invalid value. If the property is defined, even to something unhelpful, the fallback is ignored.
- Without a fallback, an undefined property makes the declaration invalid at computed-value time, as the next sections explain.
Where to declare a property
You can declare a custom property in any rule. The choice of element decides who can see it.
| Where you declare it | Who sees the value | Typical use |
|---|---|---|
| `:root` in a global stylesheet | Every element in the document | Page-wide design tokens |
| `:host` in a component stylesheet | The component's host element and everything inside it | Defaults for a component's own properties |
| A class on one element | That element and its descendants | One instance or one region |
| An inline `style` attribute | That element and its descendants | Values set from JavaScript |
In a Lightning web component, :host refers to the element that represents the component itself. Declaring defaults on :host is the usual way to give a component its own configurable values. A later chapter returns to this when you expose hooks from your own component.
How the browser decides the final value
A custom property goes through the same cascade as any other property. If two rules set it on the same element, specificity and source order decide the winner. Then inheritance fills in the elements that did not set it directly.
Substitution happens last. The browser takes the winning value, plugs it into the property that uses var(), and checks the result.
This leads to the behavior that confuses many developers. Suppose a value does not suit the property it lands in:
.panel {
--gap: blue;
margin: 1rem;
margin: var(--gap);
}You might expect the browser to fall back to margin: 1rem. It does not. The declaration is "invalid at computed-value time." The browser has already chosen margin: var(--gap) as the winner, and it then treats the property as unset. For margin, which does not inherit, that means the initial value 0. The earlier valid declaration is not restored.
Limits of custom properties
Custom properties are flexible, but there are restrictions you should know:
var()can replace a property value or part of one. It cannot build a property name or a selector.- You can't use
var()inside a media query condition, such as@media (min-width: var(--bp)). - A property that refers to itself, directly or through a cycle, becomes invalid.
- A custom property does not change by itself when the page changes. Anything that is not covered by the cascade, such as a value from JavaScript, has to be set on an element.
Common errors
| Symptom | Likely cause | What to check |
|---|---|---|
| The fallback color always appears | The property name is misspelled or set on the wrong element | Compare names character by character, including case |
| A value set on a child has no effect on its parent | Custom properties inherit downward, never upward | Set the property on the ancestor or on the element that uses `var()` |
| A normal property resets to its default | The custom property holds a value that doesn't suit that property | Look for "invalid at computed-value time" in developer tools |
| A rule on a broad selector loses to another | Specificity or source order | Compare selector specificity in the Styles panel |
| The property is set but nothing changes | No rule reads it with `var()` | Search the stylesheet for `var(--your-name` |
Why this matters for styling hooks
Two facts from this chapter carry the rest of the tutorial.
- Custom properties inherit down the tree, so a value set on a parent reaches elements inside it.
- A rule that reads
var(--name, fallback)renders the value if one arrives and the fallback if it does not.
An SLDS styling hook is the combination of these two facts. A base component's CSS reads a hook with var() and a default. Your CSS sets the hook on an ancestor, and the value flows in. The next chapter explains why this is necessary, by showing why you can't override a base component's internal CSS directly.
Exercise
Given the CSS below, what background color does <div class="tile"> inside <section class="region"> show, and why?
.region { --tile-bg: #eef4ff; }
.tile { background-color: var(--tile-bg, #f3f3f3); }
.tile { --tile-bg: #d4edda; }
Show answer
It shows #d4edda. Both .tile rules apply to the same element, and a value set on the element itself wins over the value it would inherit from .region. The fallback #f3f3f3 is never used because --tile-bg is defined.
Exercise
A developer writes margin: 1rem; margin: var(--gap); with --gap: blue;. They expect a 1rem margin. What margin do they actually get, and why?
Show answer
The margin is 0. The var() declaration wins the cascade, and then the browser finds that blue is not a valid margin. The declaration becomes invalid at computed-value time and acts as unset. For a non-inherited property such as margin, unset means the initial value, 0. The earlier margin: 1rem is not restored.
Exercise
You set --badge-color-background on a child element, but the parent's var(--badge-color-background, #f3f3f3) still shows gray. Why?
Show answer
Custom properties inherit downward only. The parent never receives a value from its child, so the parent uses the fallback. Set the property on the parent or on one of its ancestors instead.