Sub topic 3
What Styling Hooks Are and Why They Exist
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 should have read the refresher on CSS custom properties from the earlier chapters.
The problem hooks solve
A base component such as lightning-button renders its markup inside a shadow tree. Your component's CSS cannot reach into that tree. A rule like lightning-button button { background: purple; } matches nothing, because the inner button element is hidden from your stylesheet.
Even where a deep selector appears to work, it is a poor choice. The internal markup and class names of a base component are not a public contract. Salesforce can change them in any release, and your override would break without warning.
Salesforce needed a supported way to customize appearance. The mechanism it chose is the CSS custom property.
What a styling hook is
A styling hook is a CSS custom property that a component or the design system reads and that you are allowed to set. Custom properties inherit. A value set on the host element, or on any ancestor, flows down into the shadow tree. Inside it, the base component's own CSS reads the value with var().
The base component decides which parts of its appearance are open to change. You supply values, and the component applies them.
A first example
Start with a template that gives the host element a class. The class is the selector your CSS will use.
<template>
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</template>Then set hooks on that class 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 rounder corners. You did not touch its internal markup. If Salesforce restructures that markup later, the hooks keep working, because they are the documented interface.
Two kinds of hooks
Hooks come in two groups, distinguished by prefix. Later chapters cover each in detail.
| Prefix | Name | Scope |
|---|---|---|
| `--slds-c-` | Component hook | One base component, such as the button |
| `--slds-g-` | Global hook | Shared design decisions, such as brand colors and spacing |
A component hook changes one thing on one kind of component. A global hook feeds many components and changes a design decision everywhere it is used.
What hooks do not do
Hooks are an allow list. You can change only what a component exposes. If a hook does not exist for the property you want, setting a made-up custom property does nothing.
Common errors at this stage:
- Setting a hook inside the wrong scope. The property must be set on the host element or an ancestor. Setting it on an element in a sibling branch has no effect.
- Guessing names. A misspelled hook is still valid CSS, so the browser raises no error. It is silently ignored.
- Expecting hooks to replace all CSS. They cover supported customizations, not layout or arbitrary properties.
Why this design works
The design relies on two facts. Custom properties cross the shadow boundary through inheritance, so no special API is needed. And Salesforce publishes hook names as a stable interface, so upgrades do not break your styling. You trade some freedom for safety: you cannot style everything, but what you style stays supported.
Exercise
A teammate writes lightning-button button { border-radius: 1rem; } in a component's CSS file and sees no change. Explain why it fails, and name the supported alternative.
Show answer
The inner button element lives in the base component's shadow tree, which your stylesheet cannot select into. Instead, give the lightning-button host element a class and set a component hook on it, for example --slds-c-button-radius-border: 1rem;. Custom properties inherit into the shadow tree, where the button's own CSS reads the value.
Exercise
What is the difference between a --slds-c- hook and a --slds-g- hook?
Show answer
A --slds-c- hook is a component hook that customizes one kind of base component. A --slds-g- hook is a global hook that carries a shared design decision used by many components.