Skip to content
Tutorials Center
My learning

Topics

HTML Reference ยท Web Components

The <template> element

Reference for the HTML <template> element: inert markup cloned with JavaScript, the content fragment, declarative shadow DOM with shadowrootmode, and technical details.

The <template> element holds markup that the browser parses but does not render. Its children are placed in a separate DocumentFragment, available as the element's content property: images inside it don't load, scripts don't run and nothing appears on the page. Script clones that fragment whenever it needs a fresh copy, which makes templates ideal for repeated items such as table rows, cards or list entries.

HTML
<template id="row">
  <tr><td class="name"></td><td class="score"></td></tr>
</template>

Try it

Cloning a template

With the shadowrootmode attribute, the parser turns a template into the shadow root of its parent element instead. This is declarative shadow DOM: an encapsulated component with no JavaScript at all.

Declarative shadow DOM

Attributes

Attribute Values Description
shadowrootmode open, closed Makes the parser attach the template's content as a shadow root of the parent element, instead of creating a template. open lets page scripts reach it through element.shadowRoot.
shadowrootdelegatesfocus Boolean Sets delegatesFocus on the declarative shadow root, so focusing the host moves focus to the first focusable element inside.
shadowrootclonable Boolean Sets clonable on the declarative shadow root, so cloning the host also copies its shadow root.
shadowrootserializable Boolean Sets serializable on the declarative shadow root, so getHTML() can include it when asked.

It also accepts the global attributes. The last three attributes only matter together with shadowrootmode.

Usage notes

  • Clone, don't move. Use template.content.cloneNode(true) (or document.importNode(template.content, true)) for each copy. Appending template.content itself moves its nodes out, leaving the template empty.
  • Fill text safely. Set user-supplied values with textContent, not innerHTML, so they can't inject markup.
  • Works almost anywhere. A template may appear in <head>, in <body> or inside a table, and the parser keeps its contents intact. That is why it suits table rows, which would be moved around if written loose in the wrong place.
  • Declarative shadow DOM is parser-only. It is applied when HTML is parsed as a page. Setting innerHTML doesn't create a shadow root; newer APIs such as setHTMLUnsafe() do. The template must be a direct child of the host element.
  • innerHTML on a template reads the fragment. Getting or setting template.innerHTML works on its content, not on regular children.
  • Not a substitute for hiding. For content that is already on the page and should appear later, use the hidden attribute instead.

Technical summary

Content categories Metadata content, flow content, phrasing content, script-supporting element
Permitted content Nothing directly; the parsed children become the content fragment, which may hold any markup
Tag omission None; both the start and end tags are required
Permitted parents Any element that accepts metadata content, phrasing content or script-supporting elements; also a <colgroup> without a span attribute
Implicit ARIA role No corresponding role
DOM interface HTMLTemplateElement
Default display none

Browser support

<template> and its content fragment are supported in every browser. Declarative shadow DOM (shadowrootmode) has been Baseline since 2024. The shadowrootclonable and shadowrootserializable attributes are more recent, so check support if you depend on them.