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 or computed class getters, and set inline only for dynamic values. Dark mode and branding in Experience Cloud use Theme and branding properties exposed as CSS variables.