Sub topic 4
Reading hook names
This chapter assumes you can write a basic Lightning web component and read CSS selectors. You should also know what a CSS custom property is and why the shadow DOM stops ordinary selectors at a component's edge. The previous chapters covered both. Here you learn to read a styling hook name and tell what it controls without opening the documentation.
Why names matter
A styling hook is a CSS custom property, so the browser does not validate its name. If you write --slds-c-button-brand-color-backgrund, the browser accepts the declaration and the button stays unchanged. A misspelled hook produces no error and no warning. Reading names correctly is therefore the main defense against silent failures.
SLDS names follow consistent conventions. After you learn them, you can predict most hook names and spot mistakes quickly.
The two prefixes
Every SLDS styling hook starts with --slds- and a single letter that sets its scope.
| Prefix | Meaning | Scope | Example |
|---|---|---|---|
| `--slds-c-` | Component hook | One base component | `--slds-c-button-radius-border` |
| `--slds-g-` | Global hook | A shared design decision used across many components | `--slds-g-color-brand-base-50` |
The c stands for component and the g for global. A component hook changes one thing on one kind of component. A global hook changes a value that many components read, such as a brand color, a spacing step, or a font size.
Do not confuse --slds-c- with --c-. The --slds- namespace belongs to Salesforce. Hooks you define in your own components should use a different prefix. This tutorial uses --c-, as in --c-badge-color-background, which keeps your names out of the SLDS namespace.
Anatomy of a component hook
Component hook names read from general to specific. Take the hook from the earlier example:
--slds-c-button-brand-color-backgroundSplit it on the hyphens and read left to right:
--slds-c-marks a component hook.buttonnames the component. It matches the base componentlightning-button, without thelightning-prefix.brandnames a variant or part of the component. Not every hook has this segment.coloris the property group.backgroundis the specific property in that group.
The pattern is:
--slds-c-{component}-{variant or part}-{property group}-{property}-{state}Only the component and a property are always present. Compare these three hooks on the same component:
| Hook | What it changes |
|---|---|
| `--slds-c-button-radius-border` | Corner radius on every button variant |
| `--slds-c-button-color-background` | Background color of the default button |
| `--slds-c-button-brand-color-background` | Background color of the brand variant only |
The first two have no variant segment, so they apply to the component in general. The third has brand in front of the property, so it affects only variant="brand" buttons. Adding or removing the variant segment changes how many elements a hook affects.
Property groups
The property group tells you what kind of CSS value the hook accepts. These groups appear often:
| Segment | Typical meaning | Value you supply |
|---|---|---|
| `color-background` | Fill color | A color such as `#5a1ba9` |
| `color-border` | Border color | A color |
| `text-color` | Text color | A color |
| `radius-border` | Corner rounding | A length such as `0.25rem` |
| `width-border` | Border thickness | A length |
| `spacing-block-start` | Spacing on an edge | A length |
Segment order is not uniform across groups. Color hooks put color first, as in color-background. Text color is written text-color. Do not guess from one hook that a sibling follows the same pattern. Look up the exact name before you use it.
State suffixes
Hooks for interactive states end with the state name:
--slds-c-button-color-background-hover
--slds-c-button-color-background-active
--slds-c-button-color-border-focusA hook with no suffix sets the resting state. The state hook sets the value while the user hovers, presses, or focuses the element. If you change the resting background and leave the hover hook alone, the button can look wrong on hover. Check each state your design cares about.
Anatomy of a global hook
Global hooks drop the component segment, because they belong to no single component. They begin with a category that says what kind of design decision they hold.
| Hook | Category | Reads as |
|---|---|---|
| `--slds-g-color-brand-base-50` | color | A step on the brand color scale |
| `--slds-g-spacing-4` | spacing | A step on the spacing scale |
| `--slds-g-radius-border-2` | radius | A step on the corner radius scale |
| `--slds-g-font-scale-2` | font | A step on the font size scale |
Global hooks often end in a number. The number is a position on a scale, not a pixel value. The 50 in --slds-g-color-brand-base-50 is a lightness step, and 4 in --slds-g-spacing-4 is the fourth spacing step. Treat the number as an index and look up what it maps to in the documentation. Hooks in the same category share an order, so a higher number generally means a larger step.
Global hooks can change over time as the SLDS design system evolves, so confirm the exact names against the current documentation before you rely on them.
How the two kinds relate
Component hooks sit on top of global hooks. A base component's own CSS reads a component hook first. If you have not set it, the component falls back to a global value or to its built-in default. The chain looks like this:
The practical rule is to choose the narrowest hook that matches your intent. To restyle one component, set a component hook. To change a decision across your whole app, such as a brand color, set a global hook and let components pick it up. Setting a global hook for a one-off change spreads the effect to components you did not mean to touch.
Reading names in practice
Combine the rules to decode an unfamiliar hook. Suppose you find --slds-c-card-color-background in a stylesheet.
--slds-c-means it is a component hook, so it affects one component type.cardis the component,lightning-card.color-backgroundis the property, a color.- No state suffix, so it is the resting state.
You can now predict what it does: it sets the card's background color. You can also predict how to write the value, which is any valid CSS color.
The same decoding catches mistakes. In this snippet, each line has a problem.
.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
--slds-c-button-background-color: #5a1ba9;
--SLDS-c-button-radius-border: 1rem;
--slds-button-radius-border: 1rem;
}- Line 1 is well formed.
- Line 2 reverses the property segments. It is
color-background, notbackground-color, so the hook does not exist and nothing happens. - Line 3 uses uppercase letters. Custom property names are case sensitive, so
--SLDS-c-...is a different property and has no effect. - Line 4 is missing the
cscope letter, so it matches no SLDS hook.
None of these lines raises an error. Only the first one changes the button.
Parsing a hook name in code
A small script makes the structure concrete. This one splits a hook name into its scope and segments and reports an unrecognized prefix. Run it with Node.js.
function describeHook(name) {
const match = /^--slds-([cg])-(.+)$/.exec(name);
if (!match) {
return { valid: false, reason: "Does not start with --slds-c- or --slds-g-" };
}
const [, letter, rest] = match;
return {
valid: true,
scope: letter === "c" ? "component" : "global",
segments: rest.split("-"),
};
}
const names = [
"--slds-c-button-brand-color-background",
"--slds-g-spacing-4",
"--slds-button-radius-border",
"--SLDS-c-button-radius-border",
];
for (const name of names) {
console.log(name, "=>", JSON.stringify(describeHook(name)));
}The script can only check the prefix and split the rest. It cannot tell whether background-color exists for a given component. The documentation is the source of truth for that, which the later chapter on finding hooks covers.
Summary of the reading rules
--slds-c-is a component hook.--slds-g-is a global hook.- Component hooks read as component, optional variant or part, property group, property, optional state.
- A variant segment such as
brandnarrows the hook to that variant. - A state suffix such as
hoverapplies only in that state. No suffix means the resting state. - Global hooks name a category and often end in a scale number, which is an index and not a unit.
- Names are case sensitive, and a wrong name is ignored without any message.
- Your own hooks should use a separate prefix such as
--c-.
Exercise
A teammate writes --slds-c-button-brand-color-background-hover: #014486; and says it should change the background of every button on hover. Decode the name. Is the teammate right?
Show answer
No. The c makes it a component hook for button. The brand segment limits it to buttons with variant="brand", so default and neutral buttons are not affected. color-background is the property and hover is the state, so it applies only while the pointer is over a brand button. To change every button on hover, use the hook without the brand segment, --slds-c-button-color-background-hover, after confirming it in the documentation.
Exercise
You set --slds-c-card-background-color: #f4f6f9; on a wrapper element and the card does not change. No error appears. What is the likely cause, and how do you fix it?
Show answer
The name reverses the property segments. SLDS uses color-background, so the hook --slds-c-card-background-color does not exist and the browser ignores it. Correct it to --slds-c-card-color-background, and confirm the exact name in the card's documentation.
Exercise
You want every base component in your app to use a different brand color. Should you set many component hooks or a global hook, and why?
Show answer
Set a global hook such as --slds-g-color-brand-base-50, checking the current name in the documentation. Components that read that global decision pick it up, so one change covers the app. Setting individual component hooks would need many declarations and would miss components you forgot.