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 understand CSS selectors, the cascade, and inheritance, and that you know base components such as lightning-button and lightning-card. By the end you will be able to set component hooks from your own CSS file, pick the right selector for them, and recognize the most common reasons a hook does nothing.
The working model
A styling hook is a CSS custom property. A base component's internal stylesheet reads that property with var(). If you set a value for it, the component uses your value. If you do not, the component uses its default.
Custom properties inherit. Your component's CSS sets a property on an element in your template. The base component sits inside that element, and its shadow tree inherits the value. This is why hooks work even though shadow DOM blocks ordinary selectors from reaching the base component's internals.
So applying a hook takes three steps:
- Give the element that you want to style a selector you can target. A class on the host element is the usual choice.
- Look up the hook name for the property you want to change.
- Declare the hook in your component's CSS file, on that selector or on an ancestor.
Step 1: Target the element with a class
CSS in a Lightning web component is scoped to that component's template. A class selector in your CSS file matches elements in your own template, including the host element of a base component. It does not match elements inside the base component.
<template>
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</template>The class attribute is on the lightning-button tag itself. That tag is the host element, and it is the element your stylesheet can see.
Step 2: Set the hooks
Component hooks follow the pattern --slds-c-<component>-<part>-<property>. For the brand button, three useful hooks are the background color, the border color, and the border radius.
.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
--slds-c-button-brand-color-border: #5a1ba9;
--slds-c-button-radius-border: 1rem;
}You never write background-color or border-radius against the button's inner markup. You only declare custom properties. The button's own CSS decides where each value is applied, including on its hover and focus states where the component defines hooks for them.
Some points to keep in mind when you write these declarations:
- Hook names are case-sensitive and must match exactly. A misspelled name is a valid custom property that nothing reads, so the browser raises no error.
- Any valid CSS value for the underlying property works. Colors, lengths, and
var()references to other properties are all allowed. - A hook only affects the part of the component that reads it. A button background hook does not change the label color. Look for a separate text color hook.
- Setting a hook to an empty or invalid value can make the browser fall back in unexpected ways. Use a complete, valid value.
Applying several hooks together
Hooks for different states are separate properties. If you change only the base background, hover and active states keep their defaults. That can leave a button that changes color in a way that clashes with your theme. Set the related state hooks together.
.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
--slds-c-button-brand-color-border: #5a1ba9;
--slds-c-button-brand-color-background-hover: #461482;
--slds-c-button-brand-color-border-hover: #461482;
--slds-c-button-radius-border: 1rem;
}Check the hook names against the component's documentation before you use them. The pattern above is typical, but each component publishes its own list, and a hook that exists for one variant may not exist for another. Chapter material on finding hooks covers where to look.
Step 3: Choose the right scope
Because hooks are inherited properties, where you declare them decides how many elements they affect.
One instance
Put the hook on a class that only one element carries. This is the .brand-action example above. Only buttons with that class change.
A whole region
Put the hook on a container. Every matching base component inside the container inherits it.
.compact-region {
--slds-c-button-radius-border: 0.25rem;
}
.compact-region .brand-action {
--slds-c-button-brand-color-background: #014486;
}<template>
<div class="compact-region">
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
<lightning-button label="Cancel"></lightning-button>
</div>
</template>In this template, both buttons get a 0.25rem corner radius, because both are inside .compact-region. Only the Save button gets the darker background, because the more specific selector .compact-region .brand-action matches only it.
How the cascade resolves conflicts
Hooks follow ordinary cascade rules. When two declarations set the same property on the same element, the more specific selector wins. With equal specificity, the later one wins. If a hook is declared on an element and also inherited from an ancestor, the declaration on the element wins, because an inherited value is only used when the element has no value of its own.
The table summarizes the common cases.
| Where the hook is set | What it affects | Typical use |
|---|---|---|
| On the base component's own class | That one instance | A single call-to-action button |
| On a container element | All matching base components inside it | A toolbar or a compact form section |
| On `:host` of your component | Every matching base component in your template | A component-wide look |
| On a more specific selector inside a container | One instance, overriding the container value | An exception within a region |
Setting hooks on `:host`
To apply a hook to everything in your component, declare it on :host. The host is your component's own root element, and all of your template's elements inherit from it.
:host {
--slds-c-button-radius-border: 0.5rem;
}Every lightning-button rendered by your template now has a 0.5rem radius, unless a more specific rule overrides it. A parent component that wraps yours can still set the same hook on your host from outside, and the parent's rule can win depending on specificity and order. Keep that in mind if your component will be reused.
Common errors
The hook is applied correctly in the CSS but nothing changes. Work through these causes in order.
- The name is wrong. Compare each character with the documentation. A typo such as
--slds-c-button-brand-color-backgroudproduces no error. - The hook is not supported by that component or variant. Not every property of every component has a hook. If none exists, hooks cannot do this job. See the chapter on when hooks are not enough.
- The selector does not match. Your CSS file only matches elements in your own template. If the base component is rendered by a child component, your selector cannot reach it. Setting the hook on a container in your template can still work, because the value inherits down through the child's tree.
- A more specific rule wins. Another declaration of the same hook, in your file or a parent's, overrides yours. Inspect the element and see which declaration is crossed out.
- The value is invalid. A color written as
#5a1ba(five digits) or a length without a unit is not valid. The component then behaves as if the hook were unset. - You expected a different property to change. The hook you set controls one thing. A different part of the component, such as its text or its focus ring, needs its own hook.
Applying hooks to other base components
The same process works for any base component that publishes hooks. For a card, for example, you add a class to lightning-card, then set that card's hooks on the class.
<template>
<lightning-card class="summary-card" title="Summary">
<p class="slds-p-horizontal_small">Open cases: 12</p>
</lightning-card>
</template>.summary-card {
--slds-c-card-color-background: #f3f8ff;
--slds-c-card-radius-border: 0.5rem;
}Treat the hook names in this example as a pattern. Confirm exact names for each component in its documentation, because a name that follows the convention may not be one the component actually reads.
Using global hooks in values
You can mix global hooks into your values so a component follows shared design decisions instead of hard-coded colors.
.brand-action {
--slds-c-button-brand-color-background: var(--slds-g-color-brand-base-40, #5a1ba9);
}The second argument to var() is a fallback. If the global hook is not defined in the current context, the browser uses the fallback. This keeps the button styled even when the global hook is missing.
Putting it together
The following component combines a region scope, an instance override, and a :host default.
:host {
--slds-c-button-radius-border: 0.5rem;
}
.compact-region {
--slds-c-button-radius-border: 0.25rem;
}
.compact-region .brand-action {
--slds-c-button-brand-color-background: #014486;
--slds-c-button-brand-color-border: #014486;
}Buttons outside .compact-region get a 0.5rem radius from :host. Buttons inside it get 0.25rem, because the nearer declaration on .compact-region replaces the inherited one. The Save button in the region also gets the darker background and border.
Exercise
A lightning-button has class="save-btn". Your CSS file contains .save-btn { --slds-c-button-brand-color-backgroud: #5a1ba9; } and the button does not change color. What is the problem, and why does the browser not report it?
Show answer
The hook name is misspelled ("backgroud" instead of "background"). A custom property with any name is a valid declaration, so the browser sets it without complaint, but the button's CSS reads the correctly spelled property and never sees your value. Fix the spelling, then check the computed value in developer tools.
Exercise
A page section has .toolbar { --slds-c-button-radius-border: 0 } and a button inside it has the class .pill with --slds-c-button-radius-border: 1rem in the same stylesheet. What radius does that button use, and what does a sibling button without the class use?
Show answer
The .pill button uses 1rem, because the value declared on the element itself wins over the value inherited from .toolbar. The sibling without the class inherits 0 from .toolbar.