Where shadow styles stop
You will leave knowing exactly which page styles cross a shadow DOM boundary, which are stopped cold, and which three hooks let styles through on purpose.
One card, two trees
Shadow DOM stops your page's CSS at the host element. No selector written in the document can match a node inside a shadow root, and no selector inside the shadow root can reach back out. Two things still pass through: inherited properties such as color and font-family, and custom properties like --accent. Three deliberate hooks — :host, ::slotted() and ::part() — are the only places the two sides may meet on purpose. When the page must own most of the styling, light DOM is the simpler choice.
Below, the same card exists twice. On the left, the markup lives in a shadow root attached to the card element. On the right, it is ordinary light DOM. Flip the switch to apply a page stylesheet with three rules: one targets the card itself, two target the title and body inside it.
Nested body copy.
Toggle the switch to apply the page stylesheet.Both titles turn peach, because the rule that colors the card element itself crossed the boundary by inheritance — the shadow tree inherits the host's computed values. The underline and the left border land only on the light card, because the selectors .wg-card .t and .wg-card .b cannot see through the wall. The readout compares computed styles in your browser; it does not take my word for it.
</> The boundary is one line of script.Show code
const root = host.attachShadow({ mode: 'open' });
root.innerHTML = `
<style>.t { font: 600 15px var(--sans); }</style>
<div class="t">Nested title</div>
<p class="b">Nested body copy.</p>`;
// page CSS can now style `host`, but not .t or .b insideThe demos attach shadow roots to plain div elements so CSS stays the story. Custom elements are the usual packaging, but the boundary behaves exactly the same on any host.
What walks through the wall
Selectors stop, but values walk through. Inheritance never consults selectors: a shadow tree simply inherits whatever the host element computed — color, font-family, line-height, anything the spec marks as inherited. Custom properties inherit the same way, which is why they became the standard theming channel for web components. The probe below measures all three crossings at once.
Light text, plain inheritance.
--accentToggle any switch; both boxes are compared live.The first three switches change the wrapper around both boxes, never the boxes themselves. The readout compares the shadow box against the light box, computed value against computed value: color and font cross, and --accent drives the pill inside the shadow root with no selector involved. Now add the seal.
One level deeper. The fourth switch adds :host { all: initial } to the shadow root's stylesheet. all resets every standard property to its initial value, and a declared value always beats an inherited one, so color and font-family stop at the door — flip them now and only the light box moves. But all does not touch custom properties: --accent still sails through the seal and the pill still changes. A hard wall means closing your custom properties one by one.
</> Sealing a component against the page.Show code
/* inside the shadow root */
:host { all: initial; display: block; } /* inherited props stop */
:host { --accent: initial; } /* all: never resets these */No selector can aim inside
This is the part specificity cannot fix. A selector written in the document matches nodes of the document tree, and the nodes inside a shadow root are not in that tree. .wg-card .t, * .t, even * itself match none of them. !important does not help either: importance raises a declaration's priority over other declarations, but the rule still has to match an element first.
A light DOM paragraph.
light code nodePick a selector to search both trees.The second number in the readout is not a claim — the script asks host.shadowRoot directly, which is the only way in from script. Notice .t: the page tree finds zero, because that class exists only inside the shadow root. Notice *: the page search reaches the host element and stops; the nodes beyond the host exist only through the shadow root's own query. The host is the one node both trees can see.
One level deeper. For rendering, the browser flattens host, shadow content and slotted nodes into one flat tree — that is why the card paints as a single box. But style matching runs against each tree separately, which is the whole boundary. Events run the other way: a click inside a shadow root is composed: true and bubbles out to the page, so input crosses where styles cannot. We took a close look at that hit-testing side in Where your click really lands.
Encapsulation is not security. With mode: 'open', anyone can walk in through host.shadowRoot, and these demos do exactly that to take their measurements. The boundary organizes CSS. It does not hide content.
:host, the view from inside
The host element is the one node both trees style at once. From inside, :host is how the component paints its own box. From outside, any page selector that happens to match the host works too. When both trees set the same property, the page wins — the cascade treats a shadow root's :host rules as the weaker side.
Toggle either switch; the host is measured live.The first switch applies a page rule that sets the host's background and border — the exact properties :host already sets. The readout confirms the border changed, so the page rule won. The second switch only adds the class compact to the host; the shadow root's own :host(.compact) rule does the rest. That is the standard contract for variants: the page supplies a state, the component decides what it means.
</> The cascade order around :host.Show code
/* component, inside the shadow root */
:host { background: var(--well); padding: 14px; }
:host(.compact) { padding: 6px; }
/* page — wins over :host for the same property */
wg-box.out { background: var(--peach); }:host() only matches the host itself. :host-context() tried to reach up the ancestor chain, but MDN and caniuse now mark it deprecated — use a class on the host or a custom property instead.
::slotted, the front door
Slotted content is light DOM wearing a shadow frame. The nodes stay in the page tree as children of the host, so page CSS keeps styling them, and the component can only reach them through ::slotted(). That pseudo-element matches top-level slotted nodes — never their descendants.
Slotted line, and this bold child sits deeper.
Toggle a switch to style the slotted content.The first switch highlights the whole paragraph, because it is a top-level slotted node. The second switch tries to paint the nested b and never can: the rule is live in the shadow root right now, and the readout's measurement stays at zero effect. The third switch is the twist — a plain page rule reaches that same b instantly, because slotted nodes never left the page. When a page rule and a ::slotted() rule both target the same slotted element, the page's declaration wins.
</> Slot styling has exactly one level.Show code
/* inside the component's shadow root */
::slotted(.lead) { background: var(--peach); } /* top level only */
::slotted(b) { background: var(--teal); } /* matches nothing */
/* page — still matches, the node is still light DOM */
.lead b { text-decoration: underline; }::part, one named window
::part() is the component author deciding, node by node, what the page may style. The author marks an internal element with part="lamp"; the page may then write ::part(lamp) and nothing more. Exposure is opt-in, so the boundary can be opened one window at a time.
Toggle to open the styling hook.The page rule painting the lamp teal is in this page's CSS the whole time. With the switch off it matches nothing; with the switch on the readout's measured background changes, because the author added one attribute. The dot inside the lamp never changes: this page also contains a rule targeting ::part(lamp) .dot, and the browser drops it as invalid — no descendant selectors past a part.
</> The part contract, both sides.Show code
<!-- component author -->
<span class="lamp" part="lamp">…</span>
/* page */
wg-lamp::part(lamp) { background: var(--teal); }
wg-lamp::part(lamp):hover { border-color: var(--on); }
wg-lamp::part(lamp) .dot { color: red; } /* dropped: no descendants */Two more limits from the CSS Shadow Parts spec: pseudo-classes such as :hover may follow ::part(), and parts of a nested shadow root are not visible to the outer page — each boundary has to forward its own parts.
When light DOM wins
Shadow DOM earns its keep for self-contained widgets shipped to teams you do not know: a video player, a combobox, a payment field. It costs you control in exchange. Every visual hook a consumer might need has to be built on purpose — a custom property, a part, a host class — or it does not exist. One theme switch makes the trade visible.
Card body copy.
accentToggle to retheme both cards.The light card is restyled head to toe by one page rule set. The shadow card shifts only where it consumes --hd-bg and --hd-accent; its title has no hook, and the readout says so from measurement, not opinion. That gap is the daily experience of theming third-party shadow components.
So choose by ownership. Use shadow DOM when the component's internals are nobody else's business and you can publish a stable contract of custom properties and parts. Use light DOM for your application's own UI, where the design system's global CSS must reach everything, where content needs to stay findable by the page's own selectors, and where debugging styles should not require opening a shadow root. If the real goal is styling from context rather than globally, half of that job can live in the page already — see Queries that read the box.
Accessibility held throughout: every control above is a real button, the switches expose role="switch" and aria-checked, and the readouts are polite live regions. Nothing animates on this page, so readers who prefer reduced motion get the identical experience.
Default to light DOM for app UI. Reserve shadow DOM for components that must survive contact with stylesheets you have never seen — and when you do ship one, document its custom properties and parts as a public API, because that is exactly what they are.
Quick answers
What is the difference between shadow DOM and light DOM?
Light DOM is ordinary page markup: every node sits in the document tree, so any global stylesheet can select and restyle it. Shadow DOM is a second tree attached to a host element. Selectors written in the document stop at the host and cannot match nodes inside the shadow root, so the component's internals are isolated from page CSS by design.
Can page CSS cross the shadow DOM boundary?
Not selectors — a rule like .card .title in the page matches nothing inside a shadow root. But values cross: inherited properties such as color and font-family, and custom properties like --accent, flow from the host into the shadow tree, because inheritance never consults selectors. Three deliberate hooks — :host, ::slotted() and ::part() — are the only other meeting points.
Why does my page CSS not affect my web component?
The component's markup probably lives in a shadow root, and document selectors cannot match nodes inside it — even !important does not help, because a rule must match an element before importance matters. What still crosses: inherited properties, custom properties, and the :host, ::slotted() and ::part() hooks. Anything else needs a hook the component author published.
Does ::part() let page CSS style inside a shadow root?
Yes, but only where the component author opts in. The author marks an internal element with part="lamp", and the page can then write ::part(lamp) for that element and nothing more. Pseudo-classes such as :hover may follow ::part(), but descendant selectors cannot: a rule like ::part(lamp) .dot is dropped as invalid. Nested shadow roots must forward their own parts.
When should I use shadow DOM instead of light DOM?
Use shadow DOM for self-contained widgets shipped to teams whose stylesheets you have never seen — a video player, a combobox, a payment field — where encapsulation protects the internals. Use light DOM for your application's own UI, where the design system's global CSS must reach everything and debugging should not require opening a shadow root. Shadow components must publish their styling hooks on purpose.