Sub topic 12
When Hooks Are Not Enough
This chapter assumes you can apply component hooks (--slds-c-*) in a component's CSS file, and that you know how the shadow DOM limits CSS selectors. Here you will learn where hooks stop, and how to pick another approach.
What hooks can and cannot change
A styling hook is a custom property that a base component chooses to read. The component's author decides which properties get a hook. That has two consequences:
- If a component has no hook for a property, you cannot change that property through hooks.
- Hooks change values (colors, radii, spacing, font sizes). They do not change structure, markup, or which elements exist.
Common gaps include the layout of a component's internal elements, pseudo-elements, hover or focus styles that have no matching hook, and anything the component's author did not expose.
Option 1: SLDS utility classes
Utility classes, such as spacing, alignment, and text helpers, are plain classes that you put on elements in your own template. They work because your template's elements are in your component's shadow tree and SLDS styles reach them.
<template>
<div class="slds-m-bottom_medium slds-text-align_center">
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</div>
</template>Utility classes work on your elements. A class added to lightning-button lands on the host element only, so it cannot restyle the button's inner <button>. Use them for margins, padding, alignment, and visibility around a base component, not inside it.
Option 2: custom CSS on your own markup
For anything you build yourself, write ordinary CSS in your component's CSS file. It is scoped to your template, so it cannot leak out.
.summary-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 0.5rem;
border-bottom: 1px solid var(--slds-g-color-border-base-1, #c9c9c9);
}This example mixes custom layout with a global hook (--slds-g-*) and a fallback value, so the border color still follows the theme.
Option 3: replace the base component
If you need a different structure, build a custom component, or compose one from standard elements. This costs more: you take on accessibility, keyboard handling, and future SLDS changes yourself. Choose it when the design truly differs from what the base component offers.
Things that do not work
Developers often reach for these workarounds. Avoid them.
- Selectors that target a base component's internals. A rule such as
lightning-button button { ... }does not match, because the inner<button>is inside the base component's shadow tree. !importanton hooks. Hooks are inherited values. Fix a scoping problem with a more specific selector instead.- Overriding SLDS class names. Internal class names are not a public contract and can change between releases.
Comparing the approaches
| Need | Best approach | Why |
|---|---|---|
| Change a base component's color, radius or font | Component hook | Supported, upgrade-safe |
| Change one theme value across many components | Global hook | One change, many components |
| Space or align elements around a component | Utility class | No custom CSS to maintain |
| Style elements you wrote | Custom CSS | Scoped to your template |
| Change a base component's internal structure | Custom component | Hooks cannot reach structure |
Deciding in practice
- Look up the component's hooks. If one matches, use it.
- If the need is spacing or alignment, use a utility class on your own wrapper.
- If the elements are yours, write scoped CSS, and use
var()with global hooks where it helps. - If the change needs different markup or behavior, build a custom component.
Exercise
A designer wants the text inside a lightning-button to be uppercase, but the button's documentation lists no hook for text transform. You tried lightning-button button { text-transform: uppercase; } and nothing happened. Why did it fail, and what are two ways to get uppercase labels?
Show answer
The selector fails because the inner button is in the base component's shadow tree, which your CSS cannot select into. Two options: pass an already-uppercase label string from your JavaScript or template (for example, label="SAVE", or use label.toUpperCase() in a getter), or build a custom component with your own <button> element and style it with scoped CSS and slds-button classes.
Exercise
Which approach fits each need? (a) Add space below a card. (b) Change a button's corner radius. (c) Show a two-column layout of your own elements.
Show answer
(a) A utility class such as slds-m-bottom_medium on a wrapper element. (b) A component hook such as --slds-c-button-radius-border. (c) Custom CSS in your component's CSS file, since you own the markup.