Sub topic 13
Keeping overrides maintainable
This chapter assumes you can write a Lightning web component with an HTML template, a JavaScript class and a CSS file. It also assumes you know CSS selectors, the cascade and inheritance, and that you have applied component hooks (--slds-c-*) to a base component. Here you will organize those overrides so they stay readable as a project grows.
Why overrides decay
Styling hooks are easy to apply, so they tend to pile up. A typical project ends up with the same hex color in ten files, selectors tied to one page layout, and no record of why a value was chosen. Changing the brand color then means searching every CSS file.
The practices below keep that cost low.
Put values in one place
Repeating a literal value in many rules makes a rebrand a search-and-replace job. Prefer one definition per design decision. Global hooks (--slds-g-*) suit shared decisions such as brand colors. Component hooks suit decisions about one component type.
.compact-region {
--slds-c-button-radius-border: 0.25rem;
--slds-c-button-brand-color-background: #014486;
--slds-c-button-brand-color-border: #014486;
}In this example the brand blue appears twice. If you also use it in other components, store it once in a custom property and refer to it with var().
:host {
--c-brand-blue: #014486;
}
.compact-region {
--slds-c-button-brand-color-background: var(--c-brand-blue);
--slds-c-button-brand-color-border: var(--c-brand-blue);
}Now a change to --c-brand-blue updates both hooks.
Scope as narrowly as the need
Set a hook on the smallest element that needs it. Hooks set on an ancestor reach every matching base component inside it, which is useful for a region but risky when the ancestor is large. A hook set high in the tree can change components you did not intend to touch.
| Need | Where to set the hook |
|---|---|
| One button looks different | A class on that element |
| A group of components shares a look | A wrapper class around the group |
| The whole component | `:host` |
Use a class name that describes the role, such as .brand-action, and not the appearance, such as .purple-button. The role stays true when the color changes.
Prefer hooks over deeper overrides
Hooks are a supported interface, so a hook override keeps working when Salesforce changes a component's internal markup. Selectors that reach into internals do not have that guarantee, and in a Lightning web component they usually do not work at all because of style encapsulation. If a hook exists for your need, use it. Treat anything beyond hooks as a last resort and keep it isolated and commented.
Document the intent
A hook name tells a reader what is being set, but not why. Add a short comment where the reason is not obvious.
/* Meets the 4.5:1 contrast ratio on the grey card background. */
.card-action {
--slds-c-button-brand-color-background: #014486;
}Check after upgrades
Salesforce releases can change which hooks a component supports. After each release, open the pages that use your overrides and confirm they still render as expected. If a hook has no effect, use the browser developer tools to check that the hook name is current and that no closer rule overrides it.
Summary of practices
- Define each shared value once and reuse it with
var(). - Set hooks on the smallest element that needs them.
- Name classes by role.
- Use hooks before any other override technique.
- Comment values whose reason is not obvious.
- Recheck pages after each Salesforce release.
Exercise
A team sets --slds-c-button-brand-color-background: #5a1ba9; in eight component CSS files. Marketing asks for a new brand color. Describe how you would restructure the CSS so the change touches one line, and name one risk of setting the hook on a page-level ancestor.
Show answer
Define the color once, for example as --c-brand-purple on a shared ancestor or as a global hook, and change each file to use var(--c-brand-purple) for the hook value. Changing the color then needs one edit. A hook on a page-level ancestor applies to every matching base component beneath it, so it can restyle buttons that were meant to keep the default look.