Sub topic 12
How SLDS and component CSS interact with the shadow DOM
This chapter assumes you know the structure of a Lightning web component (template, JavaScript class, CSS file) and have used SLDS utility classes such as slds-grid and slds-m-top_small. It explains why some styles reach a lightning-* element and others do not.
Why styles behave differently inside components
Lightning web components render inside a shadow DOM. A shadow DOM is a separate DOM tree attached to an element, and it limits which CSS rules can see which elements. In Lightning Experience, LWC uses a synthetic shadow, which is a JavaScript emulation of the browser's native shadow DOM. The rules below describe the behavior you should rely on in both cases.
Two boundaries matter:
- Your component and its parent. A stylesheet in your component's folder applies only to elements in your component's template. It does not leak out to the parent or to child components.
- Your component and a base component. A selector in your stylesheet can target the
lightning-buttonhost element itself, but it cannot reach the elements rendered inside it.
What SLDS classes can and cannot do
SLDS is a global stylesheet that Salesforce loads into the page. Its classes work on your own markup because the platform makes them available to your template. You can write <div class="slds-grid"> and the grid styles apply.
The same classes do not pass through into a base component's internal markup. If you add slds-button_brand to a lightning-button, the class lands on the host element, not on the <button> inside it. Use the component's own attributes instead, such as variant="brand".
| You want to | Works | Does not work |
|---|---|---|
| Space your own `div` | `class="slds-var-p-around_medium"` | Not applicable |
| Space a base component from outside | `class="slds-m-top_small"` on the host | Not applicable |
| Change a button's look | `variant` attribute or a styling hook | `.slds-button { ... }` in your CSS |
| Restyle an inner element | Not possible from your CSS | `lightning-input input { ... }` |
Styling the host element
Host-level properties such as margin, width, and display do work, because they apply to the lightning-* element itself.
lightning-input {
display: block;
max-width: 20rem;
}This sets the size of the whole input component. It does not change the text box drawn inside it.
Using styling hooks for what is inside
Styling hooks are CSS custom properties that a base component reads. Custom properties inherit across the shadow boundary, which is why they can change internal appearance when ordinary selectors cannot.
lightning-button {
--slds-c-button-brand-color-background: #0b5cab;
}Use hooks for supported changes. Avoid techniques that depend on internal markup, such as global stylesheets that match inner class names. That markup is not part of the public contract, so Salesforce can change it and your styles would silently break.
Common errors
- Selector targets an inner element.
lightning-input input { color: red; }matches nothing. Use a styling hook or a different component. - Expecting the parent's CSS to style a child component. A parent stylesheet can style the child's host element only.
- Hard-coding colors that fight the theme. Prefer hooks and SLDS tokens so the component follows the org's theme.
- Using
slds-classes on custom elements and assuming they match base component output. They only style the markup you write.
Exercise
A developer writes .slds-button { background: green; } in a component stylesheet and places a lightning-button in the template. The button does not change. Explain why, and give two ways to change the button's appearance.
Show answer
The stylesheet is scoped to the component's own template, and the <button> with the slds-button class is rendered inside the base component's shadow tree, so the selector never matches it. Two supported options are to set the variant attribute (for example variant="brand") or to set a styling hook such as --slds-c-button-brand-color-background on the lightning-button host in the component's CSS.
Exercise
Which of these styles can a component stylesheet apply to a lightning-input: max-width on the host, or color on the inner <input> element? Justify your answer.
Show answer
max-width on the host works, because the lightning-input element is part of your template and your CSS can target it. The inner <input> is in the base component's shadow tree, which your stylesheet cannot reach, so a selector for it matches nothing.