HTML / Element / Initial coverage

The <template> Element

Status: Initial coverage Scope: HTML Living Standard Spec checked: 2026-09-19

Technical Summary

The template element declares an HTML fragment that can be cloned and inserted into a Document by script. The element represents nothing in rendering, and its contents are held in associated Template Contents, a DocumentFragment, rather than ordinary children of the element.

Deep-cloning template.content and inserting the resulting fragment moves its descendants into the normal Document tree. With shadowrootmode, the same element participates in Declarative Shadow DOM and must be analyzed with parser, shadow-tree, slot, focus, and accessibility boundaries.

Definition / Categories

ItemDefinition
MeaningA declaration of an HTML fragment that can be cloned and inserted into a Document by script
CategoriesMetadata content, Flow content, Phrasing content, Script-supporting element
ContextsWhere metadata, phrasing, or script-supporting content is expected; also the special colgroup context without a span attribute
Content modelNothing. Markup written between the tags becomes Template Contents
Tag omissionNeither the start tag nor the end tag is omissible
Content attributesGlobal attributes, for, shadowrootmode, shadowrootdelegatesfocus, shadowrootserializable, shadowrootslotassignment, shadowrootclonable, and shadowrootcustomelementregistry
DOM interfaceHTMLTemplateElement; content is a readonly DocumentFragment

Template Contents and the DOM tree

When the HTML parser processes a template, its markup is not treated as ordinary children of the template element. It is placed in the Template Contents returned by template.content.

<template id="card-template">
  <article>
    <h2>Card</h2>
  </article>
</template>

const template = document.querySelector('#card-template');
template.childNodes.length;         // 0
template.content.childNodes.length; // 1
template.content.firstElementChild.nodeName; // "ARTICLE"

This is more than a CSS visibility choice. Template Contents have their own owner-document and insertion processing, so parser behavior, DOM manipulation, adopting, and custom elements must be considered together.

Clone, insert, and ID boundaries

content is a DocumentFragment. Clone it deeply, update references in the clone, and then insert it into the Document. Reusing a template also requires an explicit plan for duplicate IDs, label/for, ARIA references, and form-control names and values.

const template = document.querySelector('#card-template');
const fragment = template.content.cloneNode(true);
const heading = fragment.querySelector('h2');
heading.textContent = 'A cloned card';
document.querySelector('#cards').append(fragment);

Clone as many times as needed rather than trying to insert the same node repeatedly. When external data is not intended to be parsed as HTML, set it with textContent; the template element is not a sanitizer.

Rendering, instantiation, and accessibility

The template element represents nothing in rendering. Its contents are not live page content for users until they are instantiated into the normal Document tree. After insertion, the resulting headings, labels, buttons, form controls, and ARIA relationships become the subjects of ordinary semantic and accessibility evaluation.

Writing meaningful content inside a template therefore does not guarantee that users can reach it. Fallback behavior, focus after insertion, names and roles, and status announcements must be tested separately.

Declarative Shadow DOM

shadowrootmode makes a template participate in Declarative Shadow DOM. open and closed select the shadow-root exposure state, while shadowrootdelegatesfocus, slot assignment, serialization, clonability, and a custom element registry add further conditions.

<my-card>
  <template shadowrootmode="open">
    <style>:host { display: block; }</style>
    <slot></slot>
  </template>
  <span>Light DOM content</span>
</my-card>

Declarative Shadow DOM requires separate treatment of parser-created shadow trees, Template Contents, assigned slot nodes, and host/shadow-tree accessibility boundaries. Results from ordinary cloning must not be generalized to every Declarative Shadow DOM configuration.

The for attribute and streaming use

The HTML Standard defines the template for attribute for inserting or replacing content at a location identified by processing instructions. This is a different processing model from ordinary client-side cloning, so implementation support and conditions must be recorded before relying on it.

Accessibility

Separate the fact that a template represents nothing in rendering from the way instantiated contents appear in an accessibility tree. A template does not automatically provide an accessible name for a heading or control that will eventually be created.

