Sub topic 8
Applying hooks in your component's CSS
This chapter assumes you can build a basic Lightning web component (HTML template, JavaScript class, CSS file). It also assumes you know CSS selectors, the cascade, and inheritance, and that you have used base components such as lightning-button. You already know that component hooks start with --slds-c-. Here you will put them to work.
The working model
A base component renders inside a shadow tree. Your CSS file cannot reach the elements inside it. Custom properties are different, because they inherit through the shadow boundary. If you set a hook on the base component's host element (the <lightning-button> tag itself), the component's internal CSS reads it through var() and uses your value.
So applying a hook takes three steps:
- Give the host element a class in your HTML template.
- Write a rule in your component's CSS file that selects that class.
- Set one or more
--slds-c-*properties in that rule.
Worked example: a branded button
Start with the template. The class attribute goes on the host element, not on anything inside it.
<template>
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</template>Now set the hooks in the component's CSS file.
.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
--slds-c-button-brand-color-border: #5a1ba9;
--slds-c-button-radius-border: 1rem;
}The button now has a purple background, a purple border, and rounded corners. You did not touch its internal markup or its CSS.
Two details matter here:
- The hook name must match the variant.
--slds-c-button-brand-color-backgroundaffectsvariant="brand". It does nothing on a neutral button. - Each hook controls one property. To change a color and a radius, set two hooks.
Applying hooks to many components at once
Because custom properties inherit, you can set a hook on an ancestor. Every matching base component inside it picks up the value.
.compact-region {
--slds-c-button-radius-border: 0.25rem;
}
.compact-region .brand-action {
--slds-c-button-brand-color-background: #014486;
}The first rule applies to every button inside .compact-region. The second rule is more specific, so it changes only the background of the instance with brand-action. The radius from the region still reaches that button, because the second rule does not set it.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Nothing changes | The class is on a wrapper `div`, not on the base component, or the hook name is misspelled | Put the class on the host element and copy the hook name from the component's documentation |
| Only some buttons change | The hook is for a different variant | Use the hook that names your variant, such as `brand` |
| Value is ignored | A rule with higher specificity sets the same hook | Raise the specificity of your selector or remove the competing rule |
| Style rule such as `.brand-action button { ... }` has no effect | Shadow DOM blocks selectors that reach inside | Use a hook instead of a descendant selector |
Exercise
A developer wants every brand button inside a .toolbar container to use #014486 as its background, but one button with the class danger-action should use #c23934. Write the CSS rules, using --slds-c-button-brand-color-background.
Show answer
.toolbar {
--slds-c-button-brand-color-background: #014486;
}
.toolbar .danger-action {
--slds-c-button-brand-color-background: #c23934;
}
The first rule sets the hook on the ancestor, so every brand button inside .toolbar inherits it. The second selector is more specific and sets a different value for the one instance.
Exercise
You set --slds-c-button-brand-color-background on a div that wraps a lightning-button with variant="neutral", and the color does not change. Give two possible reasons.
Show answer
The hook targets the brand variant, so a neutral button does not read it. Also, a more specific rule may set the same hook, or the name may be misspelled. Check the documented hooks for the neutral variant and inspect the computed value in developer tools.