A custom element that renders into the page DOM shares that DOM’s styling and selectors. A shadow root gives the element an internal DOM tree behind a boundary, so page CSS does not accidentally style its internals and internal CSS does not style the page. The element carrying that tree is the shadow host. section 7.8 section 7.8.2
Attach and render
In a custom element constructor, call `this.attachShadow({ mode: 'open' })`. Render internal markup through `this.shadowRoot`, not `this.innerHTML`: writing to the host adds light-DOM nodes, which appear in the shadow tree only when projected through a `<slot>`. Continue using the host for public attributes, properties, and events. section 7.8.1
Make the boundary an explicit design choice:
- Use `open` when code outside the component needs `host.shadowRoot`, including tests that query internal nodes. With `closed`, that property returns `null`; keep the root returned by `attachShadow()` if component code needs a reference. Closed mode does not provide additional style isolation or a security boundary. section 7.8.1 section 9.7.2
- Style the tree inside the shadow root. Use `:host` to style the host, and expose deliberate styling interfaces such as CSS custom properties or `::part` when consumers need customization. section 7.8.3 section 7.13
- Use slots when the host page must supply content. For slot tests, inspect assigned elements rather than inferring projection from outer HTML. section 7.8.1 section 9.7.2
A shadow root can also be declared in HTML with `<template shadowrootmode="open">`. The browser creates it while parsing, before the custom element class upgrades, so initial rendering need not wait for JavaScript. Use `shadowrootmode`, not the older `shadowroot` attribute. section 9.6.1
<product-card>
<template shadowrootmode="open"> #1
<link rel="stylesheet" href="product-card.css">
<div class="row">
<p class="title"><slot name="title">Untitled</slot></p>
<p class="price"><slot name="price">$0</slot></p>
</div>
<p class="muted"><slot name="sku"></slot></p>
<p><slot name="actions"></slot></p>
</template>
<span slot="title">USB-C Camera</span>
<span slot="price">$199</span>
<span slot="sku">SKU: CAM-UC-01</span>
<button slot="actions" type="button">Add to cart</button>
</product-card>
The product-card markup shows a declarative root containing its stylesheet and named slots, with the content supplied by the host. section 9.6.1


