Full answer
Shadow DOM gives a component its own tree and its own styles. Page styles do not reach inside, and the component's styles do not leak out. This stops clashes, but it raises a question: how can a page change the look on purpose?
Inside the component, in the shadow root's style:
:hostmatches the element that holds the shadow root, so the component can set its owndisplay, border and padding. A rule from the page, such asmy-card { display: none; }, wins over:host, so the page keeps control.:host(.big)matches the host only when it has the classbig.::slotted(p)styles light DOM elements that were placed into a slot. It matches only direct slotted elements, and page styles still win over it.
Ways for the page to style the inside:
- Custom properties inherit through the shadow boundary, so
--accentset on the host or on the page is readable inside withvar(--accent, teal). This is the cleanest public styling interface. partand::part(): the component marks an inner element withpart="title", and the page styles it withmy-card::part(title) { color: red; }. Only that one element is exposed, with no descendant selectors.- Inherited properties such as
colorandfont-familyalso pass in.
Plan the public surface on purpose: list the custom properties and parts in your documentation, and treat them like an API.
The host gets a crimson border from the page's custom property. The title is underlined by the outside ::part rule. The slotted paragraph is bold from ::slotted, and its colour comes from the page rule, because the slotted element lives in the page's tree.
A plain page rule such as h3 { color: red; } has no effect on the title inside the shadow tree.
<style>
fancy-card { --accent: crimson; }
fancy-card::part(title) { text-decoration: underline; }
p { color: rgb(1, 2, 3); }
</style>
<fancy-card id="c"><p id="slotted">Inside slot</p></fancy-card>
<script>
const root = document.getElementById("c").attachShadow({ mode: "open" });
root.innerHTML = `
<style>
:host { display: block; border: 2px solid var(--accent, teal); }
::slotted(p) { font-weight: 700; }
h3 { margin: 0; }
</style>
<h3 part="title" id="t">Title</h3><slot></slot>`;
</script>Checked in Chrome 156 only. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- :host styles the element that owns the shadow root.
- ::slotted styles direct slotted children.
- Custom properties and inherited properties pass in.
- part and ::part() expose chosen inner elements.
- Page rules beat :host and ::slotted.
Common mistakes
- Expecting page CSS selectors to reach inside a shadow root.
- Using ::part for many internal elements, which exposes the whole structure.
- Forgetting that page styles beat :host and ::slotted rules.
Follow-ups
- Why do custom properties pass through the shadow boundary?
- What does ::slotted match?
Tips
- List the three ways in: custom properties, parts and inherited properties.
- Say the page wins over :host, which keeps the author in control.