CKTIS / HTML
shadowrootmode — declarative shadow DOM
The shadowrootmode attribute on template lets the HTML parser create a declarative shadow root. Its open and closed values determine the access mode of that root.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
<template shadowrootmode="open|closed">…</template>shadowrootmode is an enumerated attribute on template, not a boolean attribute. It accepts open or closed. A missing attribute, an empty value, and an invalid value all mean the None state: this attribute alone then does not trigger shadow-root creation.
During HTML document parsing, a valid declaration creates a shadow root on the parent element if it can host Shadow DOM. The declaration's contents enter the shadow root, while the template itself does not become a DOM child of the host. If the host already has a shadow root or attaching one is disallowed, the parser leaves an ordinary template.
The open mode allows retrieval of the root through host.shadowRoot; for closed, that property returns null. Both modes create Shadow DOM — closed does not mean that the contents will not render.
This is a parser mechanism, not a retroactive switch: adding shadowrootmode to an existing template does not create a shadow root. Assigning the markup to innerHTML does not activate declarative shadow DOM either. Open the example as an HTML document rather than inserting it through innerHTML.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Declarative shadow DOM</title>
<div id="host"><template shadowrootmode="open"><span>Hello</span></template></div>
<pre id="result"></pre>
<script>
const host = document.getElementById('host');
document.getElementById('result').textContent = [
host.shadowRoot.mode,
host.shadowRoot.querySelector('span').textContent,
String(host.querySelector('template'))
].join('\n');
</script>
</html>Result
open Hello null
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.