Sub topic 15
Encapsulation and CSS Scoping in Practice
This chapter assumes you know how a component bundle is laid out, how a template binds to class fields, and what a shadow root is. It shows how shadow DOM encapsulation affects CSS and DOM queries in an LWC component, and where it causes surprises.
How scoping works
Each LWC component renders into its own shadow root. Styles in the component's CSS file apply only to elements in that component's template. Styles from the page, or from other components, do not reach in. Your styles do not leak out either.
The CSS file sits in the bundle next to the template and has the same base name.
/* helloGreeting.css */
p {
color: darkgreen;
font-weight: bold;
}This rule colors only the <p> inside helloGreeting. A <p> in a parent or sibling component is unaffected. Because of this, simple selectors such as p or .title are safe, and you do not need naming conventions like BEM to avoid collisions.
What does cross the boundary
Some things still pass through the shadow boundary.
- Inherited properties. Properties such as
color,font-familyandline-heightinherit from the host element down into the shadow tree. A parent can setfont-familyand the child text follows it, unless the child sets its own. - CSS custom properties. Variables like
--accentpass through. This is the standard way to let a parent theme a child. - The
:hostselector. It styles the component's own host element from inside its CSS.
/* statusBadge.css */
:host {
display: inline-block;
}
span {
background: var(--badge-color, gray);
padding: 0.25rem 0.5rem;
}A parent sets the variable on the child's tag:
/* parentPanel.css */
c-status-badge {
--badge-color: seagreen;
}The var(--badge-color, gray) fallback means the badge still works when no parent sets the variable.
DOM queries are scoped too
Encapsulation applies to JavaScript as well. Inside a component, use this.template.querySelector() to find elements in your own template.
import { LightningElement } from 'lwc';
export default class FocusField extends LightningElement {
focusInput() {
const input = this.template.querySelector('input');
if (input) {
input.focus();
}
}
}document.querySelector('input') would not find that input, because the global document cannot see into shadow roots.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Page CSS does not style your component | The shadow root blocks outside selectors | Move the rule into the component's CSS or use a custom property |
| `querySelector` returns `null` | The element is not rendered yet, or you used `document` | Query after render, through `this.template` |
| Parent cannot style a child's inner element | Child internals are private | Have the child expose a CSS custom property |
| `:host` rule has no effect | The rule is in the parent's CSS file | Put `:host` in the component that owns the host element |
Exercise
A parent component's CSS contains p { color: red; }. It renders a child component whose template contains a <p>. Will the child's paragraph turn red? How could the parent change that color on purpose?
Show answer
No. The parent's rule applies only inside the parent's shadow root, so it does not reach the child's <p>. The child should define its color with a CSS custom property, for example color: var(--text-color, black);. The parent can then set --text-color on the child's tag, because custom properties cross the shadow boundary.