Sub topic 12
How SLDS and component CSS interact with the shadow DOM
This chapter assumes you can read a Lightning web component (HTML template, JavaScript class, CSS file). You should also know how lightning-* base components are used and what SLDS utility classes look like, such as slds-grid and slds-var-p-around_medium. Here you will learn why some CSS reaches an element and some does not, and which tools to use when it does not.
Why styles stop at the component boundary
Lightning web components render inside a shadow tree. Each component has its own boundary, and CSS does not cross it in either direction. Two rules follow from this:
- A selector in your component's CSS file matches only elements in your own template.
- A selector cannot reach elements inside the template of a child component, including the internals of a
lightning-*base component.
This is why a rule such as .slds-button { background: red; } in your stylesheet does not recolor a lightning-button. The real <button> element lives inside the base component's own shadow tree. Your selector can only see the <lightning-button> host element.
The boundary also protects you. Your .title class cannot leak into a sibling component, and another component's .title cannot leak into yours.
Note that "shadow DOM" has two implementations on the platform. Lightning Web Security and Lightning Locker affect how the platform runs your code, and base components may use synthetic shadow in some orgs. In synthetic shadow, the boundary is emulated by the framework rather than the browser. The styling rules in this chapter hold in both cases, but the DOM you see in developer tools can differ.
What your component CSS can style
Your stylesheet can style two kinds of elements:
- Elements you wrote in your template, such as
div,pandspan. - The host element of a child component, such as
lightning-buttonorlightning-input.
The second kind is useful for layout. The host element is a normal element in your tree, so you can set its margin, width, display and similar properties.
lightning-input {
display: block;
max-width: 20rem;
}
lightning-card {
margin-bottom: 1rem;
}These rules change how the host sits in your layout. They do not change the contents inside it. A color set on the host can pass through to the content because some properties inherit across the boundary, but properties such as background, border and padding on the inner elements stay under the base component's control.
Inheritance crosses the boundary
Some CSS properties are inherited by child elements. Inherited values do cross a shadow boundary. These include color, font-family, font-size, line-height and text-align. If you set font-size on a wrapper div around a base component, the text inside can pick it up, unless the base component sets its own value.
Non-inherited properties such as border, margin and background do not cross. CSS custom properties (those starting with --) are inherited, and this is the mechanism that styling hooks use.
How SLDS classes fit in
SLDS is a stylesheet that the platform loads for you in Lightning Experience and in Experience Cloud sites that use the Lightning template. Its classes are available inside your component templates. That is how slds-grid and slds-var-p-around_medium work on your own div elements without an import.
Two points matter here:
- SLDS classes style elements in your own template, because the class and the element share the same scope.
- SLDS classes written on a child component's inner elements are not something you control. Base components apply the SLDS classes they need inside their own templates.
This leads to a common mistake. Adding class="slds-button slds-button_brand" to a lightning-button does not make an SLDS button. The host element is not a <button>, and the real button inside is built by the base component. Use the variant attribute instead, for example variant="brand".
<template>
<div class="slds-var-p-around_medium slds-text-heading_small">
Summary
<lightning-button
class="slds-var-m-left_small"
variant="brand"
label="Refresh"
onclick={handleRefresh}
></lightning-button>
</div>
</template>In this template, the classes on the div style the div. The slds-var-m-left_small class on lightning-button applies a margin to the host element, which works because margin affects how the host sits next to its siblings. The variant attribute is what changes the button's look.
Styling hooks: the supported way to change base components
Since your CSS cannot reach inside a base component, Salesforce exposes styling hooks. A styling hook is a CSS custom property that a component reads when it draws itself. Because custom properties are inherited, a value you set on the host reaches the internals.
lightning-button {
--slds-c-button-brand-color-background: #0b5cab;
}Notes on using hooks:
- Hook names follow the pattern
--slds-c-<component>-<part>-<property>. Check the component's documentation for the exact list. A name that is wrong does nothing and raises no error. - Hooks apply only to the properties the component author chose to expose. If there is no hook for the thing you want to change, you cannot change it this way.
- You can set a hook on a wrapper element to affect several components inside it at once, since the value is inherited.
.compact-form {
--slds-c-input-spacing-block-start: 0.25rem;
}If every lightning-input inside an element with class compact-form reads that hook, all of them change together. Always check that the hook exists for the component you target.
Things that do not work
These approaches are common among developers new to the platform. Each fails for a specific reason.
| Attempt | Why it fails | Better option |
|---|---|---|
| `.slds-input { ... }` to restyle a `lightning-input` | The inner `<input>` is in another shadow tree | Use a styling hook, or build a custom input |
| `lightning-input input { ... }` | Descendant selectors do not pierce the boundary | Same as above |
| `class="slds-button"` on `lightning-button` | The host is not the button | Use `variant` and `label` |
| `::part` or `>>>` style piercing | Base components do not offer shadow parts or deep selectors for this | Use hooks |
| Querying inner elements with `this.template.querySelector` | The query stops at the boundary of your template | Use public properties and methods of the child |
Do not try to work around the boundary by injecting a global style tag or by reaching into another component's shadow root from JavaScript. Internal markup is not a public API. It changes between releases, and code that depends on it breaks without warning. Under Lightning Web Security this kind of access is also restricted.
Scoping within your own component
Your CSS file is scoped to your component. A rule like p { color: gray; } affects only p elements in your template, never in other components. This means you can use short class names without worrying about collisions.
It also means you cannot share a stylesheet by name across components. To reuse styles, create a CSS-only module and import it into each component's CSS file.
/* in cssLibrary/cssLibrary.css */
.panel-title {
font-weight: 700;
border-bottom: 1px solid #c9c9c9;
}/* in myComponent/myComponent.css */
@import 'c/cssLibrary';The imported rules are scoped to the importing component, the same as if you had written them there.
Slotted content and scoping
Content that you pass into a child's slot belongs to your template, not to the child's. Your CSS therefore styles it, even though it is displayed inside the child.
<template>
<lightning-card title="Notes">
<p class="note">Remember to confirm the address.</p>
</lightning-card>
</template>.note {
color: #5c5c5c;
font-style: italic;
}The .note rule works because the p element is owned by your template. The lightning-card only decides where it is displayed. In the same way, a lightning-button placed in the card's actions slot is yours to style at the host level.
Debugging style problems
When a style does not apply, work through these checks:
- Is the target element in your own template? If it is inside a base component, only hooks and host-level rules can work.
- Is the hook name spelled exactly as the documentation lists it?
- Does a more specific rule from the base component override yours? Inspect the element in browser developer tools and look at the computed styles.
- Is the property inherited? If you expect it to pass through, confirm that the base component does not set its own value.
- Are you testing in a context where SLDS is not loaded, such as a Jest test or local preview? Classes may then have no visual effect.
Summary of the rules
- Your CSS styles your template and the host elements of children.
- Inherited properties and custom properties cross the boundary. Other properties do not.
- SLDS classes work on elements you own. For base components, use attributes such as
variantand styling hooks. - Slotted content is yours to style.
- Share styles with a CSS module and
@import.
Exercise
A teammate adds .slds-input { border-color: red; } to the component CSS and expects a lightning-input to get a red border. Nothing changes. Explain why, and name one supported way to change the look of that input.
Show answer
The inner <input> element that carries slds-input is in the base component's shadow tree, and a selector in your component's CSS cannot reach it. Your stylesheet only matches elements in your own template and the host <lightning-input> element. The supported approach is to set a styling hook, a CSS custom property documented for the component, on lightning-input or on a wrapper element. Because custom properties are inherited, the value reaches the inside of the component. If no hook exists for the border, use a custom input element that you build and style yourself.
Exercise
In the template below, which rule applies, and why? Template: <lightning-card title="T"><p class="hint">Text</p></lightning-card>. Stylesheet: .hint { color: green; }
Show answer
The rule applies. The p element is part of your template, even though it is displayed inside the card's slot. Elements owned by your template are in your style scope, so your CSS matches them.