Sub topic 1
CSS custom properties refresher
Prerequisites
Assume you know basic Lightning web component structure, CSS selectors, inheritance, the cascade, SLDS, and base components such as lightning-button.
CSS custom properties refresher
A CSS custom property is a named value that begins with --. The var() function reads that value:
:host {
--brand-color: #5a1ba9;
}
.button {
background-color: var(--brand-color, #0176d3);
}The second argument is a fallback. The browser uses it when --brand-color is not defined. Custom properties inherit by default, so a value on an ancestor can reach its descendants.
Why you cannot override base component CSS directly
Lightning base components use shadow DOM. Shadow DOM is an encapsulated DOM tree that protects a component's internal markup and styles. CSS selectors in your component cannot select an element inside a base component's shadow tree.
For example, this selector does not reliably reach the internal button:
lightning-button button {
background: purple;
}The base component controls its internal markup. Directly depending on that markup would also make your code fragile when Salesforce changes its implementation.
Custom properties provide a controlled boundary. A base component can read a property supplied by its host without exposing its internal elements.
flowchart TD
A[Your component CSS] --> B[Host element]
B --> C[Inherited custom property]
C --> D[Base component shadow tree]
D --> E[Supported visual change]What styling hooks are and why they exist
An SLDS styling hook is a documented CSS custom property that a base component reads for a particular design value. Hooks let you customize supported parts of a component while preserving its structure, accessibility behavior, and SLDS states.
<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;
}The hook works because the property inherits from the host into the component's shadow tree.
Reading hook names
Hook prefixes describe their scope:
| Prefix | Meaning | Typical scope |
|---|---|---|
| `--slds-c-` | Component hook | One base component type |
| `--slds-g-` | Global hook | Shared design decisions |
The rest of a component hook usually identifies the component, variant, and property. For example, --slds-c-button-brand-color-background refers to the brand button's background color. Use only hooks documented for the component and SLDS version you use. An undocumented property may have no effect.
Exercise
Why can a custom property cross a base component's shadow DOM boundary when a normal selector cannot?
Show answer
The base component explicitly reads the inherited custom property, while selectors outside the shadow tree cannot target its internal elements.