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 base components such as lightning-button and lightning-card. Here you will learn why the obvious way to restyle a base component fails, and what that failure shows about how styling hooks work.
The problem: a selector that matches nothing
Suppose you want a square-cornered brand button. Your first attempt is to write CSS against the button element that lightning-button renders:
<template>
<lightning-button
class="brand-action"
variant="brand"
label="Save"
></lightning-button>
</template>/* myComponent.css: this does NOT work */
.brand-action button {
border-radius: 0;
background-color: #5a1ba9;
}
lightning-button .slds-button {
border-radius: 0;
}Neither rule changes the button. No error appears in the console, and the rules are valid CSS. They match nothing because of how Lightning web components handle style encapsulation.
How style encapsulation works
Each Lightning web component renders into its own shadow tree. A shadow tree is a separate subtree of DOM nodes attached to a host element. The browser treats the boundary between the host and its shadow tree as a wall for style rules and for DOM queries.
Two rules follow from this:
- A stylesheet defined by a component applies only to the elements in that component's own template.
- Selectors in an outer component cannot select elements inside the shadow tree of a child component.
When your template includes <lightning-button>, your CSS can see the lightning-button element itself. That element is the host. Your CSS cannot see the <button> that lightning-button creates inside its own template. The selector .brand-action button asks for a button descendant of .brand-action, but the real <button> is in a different tree, so the match fails.
Synthetic shadow and native shadow
Salesforce has run Lightning web components with two shadow DOM implementations. Native shadow DOM is the browser's built-in feature. Synthetic shadow is a JavaScript emulation that Lightning Experience has used to support older browsers and to work with Lightning Locker. In synthetic shadow, the platform adds scoping attributes to elements and rewrites selectors so that component styles stay inside their templates.
You do not need to know which one your org uses to follow this chapter. The scoping rules for your component's CSS are intended to behave the same in both. Treat the boundary as real, even where an implementation detail seems to let a rule leak through. A style that works only by accident of the implementation can break in a later release.
Things that do not work
Developers who hit this wall often try the following. Each attempt fails or is unsupported.
| Attempt | Why it fails |
|---|---|
| `.brand-action button { ... }` | The selector cannot cross into the child's shadow tree. |
| `lightning-button .slds-button { ... }` | Same reason. SLDS class names inside the shadow tree are out of reach. |
| Piercing combinators such as `>>>` or `/deep/` | Lightning web components do not support them. |
| `this.template.querySelector('button')` from JavaScript, then setting `style` | The query runs in your template and cannot see into the child's shadow tree. |
| Global stylesheet overrides | They are fragile and unsupported for base component internals. Structure can change between releases. |
The JavaScript approach is worth a closer look, because it feels like a way around CSS:
import { LightningElement } from 'lwc';
export default class MyComponent extends LightningElement {
renderedCallback() {
// Returns null: the inner button is not in this component's template.
const inner = this.template.querySelector('lightning-button button');
console.log(inner);
}
}this.template.querySelector searches only your own template. The result is null, and any code that depends on the result will fail.
What you can style
Your CSS can still style the host element of a base component. The class you place on <lightning-button> lets you target it:
.brand-action {
margin-inline-start: 0.5rem;
display: inline-block;
}Properties that affect the host itself work. Examples are margin, width, display, and positioning. These change how the host box sits inside your layout. They do not change the parts that the base component draws inside itself, such as the button's background, border, or text.
The boundary has one deliberate opening
Some CSS values pass through the shadow boundary on purpose. These are inherited properties. When an element does not have a value for an inherited property such as color or font-family, it takes the value from its parent. The browser follows this chain through shadow tree boundaries, from the host into its shadow tree.
CSS custom properties are inherited by default, which makes them the same kind of value. If you set --my-value: red on a host element or any ancestor, every element in the shadow trees below can read it with var(--my-value). A component can read the value even though it is not allowed to see your selectors.
This gives a base component author a contract:
- The author writes the component's internal CSS using
var()with a custom property, for examplebackground-color: var(--slds-c-button-brand-color-background, #0176d3). - You set that custom property on the host element, or on any ancestor of it, in your own CSS.
- The browser resolves
var()inside the shadow tree using the inherited value. The fallback after the comma is used only when no value was set.
The author decides which parts are open to change by choosing which values go through var(). You never touch the internal markup. This is the mechanism behind SLDS styling hooks, and later chapters cover the names and the full list.
Here is a preview of the working version of the button from the start of this chapter:
/* myComponent.css: this works */
.brand-action {
--slds-c-button-brand-color-background: #5a1ba9;
--slds-c-button-brand-color-border: #5a1ba9;
--slds-c-button-radius-border: 0;
}Your selector is still .brand-action, which matches the host. Only the values you set on it travel inward. The rule does not need to match anything inside the shadow tree.
Edge cases and common errors
Setting the value on the wrong element
A custom property set on an element reaches only that element's descendants. If you set it on a sibling or on a child of the host, the base component cannot see it. Set the hook on the host element itself, or on an ancestor of the host.
/* Wrong: .toolbar-label is inside the toolbar, not an ancestor of the button */
.toolbar-label {
--slds-c-button-radius-border: 0;
}
/* Right: the hook is on the host */
.brand-action {
--slds-c-button-radius-border: 0;
}Setting a value for a property the component does not read
A custom property has an effect only if some CSS reads it with var(). If you write a misspelled or invented name, the browser accepts it silently and nothing changes. The browser does not validate custom property names. Always copy hook names from the component's documentation.
Expecting the fallback to use your value
The fallback in var(--name, fallback) applies only when --name is not defined. If your ancestor defines the property, the fallback is ignored, even when your value is a poor choice. An empty value such as --name: ; is a defined value and can produce unexpected results, so avoid it.
Using `!important` to force a result
!important on a custom property raises its priority against other declarations of the same custom property. It does not let your outer selectors reach internal elements. Because the problem is the boundary and not specificity, it does not help.
Why Salesforce designed it this way
The restriction protects both sides:
- Component authors can change their internal markup and class names without breaking your customizations, because your CSS never depends on them.
- You get a stable list of named values that Salesforce can document and keep working across releases.
- Styles in one component cannot accidentally change another component on a page that has many authors.
The cost is that you can only change what the author exposed. If a base component has no hook for the property you need, custom properties cannot help. A later chapter on the limits of hooks covers the alternatives.
Summary of the working model
- Your CSS is scoped to your template, so it cannot select internal elements of a base component.
- The host element of a base component is in your template, so you can select it and set properties on it.
- Custom properties inherit through the shadow boundary, so values set on the host or an ancestor reach the base component's internal CSS.
- The base component must read the custom property with
var()for your value to matter.
Exercise
A teammate writes .card-area lightning-button button { border-radius: 0; } in their component's CSS and reports that the button corners do not change. Explain in one or two sentences why, and name the kind of value they should set instead.
Show answer
The button element is inside the shadow tree of lightning-button, and the stylesheet of the outer component cannot select into another component's shadow tree. They should set a CSS custom property (a component styling hook such as --slds-c-button-radius-border) on the lightning-button host element or on an ancestor, because custom properties inherit into the shadow tree.
Exercise
In this CSS, which rule can actually affect the border radius of the button, and why?
.toolbar-title { --slds-c-button-radius-border: 0; }
.save-button { --slds-c-button-radius-border: 0; }
The template contains <div class="toolbar-title">Actions</div> followed by a sibling <lightning-button class="save-button" label="Save"></lightning-button>.
Show answer
Only the .save-button rule works. It sets the custom property on the host element, so the value is inherited into the button's shadow tree. The .toolbar-title element is a sibling, not an ancestor of the button, so its value is never inherited by the button.