Sub topic 9
Scoping Overrides to One Instance or a Whole Region
This chapter assumes you can write a Lightning web component with an HTML template and a CSS file. It also assumes you know CSS selectors, the cascade, and inheritance. You should have applied a component hook to a base component such as lightning-button.
Why scope matters
A styling hook is a CSS custom property. Custom properties inherit, so the place where you set a hook decides which elements receive the value. You can set a hook on one element, or on an ancestor so that every matching base component inside it picks the value up. The cascade then settles conflicts, and the more specific selector wins.
Scoping to one instance
Give the element a class in the template, then set the hook on that class.
<template>
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
<lightning-button
variant="brand"
label="Cancel"
></lightning-button>
</template>.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
--slds-c-button-brand-color-border: #5a1ba9;
}Only the Save button changes. The Cancel button has no brand-action class, so it keeps the default value.
Scoping to a region
Set the hook on a wrapper element to style every matching base component inside it.
<template>
<div class="compact-region">
<lightning-button variant="brand" label="Save"></lightning-button>
<lightning-button variant="neutral" label="Back"></lightning-button>
</div>
<lightning-button variant="neutral" label="Help"></lightning-button>
</template>.compact-region {
--slds-c-button-radius-border: 0.25rem;
}Both buttons inside compact-region get the smaller corner radius. The Help button sits outside the wrapper, so it does not.
Combining region and instance
You can set a region default and then override a single instance inside it.
.compact-region {
--slds-c-button-radius-border: 0.25rem;
}
.compact-region .brand-action {
--slds-c-button-brand-color-background: #014486;
}The button with brand-action inherits the radius from the region. It also sets its own background color. A hook that is set on the element itself always beats an inherited value for the same property, so the instance value takes priority.
How conflicts resolve
Two rules decide which value an element ends up with:
- If a hook is set directly on the element, that value is used. An inherited value is ignored for that property.
- If two rules set the same hook directly on the same element, normal cascade rules apply. Specificity is compared first, and source order breaks ties.
| Where the hook is set | Who receives it | Typical use |
|---|---|---|
| On the base component's class | That one instance | One-off emphasis, such as a primary action |
| On a wrapper element | Every matching component inside it | A compact toolbar or a themed panel |
| On `:host` of your component | Everything in your component's template | A component-wide default |
Common mistakes
- Setting the hook on a selector that does not match. In LWC, your CSS file only reaches elements in your own template. A class you add to a child component's internal markup does not exist from your side.
- Writing a descendant selector that is too weak. If another rule in your file sets the same hook with higher specificity, yours loses even if it comes later.
- Expecting a region hook to reach a component that sets the hook itself. If a component assigns its own value to the property on its host, that value replaces what it would have inherited.
Exercise
Exercise
A template contains <div class="toolbar"> holding two lightning-button elements. One of them has class="danger". Write CSS that gives every button in the toolbar a corner radius of 0.25rem through --slds-c-button-radius-border, and gives only the danger button a corner radius of 1rem. Explain why the second rule wins for that button.
Show answer
.toolbar { --slds-c-button-radius-border: 0.25rem; } and .toolbar .danger { --slds-c-button-radius-border: 1rem; }. The first rule sets the hook on the wrapper, and both buttons inherit it. The second rule sets the same hook directly on the danger button. A value set on the element itself replaces the inherited one, so that button gets 1rem and the other keeps 0.25rem.