# How does CSS styling encapsulation work in LWC, and how can developers override SLDS design tokens?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, CSS Styling, SLDS Tokens

Read it on the site: https://www.forcechallenges.com/interview-questions/how-does-css-styling-encapsulation-work-in-lwc-and-how-can-developers-override-slds-design-tokens

## Answer

**Basics:** CSS in a component's `.css` file applies only to that component's template. Parent styles do not reach into a child's shadow tree and your styles do not leak out. This prevents accidental collisions but means you cannot restyle `lightning-button` internals with ordinary selectors.

**How it works:** Salesforce Lightning Experience loads SLDS globally and uses synthetic shadow by default, which emulates shadow DOM through attribute scoping. Your component can use SLDS utility classes (`slds-m-around_small`) directly. To theme, use CSS custom properties: SLDS styling hooks (`--slds-c-button-brand-color-background`) set on a parent or `:host` flow down into base components that expose those hooks. Older `--lwc-*` design tokens (for example `var(--lwc-brandAccessible)`) are still available in many contexts but styling hooks are the current direction.

**Advanced / deep dive:** Share styles by creating a CSS-only module and importing it with `@import 'c/sharedStyles';`. Use `:host` to style the component root and `::slotted()` for slotted content. Do not rely on `querySelector` plus injected style tags or `.slds-` deep selectors against child internals: with native shadow or Lightning Web Security they fail, and Salesforce can change base component markup. Light DOM (`static renderMode = 'light'`, with `lwc:render-mode="light"` in the template) opts a component out of shadow encapsulation so global CSS applies, at the cost of losing style and DOM isolation; it is aimed at Experience Cloud and theming needs. SLDS is only auto-available on the Salesforce platform; in Lightning Out or Lightning Web Components Open Source you must supply it. For dynamic styling toggle classes via `classList` or computed class getters, and set inline `style` only for dynamic values. Dark mode and branding in Experience Cloud use Theme and branding properties exposed as CSS variables.

**Code / config example:**
```css
/* myCard.css */
:host {
  display: block;
  /* SLDS styling hooks flow into lightning-button inside this component */
  --slds-c-button-brand-color-background: #0b5cab;
  --slds-c-button-brand-color-border: #0b5cab;
}
.card-title { color: var(--lwc-colorTextDefault, #181818); }  /* design token with fallback */
@import 'c/sharedStyles';  /* must appear first in a real file */
```

**Hidden gotchas & must-remember points:**
- You cannot style child component internals with normal selectors; only exposed hooks, `::part` (where available), or the component's own attributes.
- Styling hooks only work on components and properties that document them.
- Parent CSS does not apply to child shadow trees.
- `@import` statements must come before other rules.
- Light DOM removes isolation; global CSS can then affect your component.
- Avoid hard-coding hex values; use tokens or hooks so themes and dark mode work.

**Best practice:** Use SLDS utility classes and styling hooks with fallbacks, share CSS via a module, and avoid brittle selector hacks.

---
Source: ForceChallenges · last updated 2026-11-02
