HTML Declarative Shadow DOM: shadowrootmode
Declare a root in HTML
During HTML document parsing, template shadowrootmode="open" can create a shadow root on its parent host. Unlike an ordinary template, the parser consumes this template to form the root; it is not left as a child. This permits initial rendering without a component registration script. Use a full document or a parser that explicitly supports declarative roots.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Declarative root</title> </head> <body> <div id="host"> <template shadowrootmode="open"> <p>Ready without script</p> </template> </div> </body></html>The root exists directly after parsing.
Combine initial markup and slots
Declarative roots can contain styles and slots. The light DOM stays available for browsers that do not create the shadow root, so provide meaningful light content as a baseline. The template itself does not display in browsers that treat it as an ordinary template.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Slot card</title> </head> <body> <div id="card"> <template shadowrootmode="open"> <style>section { padding: 8px; }</style> <section> <slot></slot> </section> </template> <p>Study guide</p> </div> </body></html>The guide is distributed into the declarative root.
Preserve a parsed root during enhancement
An upgrade can reuse an existing open declarative root instead of creating another one. Ordinary innerHTML assignment does not instantiate declarative shadow roots. When attachShadow is called with a matching mode on a declarative root, it clears that root, so explicitly reuse shadowRoot when preserving initial content. This class adds text to the existing root.
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <title>Enhance root</title> </head> <body> <study-card> <template shadowrootmode="open"> <p>Initial content</p> </template> </study-card> <script> class StudyCard extends HTMLElement { connectedCallback() { const root = this.shadowRoot || this.attachShadow({mode: "open"}); if (!root.querySelector("small")) { const note = document.createElement("small"); note.textContent = "Enhanced"; root.append(note); } } } customElements.define("study-card", StudyCard); </script> </body></html>Initial content remains and the enhancement is added.
Common mistakes and practice
Test support in target browsers and the actual rendering pipeline. A fragment inserted using ordinary innerHTML is not equivalent to a parsed document. The examples are static full documents for this reason.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Declarative Shadow DOM: shadowrootmode — Knowledge Check
Apply the components concepts taught in this lesson.
What creates a declarative root in the first example?
What happens to the declarative template after root creation?
Does ordinary innerHTML instantiate declarative shadow roots?
How can an upgrade preserve an existing open root?
What provides a useful baseline when declarative roots are unsupported?