Sub topic 11
Customizing appearance with styling hooks
Prerequisites
This chapter assumes you can build a Lightning web component with an HTML template, a JavaScript class, and a CSS file. It also assumes you know the basics of lightning-button, lightning-card, and SLDS utility classes from earlier chapters. You need a Salesforce org or scratch org if you want to see the results.
The problem styling hooks solve
Base components render their markup inside a shadow boundary. Your component's CSS cannot reach the elements inside lightning-button. A rule such as lightning-button button { background: red; } does nothing, because the inner button is not visible to your stylesheet.
Developers used to work around this in two ways. Both are fragile:
- Overriding internal class names such as
.slds-button_brand. These names are an implementation detail and can change between releases. - Replacing the base component with a custom element so that all the CSS is under their control. This loses the behavior, accessibility, and updates that the base component provides.
Styling hooks are the supported alternative. A styling hook is a CSS custom property (a variable whose name starts with --) that a base component or SLDS blueprint reads when it paints itself. CSS custom properties inherit through the shadow boundary, so a value you set on the host element reaches the markup inside.
How a hook works
Consider the hook from the topic's example:
lightning-button {
--slds-c-button-brand-color-background: #0b5cab;
}The rule sets a custom property on the lightning-button host element. Inside the component, the button's own styles read that property, roughly in the form background-color: var(--slds-c-button-brand-color-background, <default>). When the property is set, the button uses your value. When it is not set, the button uses its default.
Because the component author chose which properties to read, you can change only what the hook allows. That limit is the point. The hooks are a documented contract, so your customization keeps working when the internal markup changes.
Reading a hook name
SLDS hook names follow a pattern. Take --slds-c-button-brand-color-background:
| Part | Meaning |
|---|---|
| `--slds-` | The SLDS namespace |
| `c-` | Component hook, scoped to one component |
| `button-brand` | The component and variant it applies to |
| `color-background` | The property being changed |
Hooks that begin with --slds-g- are global hooks. They represent design decisions shared across the system, such as a brand color or a spacing value, and are covered below.
Component hooks and global hooks
The two kinds of hook differ in reach.
| Kind | Prefix | Reach | Typical use |
|---|---|---|---|
| Component hook | `--slds-c-` | One component, or one variant of it | Change one button, one card, or one input |
| Global hook | `--slds-g-` | Everything that reads the value | Apply a consistent color, spacing, or font choice |
Use a component hook when you want a local change. Use a global hook when you want consistency, for example when every control on a screen should share a brand color. Check the reference for the hooks your org supports, because the available set depends on the release and on the component.
Where to set a hook
Where you place the declaration decides how far it reaches. Custom properties inherit, so setting one on a parent affects its descendants.
On the base component's host
Target the element by its tag name in your component's CSS file:
lightning-button {
--slds-c-button-brand-color-background: #0b5cab;
}This changes every lightning-button in your template that uses the brand variant, and no others in the page.
On a class, for a single instance
Add a class to one element and select it. This keeps other buttons unchanged:
<template>
<lightning-button
class="save-button"
variant="brand"
label="Save"
></lightning-button>
<lightning-button
variant="brand"
label="Cancel"
></lightning-button>
</template>.save-button {
--slds-c-button-brand-color-background: #0b5cab;
}Only the Save button changes. The Cancel button keeps its default brand color.
On a container, for a group
Set the property on a wrapping element and every matching component inside inherits it:
.toolbar {
--slds-c-button-brand-color-background: #0b5cab;
--slds-c-button-brand-color-border: #0b5cab;
}Any brand button inside an element with the toolbar class picks up both values. This is the most economical option when you style a section rather than a single control.
Combining hooks
A single component usually exposes several hooks, one per visual property. A brand button can change its background, border, text color, and hover state independently:
.save-button {
--slds-c-button-brand-color-background: #0b5cab;
--slds-c-button-brand-color-border: #0b5cab;
--slds-c-button-text-color: #ffffff;
}Set only the properties you need. Anything you leave out keeps its default, so a partial override does not break the rest of the component.
States and contrast
A button has more than one visual state: normal, hover, focus, active, and disabled. If you change the normal background, check the others. Where a hook for the hover or active state exists, set it too, or the button will jump back to the default color when the pointer moves over it.
Contrast matters as much as color choice. Text must stay readable against the new background. WCAG 2.1 asks for a contrast ratio of at least 4.5:1 for normal-size text. A light background with white text fails that check. Verify your values with a contrast checker, and do not assume that a color that looks fine on your monitor is accessible.
Why not override internal styles
It can be tempting to reach inside with a global stylesheet or a selector that pierces the shadow boundary. Avoid it, for these reasons:
- It breaks on upgrade. Internal class names and markup are not part of the public contract. Salesforce can change them in any release, and your override silently stops working.
- It may not work at all. Shadow DOM encapsulation exists to stop your CSS from reaching inside. Techniques that work in one rendering mode can fail in another.
- It can damage accessibility. Internal styles include focus rings and state indicators. Overriding them can remove cues that keyboard users depend on.
Styling hooks avoid all three. They are public, they pass through the boundary on purpose, and they change only values that the component author expected people to change.
Fallbacks and your own hooks
The var() function takes a second argument, a fallback used when the property is not set. You can use the same mechanism in your own components so that people who use them can customize them in the same way:
.badge {
background-color: var(--c-badge-background, #e5e5e5);
color: var(--c-badge-text, #181818);
}A parent that sets --c-badge-background changes the badge. A parent that sets nothing gets the gray default. Use a prefix of your own, such as --c-, so your names cannot collide with SLDS names.
Checking that a hook applied
When a hook seems to do nothing, work through this list:
- Open the browser developer tools and select the base component's host element.
- Look at the computed styles for your custom property. If it is missing, your selector did not match, or a more specific rule replaced it.
- Confirm the hook name matches the documentation exactly. A typo creates a valid but unused custom property, and the browser gives no error.
- Confirm the variant matches. A
--slds-c-button-brand-*hook affects only buttons withvariant="brand". - Confirm that the component and your org's release support the hook.
The typo case is the most common. Because the browser accepts any custom property name, a wrong name fails silently.
Putting it together
The following component combines a container hook, a per-instance hook, and a fallback in one template and stylesheet.
<template>
<div class="toolbar">
<lightning-button
class="primary-action"
variant="brand"
label="Save"
></lightning-button>
<lightning-button
variant="brand"
label="Save and new"
></lightning-button>
</div>
</template>.toolbar {
--slds-c-button-brand-color-background: #0b5cab;
--slds-c-button-brand-color-border: #0b5cab;
}
.primary-action {
--slds-c-button-brand-color-background: #014486;
--slds-c-button-brand-color-border: #014486;
}Both buttons inherit the lighter blue from .toolbar. The first button then sets a darker blue on itself, and that value wins because a declaration on the element is closer than an inherited one.
Summary of decisions
- Use a component hook (
--slds-c-) for a local change and a global hook (--slds-g-) for a shared design decision. - Set the property on the narrowest element that gives the result you want.
- Set related hooks together, including hover and focus states where they exist.
- Check contrast for every state.
- Treat a missing hook as a design constraint and do not reach into internal markup.
Exercise
A teammate writes lightning-button button { background: #0b5cab; } in the component stylesheet, and the button color does not change. Explain why, and show the supported way to change the brand button background to the same color for one button with the class save-button.
Show answer
The inner button element is inside the base component's shadow boundary, so a selector in your stylesheet cannot match it. Use a styling hook instead, scoped to that one element: .save-button { --slds-c-button-brand-color-background: #0b5cab; }. The button must also have variant="brand", because the hook applies only to the brand variant.
Exercise
You set --slds-c-button-brand-color-backgroud: #0b5cab; (note the spelling) on a .toolbar container, and no button changes. What happened, and how would you find the cause?
Show answer
The name has a typo ("backgroud"). The browser accepts any custom property name, so it creates an unused property and reports no error, and the button never reads it. Open developer tools, inspect the computed styles, and compare the property name with the documented hook name. Correct the spelling to --slds-c-button-brand-color-background.