Sub topic 2
Why You Cannot Override Base Component CSS Directly
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 SLDS base components such as lightning-button.
The problem
Suppose you want a purple Save button. The first thing most developers try is to target the button's internal markup from their own CSS file.
<template>
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</template>.brand-action button {
background-color: #5a1ba9;
}This rule does nothing. The button element is not in your template. It is rendered inside the base component, and your selector cannot reach it.
Style encapsulation
Lightning web components use shadow DOM to encapsulate styles. Each component has a shadow tree, which is its own private DOM subtree. Two rules follow from this:
- CSS defined in a component's CSS file applies only to elements in that component's template.
- CSS from outside does not apply to elements inside the component's shadow tree. This includes your parent component's CSS.
The next diagram shows where the boundary sits.
The host element, lightning-button with your brand-action class, is part of your template. Your CSS can style it. The elements inside it belong to the base component.
What encapsulation protects
The boundary exists on purpose. It gives you three things:
- Your styles cannot accidentally break a base component.
- Base component styles cannot leak into your markup.
- Salesforce can change a base component's internal markup without breaking your page.
The last point matters most for theming. Internal class names and element structure are not a public API. A selector that works today can silently stop working after a release.
Workarounds that fail
Developers often try these approaches. None is a reliable fix.
| Approach | Why it fails |
|---|---|
| Descendant selectors such as `.brand-action button` | Selectors do not cross the shadow boundary. |
| `!important` on the same rule | The rule never matches, so its priority is irrelevant. |
| Querying inner elements in JavaScript and setting `style` | It depends on internal markup and may be blocked by the platform's DOM isolation. |
| Global stylesheets | They do not pierce the shadow tree in the same way. They also tie your code to internal class names. |
The one thing that does cross the boundary
Inheritance is the exception. Most normal properties set on the host element, such as background-color, do not reach inner elements, because the inner elements set their own values. A few properties, such as color and font-family, inherit by default. They still pass through the shadow boundary.
CSS custom properties always inherit. A custom property is a name that starts with two hyphens and holds a value, for example --brand-purple: #5a1ba9. Because custom properties inherit, a value you set on the host element is visible to every element inside its shadow tree. If the base component reads that property with var(), the value changes the component's appearance.
.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
}You are not styling the inner element. You are setting a value, and the component's own CSS decides where to use it. The component's author decides which values you may set, so the internal markup stays private.
SLDS styling hooks use this mechanism. The next chapters cover what they are and how to read their names.
Summary of the model
- Your CSS reaches the host element but not the shadow tree.
- Inherited values, and custom properties in particular, cross the boundary.
- A base component exposes named custom properties that it reads internally.
- You set those properties on the host element or an ancestor.
Exercise
A developer writes .brand-action button { background-color: #5a1ba9; } in the parent component's CSS file to restyle a lightning-button, and nothing changes. Explain why, and name the mechanism that allows a supported change to work.
Show answer
The button element is inside the base component's shadow tree. Shadow DOM encapsulation prevents the parent's selectors from matching it, so the rule never applies. CSS custom properties inherit into the shadow tree, so setting a supported property (a styling hook) on the host element or an ancestor changes the component, provided the component reads that property with var().
Exercise
Why is it a poor idea to rely on a base component's internal class names, even if you found a way to target them?
Show answer
Internal markup and class names are not a public API. Salesforce can change them in any release, which would silently break your styles.