Sub topic 12
Shared CSS modules
This chapter assumes you know the layout of a component bundle and how a component's own CSS file is scoped to its template. Here you will see how several components can reuse one stylesheet.
The problem
Each component's CSS file applies only to that component's template. Two components that need the same button or card styles would otherwise copy the rules. When the design changes, you would have to edit both copies.
A shared CSS module fixes this. It is a bundle that contains only a CSS file, and other components import it.
Create the module
Make a folder under lwc and put one CSS file in it. The file's base name matches the folder name, as with any bundle.
force-app/main/default/lwc/
├── sharedStyles/
│ └── sharedStyles.css
└── greetingCard/
├── greetingCard.html
├── greetingCard.js
└── greetingCard.cssThe shared file holds ordinary rules:
.card-title {
font-size: 1.25rem;
color: #032d60;
}
.card-note {
color: #444444;
font-style: italic;
}The bundle has no HTML or JavaScript file, so it is not a component. You cannot place it on a page or use it as a tag.
Import it
In the CSS file of a consuming component, use @import with the module name. The module name is the namespace, a slash, and the folder name. For code in your own org or project, the namespace is c.
@import 'c/sharedStyles';
.card-body {
padding: 1rem;
}Put @import statements at the top of the file, before other rules. This follows the normal CSS rule.
Now the template can use the shared classes:
<template>
<div class="card-body">
<h2 class="card-title">Hello</h2>
<p class="card-note">Shared styles in use.</p>
</div>
</template>How scoping still works
The imported rules are applied as if you had written them in the importing component's own CSS file. They affect only that component's template.
- Each importing component gets the rules for its own template.
- The styles do not pass into child components. Their templates have their own scope.
- Editing
sharedStyles.csschanges every component that imports it.
Common errors
| Symptom | Likely cause |
|---|---|
| Import fails to resolve | The folder name and file base name differ, or the module name has the wrong case. |
| Styles never apply | The import is in the HTML or JavaScript file instead of the CSS file. |
| Styles apply in the parent but not in a child | Styles do not cross component boundaries. Import the module in the child as well. |
| Rules below the import are ignored | A syntax error, or `@import` is placed after other rules. |
When to use one
Use a shared CSS module when two or more components repeat the same rules, such as spacing, headings or brand colors. For styles used by one component, keep them in its own CSS file. Splitting a single component's rules into a separate module only adds a file to maintain.
Exercise
You create lwc/sharedStyles/sharedStyles.css and import it in parentCard.css with @import 'c/sharedStyles';. The parent's styles work, but the childBadge component inside it does not pick up the shared rules. Why, and what is the fix?
Show answer
Each component's styles are scoped to its own template, so styles imported by the parent do not reach the child's template. Add @import 'c/sharedStyles'; at the top of childBadge.css as well.