HTML BasicsLesson 26 of 36

The template Tag

The template tag holds markup you plan to reuse, like a blueprint sitting in the document. Nothing inside a template ever renders. You can drop any normal HTML inside, and the browser parses it once, validates the markup, and waits until JavaScript asks for a copy.

The template tag holds markup you plan to reuse, like a blueprint sitting in the document. Nothing inside a template ever renders. You can drop any normal HTML inside, and the browser parses it once, validates the markup, and waits until JavaScript asks for a copy.

It is the only tag whose content is parsed but invisible: no display:none tricks, no empty divs waiting in the layout. Scripts clone the template content and inject it wherever new cards, rows, or list items are needed. That makes it ideal for data-driven pages that render the same block over and over.

Why it exists: building HTML from JavaScript strings is fragile, full of quote bugs, and impossible to preview in an editor. A template keeps the markup in the page where the browser validates it. It is supported in every modern browser, and cloning takes a single call.

Cloning content

The content property holds the template as a document fragment. Calling content.cloneNode(true) makes a fresh copy with all inner elements intact. Each clone is independent, so you can fill them with different data without one clone affecting another.

HTML
const tpl = document.getElementById("card");
const copy = tpl.content.cloneNode(true);
document.body.appendChild(copy);

Why templates beat strings

Writing HTML inside JavaScript strings is hard to read and easy to break with quotes. A template keeps the markup in the document where it belongs. A common mistake is appending tpl.content directly instead of cloning it: that moves the fragment, so the second append inserts nothing.

Syntax

HTML
<template id="card">
  <div class="card">
    <h3>Title</h3>
    <p>Body text.</p>
  </div>
</template>

template.content Is a Ghost - Append It Once and It Empties

The <template> content lives in a DocumentFragment not in the DOM - if you do parent.append(template.content) without cloning, the fragment moves and template becomes empty forever (second append does nothing). Stack Overflow #65605084 asks why cloneNode is mandatory. Also, cloneNode(true) does NOT clone event listeners - new clones are inert. Use template.content.cloneNode(true) every time and re-attach listeners via delegation.

HTML
const clone = document.getElementById('row').content.cloneNode(true)
// not template.content - that moves! + listeners lost

Good to know

  • Template content is parsed but never displayed.
  • Clone it with content.cloneNode(true).
  • Appending content directly moves it; clone when you need copies.
  • Great for repeated cards, rows, and list items.
Try it Yourself Edit the code, press Run, then check what changed in the Result.
Ready
Code
Result

Practice makes it stick

Open the Playground and build your own page with the tags you just learned.

→ Open Playground