Scripts & Templates

HTML <template> element

Stores inert template content for later use, or declares a shadow root with supported shadowroot attributes. Ordinary template content is not displayed until instantiated. Ordinary template content is inert and available through the content DocumentFragment until cloned or inserted. Declarative shadow DOM uses shadowrootmode on an eligible template and is processed during parsing; the two uses are not interchangeable. It accepts global HTML attributes; individual global features may be inapplicable or restricted.

Formal Syntax & Structure

html syntaxhtml
<template id="item">Content</template>

Parameters & Attributes

NameTypeDescription
shadowrootmodeenumerated: open, closedMakes an eligible template declaratively create a shadow root on its parent during HTML parsing. open exposes the root through host.shadowRoot; closed does not provide access through that property.
shadowrootdelegatesfocusbooleanEnables focus delegation for a declaratively created shadow root, affecting how focus is forwarded to focusable shadow descendants.
shadowrootclonablebooleanRequests that a declaratively created shadow root can be cloned when its host is cloned or imported through supported DOM operations.
shadowrootserializablebooleanMarks a declaratively created shadow root as eligible for serialization when requested by supported DOM serialization APIs. It does not make ordinary innerHTML include shadow contents.
shadowrootcustomelementregistrybooleanRequests use of a scoped custom element registry setup for a declarative shadow root in supporting implementations. This is an evolving feature; verify the current specification and browser support before use.

Return Value

HTMLTemplateElement

Content model: Template contents are parsed into a separate document fragment for ordinary templates; declarative shadow DOM has distinct processing.

Working Example

Example Snippethtml
<!doctype html><html lang="en">  <head>    <meta charset="utf-8">    <title>HTML example</title>  </head>  <body>    <template id="item">      <p>New entry</p>    </template>    <div id="list"></div>    <script>      document.getElementById('list').append(document.getElementById('item').content.cloneNode(true));    </script>  </body></html>
HTML Reference