Sub topic 6
The CSS file and style scoping
This chapter assumes you know basic CSS selectors, have read the earlier chapters on the HTML template and JavaScript class, and know that a bundle's files share one base name.
What the CSS file does
The CSS file is optional. If it exists, it has the same base name as the bundle folder, for example greetingCard.css. The platform loads it automatically. You don't import it in JavaScript or link it in the template.
.greeting {
color: #032d60;
font-weight: bold;
}This rule styles the <p class="greeting"> in greetingCard.html. It applies to nothing outside that component's template.
How scoping works
Each component renders inside its own shadow tree. The browser, or the LWC engine's synthetic shadow in Salesforce, treats that tree as a boundary for styles. This has two effects:
- Rules in a component's CSS file match only elements in that component's own template.
- Rules from a parent component, or from other CSS, don't match elements inside a child component's template.
You can therefore use a plain class name such as .title in every component without collisions.
Styling the host element
The tag that contains a component, such as <c-greeting-card>, is the host element. The component's own CSS can style it with the :host selector.
:host {
display: block;
margin: 0.5rem;
}
:host(.highlight) {
border: 1px solid #0176d3;
}Custom elements are display: inline by default. Setting display: block on :host is a common first step when a component lays out oddly.
A parent can also style a child's host element from the parent's CSS, because the host element belongs to the parent's template.
c-greeting-card {
width: 20rem;
}Common errors
- A mismatched file name.
greeting-card.cssorGreetingCard.cssin agreetingCardfolder won't be applied. The base names must match exactly. - Styling a child's internals from the parent. A rule like
c-greeting-card .greeting { ... }in the parent matches nothing, because.greetinglives inside the child's shadow tree. To let a parent adjust a child, expose a public property with@apithat changes a class, or use a CSS custom property. - Relying on ID selectors. The engine rewrites
idvalues in templates to keep them unique. Use classes for styling. - Expecting styles on manually inserted markup. Elements added with
innerHTMLunderlwc:dom="manual"aren't styled by the component's CSS file by default.
Crossing the boundary on purpose
CSS custom properties are inherited through shadow boundaries. A child can read a variable that a parent sets.
/* child.css */
.badge {
background: var(--badge-color, #747474);
}/* parent.css */
c-child {
--badge-color: #2e844a;
}The fallback value after the comma applies when the parent sets nothing.
Summary of what to remember
| Question | Answer |
|---|---|
| Where do styles go? | In a CSS file with the bundle's base name |
| How is it loaded? | Automatically, with no import |
| What does it affect? | Only this component's template and its host (via `:host`) |
| How do I pass styling to a child? | A custom property or an `@api` property |
Exercise
A parent component's CSS contains c-user-badge .name { color: red; }, but the name text inside the child c-user-badge stays unchanged. Why, and what is one way to let the parent control the color?
Show answer
The .name element is inside the child's shadow tree, and the parent's CSS can't match elements across that boundary. One fix is to have the child use color: var(--name-color, black); in its own CSS. The parent then sets c-user-badge { --name-color: red; }, because custom properties are inherited through the boundary. Another fix is an @api property on the child that switches a class in the child's template.