# \[shadowrootmode\]

> The `shadowrootmode` HTML attribute of the `<template>` element creates a declarative shadow root.

## How do I use declarative shadow DOM with shadowrootmode?

The attribute of the `<template>` element that creates a shadow root for its parent element directly in HTML. Takes 'open' or 'closed' values and allows rendering web components with encapsulated styles on the server without JS.

- Topic: Element Attributes
- Type: attribute
- Browser support: Chrome 111+, Chrome Android 111+, Edge 111+, Firefox 123+, Firefox Android 123+, Safari 16.4+, Safari iOS 16.4+

## Example

Wrong:

```js
const html = '<template shadowrootmode="open"><p>Hi</p></template>';
host.innerHTML = html;
```

Right:

```js
const html = '<template shadowrootmode="open"><p>Hi</p></template>';
host.setHTMLUnsafe(html);
```

`setHTMLUnsafe()` parses declarative shadow roots, while `innerHTML` leaves a plain template.

## Common mistakes

- Setting `innerHTML` with a `<template shadowrootmode>` and getting a plain template.

## Key points

- Markup with declarative shadow roots is inserted with `setHTMLUnsafe()`, not `innerHTML`, which ignores `shadowrootmode`.
- The custom element checks `this.shadowRoot` before calling `attachShadow()`.
- Styles for the component are inside the `<template>`, so they are scoped to the shadow root.

## Interview questions

### Does declarative shadow DOM need JavaScript?

No. The HTML parser creates the shadow root, so server-rendered components show before any script runs.

### What is the difference between open and closed shadowrootmode?

With 'open', `element.shadowRoot` returns the root. With 'closed', it returns `null`.

## Related cards

- [`<template>`](https://wonderium.dev/html/template.md)
- [Shadow DOM](https://wonderium.dev/js/shadow-dom.md)
- [Web Components](https://wonderium.dev/web/web-components.md)
- [.getHTML()](https://wonderium.dev/js/get-html.md)
- [`<slot>`](https://wonderium.dev/html/slot.md)

---

- Level: [HTML](https://wonderium.dev/html.md)
- HTML version: https://wonderium.dev/html/shadowrootmode
- Last updated: 2026-11-25