StageWhat to inspectBoundary
Before instantiationThe template element and Template ContentsDo not treat them as live rendered content or an interaction target
After ordinary insertionHeadings, labels, buttons, form controls, and live regionsObserve semantics, names, and focus on the inserted DOM
Shadow DOMHost, shadow tree, slots, and focus delegationSeparate browser, platform API, and assistive-technology mappings

Fact / Evidence

Normative claims about the template element, Template Contents, the DOM interface, and Declarative Shadow DOM are separated from browser and accessibility observations.

Key facts and source locations
TypeFactCondition / ScopeStatusSource
SPECtemplate declares an HTML fragment that can be cloned and inserted by script, and represents nothing in rendering.Element definition, rendering, and script insertionReviewedHTML Standard: the template element
SPECTemplate Contents are an associated DocumentFragment, not ordinary children of the template element.content, template contents owner document, and parser behaviorReviewedHTML Standard: template contents
SPECHTMLTemplateElement.content returns the template's Template Contents DocumentFragment.Readonly IDL attribute, cloning, and insertionReviewedHTML Standard: HTMLTemplateElement
SPECshadowrootmode selects the open or closed state of a Declarative Shadow Root.Shadow tree, slot assignment, focus delegation, and serializationReviewedHTML Standard: shadowrootmode
SPECThe template content model is Nothing, and neither tag is omissible.Authoring conformance and parser creation of Template ContentsReviewedHTML Standard: content model

Evidence

  1. HTML Standard: the template element — definition, categories, content attributes, HTMLTemplateElement, and rendering
  2. DOM Standard: DocumentFragment — the fragment interface used for Template Contents
  3. HTML Accessibility API Mappings — an entry point for separating the template from its instantiated contents
  4. Web Platform Tests: the-template-element — tests for template parsing, content, and DOM APIs
  5. Web Platform Tests: declarative Shadow DOM — tests related to shadowrootmode and shadow trees

Implementation Evidence

Normative processing is registered separately from browser observations of Template Contents, cloning, parsing, Declarative Shadow DOM, and accessibility trees. No dedicated fixture has been run yet, so unrun items remain unreviewed.

Dedicated fixture candidate: template-v1 should reproduce the difference between child nodes and content, deep cloning, fragment insertion, duplicate IDs, shadowrootmode, and accessibility trees before and after instantiation.

Implementation evidence registry for the template element
TypeReproduction scopeConditions to recordStatus
IMPLTemplate Contents, content, deep cloning, fragment insertion, adopting, parsing, ID references, and Declarative Shadow DOMCreate fixture ID template-v1; record browser, version, OS, date, and each resultNot run
WPTTemplate parsing, content, DOM APIs, and Declarative Shadow DOM testsRecord selected files, browser, date, pass/fail, and reasons for anything not runNot run
AAMTemplate before instantiation, ordinary insertion, host, shadow tree, slot, and focus mappingsSeparate browser, OS, accessibility tree, assistive technology, and instantiation stateNot run

Coverage / Open Issues

  • CoveredMeaning, categories, contexts, content model, tag omission, and content attributes
  • CoveredTemplate Contents, DocumentFragment, content, and the normative entry points for cloning and insertion
  • CoveredNormative entry points for shadowrootmode, slot assignment, focus delegation, and serialization
  • OpenCross-browser comparison of parsing, custom elements, adopting, dynamic changes, duplicate IDs, and form/ARIA references
  • OpenDeclarative Shadow DOM, slots, closed roots, focus, serialization, and custom element registry implementation differences
  • OpenIndividual WPT results, HTML-AAM platform mappings, assistive technology, compatibility, and Expert-gap Review

This page is initial coverage. It organizes normative entry points without claiming identical behavior across browsers, DOM operations, custom elements, Shadow DOM configurations, or assistive technologies.

Related surface

For the beginner-friendly explanation of reusable markup, content, and cloning, see How to Use the template Element in Yugien. DOM operations are also covered by The script Element; slot and shadow-tree behavior remain part of the Declarative Shadow DOM implementation scope.