Styling Web Components
CSS inside a shadow root is fully scoped — page styles don’t bleed in, shadow styles don’t bleed out. But three mechanisms cross the boundary BY DESIGN: :host / ::slotted / ::part selectors, and CSS custom properties. Understanding which rules stay isolated and which ones intentionally pierce the shadow boundary is the foundation of robust web-component theming.
flowchart LR
subgraph Page
PA[".btn color red"]
PB["::part(label)"]
PC["--theme-color blue"]
end
subgraph Shadow
SA[":host styles"]
SB["::slotted(span)"]
SC["var(--theme-color)"]
SD["internal .btn"]
end
PA -. "blocked" .-> SD
PB --> SC
PC --> SC
This module covers four topics that together give you complete control over shadow-DOM styling:
:hostselectors — style the custom element itself from inside its shadow root::slotted— target light-DOM children that land in a slot::partandexportparts— expose named internals so page CSS can style them- CSS custom properties and Constructable Stylesheets — the most flexible theming approach