Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLTemplateElement: inert template content

HTMLTemplateElement stores template content in a separate DocumentFragment exposed through content. The content is not rendered inside the template; copy it with document.importNode() and insert it into the document without consuming the original.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const template = document.createElement("template");
template.content.append(document.createElement("p"));
const fragment = document.importNode(template.content, true);
document.body.append(fragment);

Template inert content means the contents of an ordinary template element, held for later use rather than rendered where declared. The HTMLTemplateElement interface exposes those contents through content as a DocumentFragment. This is a browser DOM API, not an ECMAScript feature; having Node.js 24 alone does not establish its availability.

Query template nodes through template.content.querySelector(), not template.querySelector(). Parsed contents belong to the fragment, not to the element's ordinary children. The content property is read-only, but the fragment itself is mutable. This inertness is not the same as the global inert attribute, which restricts interaction with existing elements.

Use document.importNode(template.content, true) to copy the entire subtree in the target document's context. This matters for constructing custom elements because template content belongs to a separate document. Inserting a fragment moves its children into the destination and empties the fragment. Inserting template.content directly would therefore consume the template's contents instead of preserving them for reuse.

The core interface and content have been widely available across browsers since November 2015; newer features require separate compatibility checks. The example concerns an ordinary template, not declarative Shadow DOM: during parsing, an appropriate declaration using shadowrootmode can create a ShadowRoot instead of an HTMLTemplateElement.

Examples

(() => {
  const template = document.createElement("template");
  const paragraph = document.createElement("p");
  paragraph.textContent = "Hello";
  template.content.append(paragraph);

  const host = document.createElement("section");
  document.body.append(host);
  console.log(`before: ${host.childElementCount}`);

  const fragment = document.importNode(template.content, true);
  fragment.querySelector("p").textContent = "Hello, template!";
  host.append(fragment);

  console.log(`rendered: ${host.textContent}`);
  console.log(`template: ${template.content.textContent}`);
  console.log(`fragment children: ${fragment.childNodes.length}`);
  host.remove();
})();

Result

before: 0
rendered: Hello, template!
template: Hello
fragment children: 0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources