How to Style Web Components From Outside
Style web components from outside with CSS custom properties, ::part, ::slotted, :host and adoptedStyleSheets, and design a stable styling API.

Shadow DOM gives a web component a private tree and private styles. That is the whole point: the page's CSS cannot accidentally break the component, and the component's CSS cannot leak onto the page. It also means that the most natural thing a page author wants to do, changing the color of a button inside a component, does nothing with a normal selector. The platform provides specific, deliberate openings in the boundary: inherited properties, CSS custom properties, the part attribute with the ::part() pseudo-element, slotted content that stays in the page's own tree and constructable stylesheets for sharing rules. Knowing which to expose, and which to keep closed, is the craft of a good component. This guide shows how to style web components from outside: what the shadow boundary blocks, what passes through, custom properties as the main theming mechanism, part and ::part(), styling slotted content, the :host and :host-context() selectors from the inside, sharing and overriding styles with adoptedStyleSheets, state styling with custom states, designing the styling API and testing. The features are documented on MDN and are supported by all current browsers.
What the boundary blocks
A selector in the page cannot reach into a shadow tree. With a component that contains a button inside its shadow root, a page rule for button styles every button on the page except that one.
class ActionBar extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" }).innerHTML = `
<style>button { padding: 0.5rem 1rem; border: 0; background: #444; color: #fff; }</style>
<button>Save</button>`;
}
}
customElements.define("action-bar", ActionBar);button {
background: crimson;
}(the Save button inside <action-bar> stays dark grey; the page rule does not reach it)The same applies in the other direction: the <style> inside the shadow root affects only that tree. This is the encapsulation, working as designed. The tools below are how a component author lets some control back out.
What passes through: inheritance
Inherited CSS properties cross the boundary, because inheritance follows the tree from the host element down into its shadow tree. Font family, font size, text color, line height, direction and visibility set on or above the component reach the text inside it, unless the component sets its own. So a page can change the typography of a component without any special support.
action-bar {
font-family: "Inter", sans-serif;
color: #222;
font-size: 1.125rem;
}(text inside the shadow tree uses the page's font, color and size unless the component overrides them)Properties that are not inherited, such as background, padding and border, do not pass this way. A component author who wants the typography to follow the page should avoid setting font-family inside, or should use font: inherit on controls, since form elements such as <button> and <input> do not inherit fonts by default from their parent in browsers.
Custom properties: the main API
CSS custom properties are inherited, so they pass through the boundary, and they are the most flexible way to theme a component. The component reads variables with fallback values, and the page sets them on the element or on any ancestor.
class ActionBar extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" }).innerHTML = `
<style>
:host { display: inline-block; }
button {
padding: var(--action-bar-padding, 0.5rem 1rem);
border: 0;
border-radius: var(--action-bar-radius, 4px);
background: var(--action-bar-bg, #444444);
color: var(--action-bar-fg, #ffffff);
font: inherit;
}
button:hover { background: var(--action-bar-bg-hover, #222222); }
</style>
<button><slot>Save</slot></button>`;
}
}
customElements.define("action-bar", ActionBar);action-bar {
--action-bar-bg: #0b5fff;
--action-bar-bg-hover: #0847bd;
--action-bar-radius: 999px;
}
.dark action-bar {
--action-bar-bg: #7aa7ff;
--action-bar-fg: #101010;
}(the button becomes blue and pill shaped, and changes in the dark context)Because the variables cascade, setting them on :root themes every instance, and setting them on a container themes its descendants, which makes dark mode and per-section themes easy. Choose a clear naming convention with a prefix for the component, document every variable and its default, and treat them as public API: renaming one breaks every page that uses it. Prefer a small set of meaningful variables over one per CSS property.
Expose pieces with part and ::part()
When a page needs to style an element in ways variables do not cover, the author can mark the element with a part attribute. The page then targets it with ::part(name) on the host, which can carry any property that fits, along with pseudo-classes such as :hover.
class ActionBar extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" }).innerHTML = `
<style>button { padding: 0.5rem 1rem; }</style>
<div part="container">
<button part="button"><slot>Save</slot></button>
<span part="badge" hidden></span>
</div>`;
}
}
customElements.define("action-bar", ActionBar);action-bar::part(button) {
background: crimson;
color: white;
text-transform: uppercase;
}
action-bar::part(button):hover {
background: darkred;
}
action-bar::part(badge) {
border-radius: 50%;
}(the page styles the button and the badge by their part names)There are limits that keep the encapsulation meaningful. ::part() selects a single part, so you cannot chain ::part(a) span to reach its descendants, and you cannot target structural pseudo-classes such as :first-child on it. The page controls only the elements the author chose to name. An element may have several part names, separated by spaces, to group elements: part="button primary". To make parts of a nested component available through the outer one, use exportparts="inner-name: outer-name" on the inner host. Treat part names as public API in the same way as variables.
Style the host from inside
From within the shadow tree, the :host selector refers to the element that owns the shadow root, and it is where a component sets its defaults, such as display, which is inline for custom elements unless told otherwise. A function form, :host(.selected) or :host([disabled]), applies only when the host matches the selector, which lets the component respond to attributes and classes the page sets.
:host {
display: inline-block;
}
:host([disabled]) {
opacity: 0.5;
pointer-events: none;
}
:host(:focus-visible) {
outline: 2px solid var(--action-bar-focus, #0b5fff);
}(the host reacts to the disabled attribute and to keyboard focus)Styles that the page applies to the host element from outside have higher priority than :host rules inside, since outer styles win in the cascade for the host. So action-bar { display: block; } in the page overrides the component's :host { display: inline-block; }, which is a useful property: defaults inside, overrides outside. The :host-context(.dark) selector, which matches when an ancestor of the host does, exists in some engines but is not available in all, so use custom properties for context-dependent themes.
Slotted content stays in the page
Content that the page author puts between the tags and assigns to a <slot> remains in the page's own DOM, rendered inside the shadow tree. So the page's regular CSS styles it, and the component can add some rules with ::slotted(), which matches elements assigned to a slot but only the direct slotted elements, not their children.
const template = document.createElement("template");
template.innerHTML = `
<style>
::slotted(h2) { margin: 0 0 0.5rem; font-size: 1.1rem; }
::slotted(p) { color: #555; }
</style>
<slot></slot>`;(the component styles slotted headings and paragraphs, and the page can still override with its own selectors)Page styles for slotted content have higher priority than ::slotted() rules from the shadow tree, which again lets the page win, so the component sets good defaults. This makes slots the most natural place for free-form content that authors want to style with their normal CSS.
Share and override styles with constructable stylesheets
Repeating a <style> block in every instance parses the CSS many times. A constructable stylesheet is created once from script and shared by every shadow root with adoptedStyleSheets, which is faster and allows a page to add a stylesheet to a component from outside, if the component has an open shadow root.
const sheet = new CSSStyleSheet();
sheet.replaceSync(`
button { padding: 0.5rem 1rem; background: var(--action-bar-bg, #444); color: #fff; }
`);
class ActionBar extends HTMLElement {
constructor() {
super();
const root = this.attachShadow({ mode: "open" });
root.adoptedStyleSheets = [sheet];
root.innerHTML = "<button><slot>Save</slot></button>";
}
}
customElements.define("action-bar", ActionBar);
const theme = new CSSStyleSheet();
theme.replaceSync("button { border-radius: 999px; }");
const bar = document.createElement("action-bar");
document.body.append(bar);
bar.shadowRoot.adoptedStyleSheets = [...bar.shadowRoot.adoptedStyleSheets, theme];
console.log(bar.shadowRoot.adoptedStyleSheets.length);2Reaching into a shadow root from outside breaks encapsulation on purpose, and ties the page to the component's internal structure, so use it as an escape hatch for application code that owns both sides, and prefer variables and parts for libraries.
Style by state with custom states
Components often have states beyond attributes, such as "loading" or "expanded". The CustomStateSet, reached through ElementInternals with internals.states, lets the component expose a state that the page can match with a pseudo-class written :state(name), in current browsers.
class ToggleCard extends HTMLElement {
#internals = this.attachInternals();
set open(value) {
if (value) this.#internals.states.add("open");
else this.#internals.states.delete("open");
}
}
customElements.define("toggle-card", ToggleCard);toggle-card:state(open) {
border-color: #0b5fff;
}(the page can style the open state without the component adding a class or attribute)Where :state() is not supported by your audience, reflect the state as an attribute and select it with [open].
Design the styling API
Decide what you promise. A reasonable layered design: expose a handful of custom properties for the common theme choices, such as colors, radius, spacing and font; expose parts for the few elements that authors need full control over; use slots for content; and keep everything else private so you can change it. Document each variable with its default and the parts with their meaning, in the same place as the attributes and events. Avoid exposing every internal element as a part, because each one becomes something you can never rename. Test the component in light and dark pages, with a different font set on the page and inside containers with different inherited colors, since inheritance is part of the contract. Make sure focus indicators remain visible when the page theme changes, and keep contrast ratios acceptable with the defaults.


