Sub topic 3
Scoping SLDS Styling Hook Overrides
Assume you know LWC templates, CSS selectors, inheritance, SLDS, and base components such as lightning-button. Styling hooks are CSS custom properties, so their values can inherit into supported base components.
Scope an override
Put a hook on the base component's host element to affect one instance:
<template>
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</template>.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
--slds-c-button-brand-color-border: #5a1ba9;
--slds-c-button-radius-border: 1rem;
}Put the hook on an ancestor to affect every matching component in that region. A more specific selector can then override one instance.
.compact-region {
--slds-c-button-radius-border: 0.25rem;
}
.compact-region .brand-action {
--slds-c-button-brand-color-background: #014486;
}Expose hooks from your component
A custom component can define its own public hook with var(). The fallback applies when no parent supplies a value.
:host {
--c-badge-color-background: #eef4ff;
}
.badge {
background-color: var(--c-badge-color-background, #f3f3f3);
padding: 0.25rem 0.5rem;
border-radius: 0.25rem;
}A parent can set --c-badge-color-background on the component or one of its ancestors. Document custom hooks as part of the component's styling contract.
Debug a hook with no effect
First confirm that the hook name is supported by that base component. Then inspect the rendered host element in browser developer tools. Check:
- The rule matches the intended element.
- The custom property appears in the computed styles.
- A later or more specific rule does not replace it.
- The component's variant supports that hook.
- The value is valid CSS.
When hooks are not enough
Use SLDS utility classes for standard spacing, layout, and display behavior. Use hooks for documented component tokens, such as button colors or radii. Use custom CSS or a wrapper component when the supported hooks cannot express the required structure or behavior. Avoid undocumented internal selectors.
Keep overrides maintainable
Keep related hooks near the selector that scopes them. Prefer semantic class names such as .brand-action over element-wide overrides. Centralize shared global decisions, and use component hooks for local exceptions. Recheck hook support when Salesforce or SLDS versions change.
Exercise
How would you make every button in a .compact-region use a small radius while keeping one .brand-action button purple?
Show answer
Set --slds-c-button-radius-border on .compact-region, then set --slds-c-button-brand-color-background on .compact-region .brand-action.