Sub topic 9
Scoping overrides to one instance or a whole region
This chapter assumes you can write a basic Lightning web component (HTML template, JavaScript class, CSS file) and that you know CSS selectors, the cascade and inheritance. It also assumes you have applied a component hook such as --slds-c-button-brand-color-background to a base component. Here you decide how far each override reaches: one button, one region of a template, or everything inside a component.
Why scope matters
A styling hook is a CSS custom property. A custom property is declared on an element and inherited by that element's descendants. This one rule determines how far a hook reaches.
- Declare a hook on a base component's host element, and that component sees the value.
- Declare it on an ancestor, and every matching base component below it sees the value.
- Declare it on nothing, and the component uses its default, which is the fallback inside its own
var()call.
Inheritance crosses the shadow DOM boundary. Ordinary selectors do not. That difference explains most scoping problems, and later sections come back to it.
Targeting one instance
To change a single component instance, give the host element a class and set the hook on that class. The template below has one button with a class.
<template>
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</template>The component's CSS file attaches hook values to that class.
.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
--slds-c-button-brand-color-border: #5a1ba9;
--slds-c-button-radius-border: 1rem;
}Only the button with class="brand-action" changes. Other lightning-button elements in the same template keep their defaults, because the custom properties are declared on that one element and its descendants.
Use a class that describes the purpose, such as brand-action or danger-action. A class like purple-button goes stale the first time the design changes color.
Targeting a whole region
To restyle several components at once, set the hook on a shared ancestor. Every base component below it that reads the hook picks up the value through inheritance.
<template>
<div class="compact-region">
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
<lightning-button
variant="brand"
label="Cancel"
></lightning-button>
</div>
<lightning-button
variant="brand"
label="Outside the region"
></lightning-button>
</template>.compact-region {
--slds-c-button-radius-border: 0.25rem;
}
.compact-region .brand-action {
--slds-c-button-brand-color-background: #014486;
}The result for each button:
| Button | Corner radius | Brand background |
|---|---|---|
| Save (`.brand-action` inside the region) | 0.25rem, inherited from the region | #014486, set on the instance |
| Cancel (inside the region) | 0.25rem, inherited from the region | Default |
| Outside the region | Default | Default |
The region sets a shared decision, a tighter corner radius. The instance rule adds one extra difference. The region rule does not need to repeat itself for each button, and the buttons outside the region are untouched.
How the nearest declaration wins
Inheritance has a clear precedence rule. If an element declares a custom property itself, that value is used. The element inherits from its parent only when it declares nothing. So the declaration closest to the base component wins.
This differs from normal cascade conflicts. Two rules that both target the same element compete by specificity, then source order. A declaration on an ancestor never competes with a declaration on the element. The element's own declaration is used, whatever the specificity of the ancestor's selector.
Suppose you want the Save button to use 0.5rem radius even inside .compact-region. Declaring the hook on the button works.
.compact-region {
--slds-c-button-radius-border: 0.25rem;
}
.brand-action {
--slds-c-button-radius-border: 0.5rem;
}The .brand-action rule has lower specificity than nothing at all in this case, since the two selectors do not target the same element's competing rules. Even a more specific selector on the ancestor would not change the result. The button declares the property itself, so the button's value is used.
When two rules do target the same element, ordinary cascade rules apply. For example, .compact-region .brand-action (two classes) beats .brand-action (one class), no matter which comes first in the file.
Scoping to the whole component with :host
Sometimes the region is the entire component. Instead of adding a wrapper div, set the hooks on :host. This selector matches your component's own host element, so everything in its template inherits the values.
:host {
--slds-c-button-radius-border: 0.25rem;
--slds-c-card-color-background: #f3f3f3;
}Every base component in this template that reads those hooks is affected. Any component your component contains also inherits the values, which can surprise you (see the next section).
Rules you set on :host can be overridden by a parent, since a parent's declaration on your host element from its own stylesheet is closer to the cascade's outer layer. Keep this in mind if your component is reused in several places.
Where selectors stop and inheritance continues
LWC uses synthetic or native shadow DOM, depending on the org and the component. In both cases, the CSS file of a component only matches elements in its own template. Selectors do not reach into child components.
Inherited values behave differently. A custom property flows down through every shadow tree between the declaring element and the consumer.
This gives two rules of thumb:
- A selector such as
.compact-region .brand-actionworks only when both elements are in the same template. If.brand-actionlives in a child component's template, your selector will not match it. - A hook set on an element in your template is still seen by base components deep inside child components, as long as they are descendants of that element.
For the second case, a wrapper in your template can theme a child component's internals.
<template>
<div class="dense-theme">
<c-order-summary></c-order-summary>
</div>
</template>.dense-theme {
--slds-c-button-radius-border: 0.25rem;
}Every lightning-button inside c-order-summary now has the smaller radius. This includes buttons you did not write and may not know exist, so use the technique deliberately. If the child's author sets the same hook on one of its own elements, that declaration wins inside the child, by the nearest-declaration rule.
Choosing a scope
Pick the narrowest scope that matches the design decision.
| Design decision | Where to declare the hook |
|---|---|
| One special button | A class on that host element |
| Every control in one section of a template | A wrapper element around that section |
| Every control in one component | `:host` |
| A shared brand decision across the app | Global hooks (`--slds-g-*`), covered in a separate chapter |
A mismatch in either direction causes trouble. Too broad, and unrelated components change. Too narrow, and you repeat the same value across many classes and miss one when the design shifts.
A common pattern: defaults plus exceptions
A maintainable stylesheet often follows this structure.
:host {
--slds-c-button-radius-border: 0.25rem;
}
.toolbar {
--slds-c-button-brand-color-background: #014486;
}
.toolbar .brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
}The component sets one default, the toolbar region sets a group value, and one instance carries an exception. Reading top to bottom, each rule is a little narrower than the last. Anyone debugging a color can walk from the element up through its ancestors and find the nearest declaration.
Gotchas
- Selector in the wrong template. A rule like
.compact-region .brand-actionsilently matches nothing if the two elements are in different templates. Check where each class lives. - Hook on the wrong element. Declaring a hook on a sibling or on a descendant of the base component has no effect. The declaration must be on the host element or one of its ancestors.
- Declaring the same hook twice on one path. If you set a hook on both the region and the instance, remember the instance value wins. Removing the instance rule later changes the instance's appearance to the region's value.
- Assuming a parent's rule cannot be overridden. A parent component can declare hooks on your component's host element, and those declarations reach your template's descendants unless you declare the same hook closer to them.
- Unsupported hook names. A hook a component does not read does nothing, whatever its scope. Confirm the name against the component's documentation first.
Exercise
A template contains <div class="filters"> wrapping three lightning-button elements, and one more lightning-button outside the div. Your CSS file contains .filters { --slds-c-button-radius-border: 0.25rem; } and .filters .apply { --slds-c-button-radius-border: 1rem; }, where only one of the three buttons inside the div has class="apply". What radius does each of the four buttons get, and what would change if you moved the first rule to :host?
Show answer
The two buttons inside .filters without the apply class get 0.25rem through inheritance. The button with class="apply" gets 1rem, because the hook is declared on the element itself and the nearest declaration wins. The button outside the div gets the default radius, since no ancestor declares the hook. If the first rule moved to :host, the outside button would also get 0.25rem. The apply button would still get 1rem.
Exercise
Your component's CSS contains .compact-region .brand-action { --slds-c-button-brand-color-background: #014486; }, but the brand-action button is rendered inside a child component's template, and nothing changes. Why, and what is one way to fix it?
Show answer
A component's CSS selectors only match elements in its own template, so the selector never matches the button inside the child component. One fix is to set the hook on an element in your own template that is an ancestor of the child component, for example the wrapper or the child's host element, so the value is inherited into the child. Another is to have the child component define the hook on its own element and expose it, as described in the chapter on exposing your own hooks.