HTML <template> Tag

Beginner
7 min read
Updated: Nov 2026
4 examples
Live preview

Definition and Usage

The <template> element holds an inert HTML fragment. The browser parses the markup but does not render it. With <script>, you clone template.content and insert copies into the live document.

Use it for reusable cards, list items, or <table> rows. Nested nodes are not normal children of the element — read them from the content DocumentFragment only.

Baseline · Widely available.

Not deprecated. The template element has been stable across browsers since about November 2015. Some extras (declarative shadow roots, streaming for) have narrower support.

Declarative Shadow DOM.

With shadowrootmode, the parser can turn a <template> into a shadow root on its parent. Skip this until you need Web Components.

Experimental & non-standard.

The streaming for patch attribute is experimental. The old non-standard shadowroot attribute was removed — use shadowrootmode instead.

HTML Syntax

syntax.html
<template id=" name">markup</template>

Both start and end tags are required. Give the template an id so scripts can find it. Anything you nest in the source is stored in content, not as childNodes of the element.

Attributes

Everyday templates usually need only global attributes (especially id). These extras matter for advanced cases:

AttributeDescriptionNotes
shadowrootmodeopen or closed — declarative shadow root on the parentAdvanced Web Components
shadowrootdelegatesfocusDelegates focus into the declarative shadow treeWith shadowrootmode
shadowrootclonableCloned hosts keep a shadow root copyWith shadowrootmode
forMatches a streaming patch marker nameExperimental. Not the label for

Global Attributes

The <template> tag also supports the Global Attributes in HTML.

Live Preview

Pick a title, then clone a real <template> into the list.

The template below is inert. Each click clones content into the output list.

Cloned cards

No clones yet — press Clone card.

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Basic Clone

Store markup in a template, then append one clone to the page.

HTML
<template id="note">
  <p>Hello from a template.</p>
</template>

<div id="out"></div>

<script>
  const tpl = document.getElementById("note");
  const clone = document.importNode(tpl.content, true);
  document.getElementById("out").appendChild(clone);
</script>
Try It Yourself

How It Works

  1. Define <template id="…"> with the HTML you want to reuse.
  2. Read tpl.content — that is the inert DocumentFragment.
  3. Clone with document.importNode(…, true) (deep clone into this document).
  4. Append the clone so the markup becomes visible.

Example 2 — Fill Data, Then Clone

Clone once per item and set text before appending.

HTML
<template id="item">
  <li></li>
</template>

<ul id="list"></ul>

<script>
  const tpl = document.getElementById("item");
  const list = document.getElementById("list");
  ["Ada", "Grace", "Lin"].forEach((name) => {
    const clone = document.importNode(tpl.content, true);
    clone.querySelector("li").textContent = name;
    list.appendChild(clone);
  });
</script>
Try It Yourself

How It Works

  1. Keep a blank structure inside the template (here, an empty <li>).
  2. Loop your data and clone once per value.
  3. Write into the clone with querySelector + textContent.
  4. Append each clone to the live list.

Example 3 — Table Row Template

Store a <tr> in a template and append clones to <tbody>.

HTML
<table>
  <thead>
    <tr>
      <th scope="col">Code</th>
      <th scope="col">Name</th>
    </tr>
  </thead>
  <tbody id="rows"></tbody>
</table>

<template id="row">
  <tr>
    <td></td>
    <td></td>
  </tr>
</template>

<script>
  const tpl = document.getElementById("row");
  const body = document.getElementById("rows");
  const data = [
    ["101", "Notebook"],
    ["202", "Pencil"],
  ];
  data.forEach(([code, name]) => {
    const clone = document.importNode(tpl.content, true);
    const cells = clone.querySelectorAll("td");
    cells[0].textContent = code;
    cells[1].textContent = name;
    body.appendChild(clone);
  });
</script>
Try It Yourself

How It Works

  1. Build the table shell with an empty <tbody>.
  2. Put one <tr> (with empty <td> cells) in the template.
  3. Clone and fill each cell from your data array.
  4. Append to tbody so the row joins the table correctly.

Example 4 — template vs hidden div

Prefer <template> for reusable markup that should stay inert.

HTML
<!-- Prefer: inert until cloned -->
<template id="card">
  <article><h3>Title</h3></article>
</template>

<!-- Avoid for reusable fragments -->
<div id="card-hidden" hidden>
  <article><h3>Title</h3></article>
</div>
Try It Yourself

How It Works

  1. <template> stores a fragment that is not painted.
  2. A hidden div is still part of the live tree.
  3. Scripts and media inside a hidden div can still activate; template content does not until cloned.
  4. Clone from the template when you need a visible copy.

Edge Cases & Pitfalls

Most template bugs come from reading the wrong nodes or mutating the fragment incorrectly.

api

Use content, not children

template.childNodes is empty for nested source markup. Always clone from template.content.

clone

Prefer document.importNode

Importing into the current document is safer for custom elements than cloning with the template’s own document context.

append

Do not append into the template element

Calling appendChild on the <template> itself does not update content. Append clones to a live parent instead.

ids

Avoid duplicate ids in clones

If the fragment contains an id, change it (or remove it) on each clone so the document stays unique.

parsing

Body/html tags inside are ignored

The HTML parser ignores html, head, and body wrappers inside a template. Put the real fragment nodes only.

Browser Support

The <template> element is core HTML in modern browsers. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<template>

Widely available since about November 2015. Cloning template.content works everywhere modern. Declarative shadowrootmode and streaming for have narrower support.

96% Global support (approx.)
Google Chrome 26+
Full support
Mozilla Firefox 22+
Full support
Apple Safari 8+
Full support
Microsoft Edge 13+
Full support
Opera 15+
Full support
Samsung Internet 1.5+
Full support
Internet Explorer No
Not supported
<template> Widely supported

Bottom line: Safe for modern sites. Use importNode + content for everyday cloning; treat shadowrootmode and for as progressive extras.

Key Takeaways

  • Role: <template> stores inert HTML for later cloning.
  • API: clone template.content with document.importNode.
  • Reuse: fill each clone, then append — lists, cards, or table rows.
  • Not a hidden div: template content stays inert until activated.

One line: put reusable markup in <template>, then importNode its content into the page when you need it.

Default CSS Settings

Typical user-agent styling keeps the template element itself out of layout:

default.css
template {
  display: none;
}

Even without that rule, nested source markup is not painted until you clone and insert content. Do not rely on CSS alone to “show” a template.

Frequently Asked Questions

The template element holds HTML that is not rendered on its own. JavaScript clones the fragment later and inserts copies into the page.
Nested markup lives in a DocumentFragment on the content property, not as normal children of the template element. The browser does not paint that fragment until you clone and append it.
Select the template, clone its content (prefer document.importNode(template.content, true)), optionally fill text or attributes, then append the clone to the document.
Yes for reusable markup. A hidden div still parses scripts and images as live page content. A template stays inert until you activate a clone.
It is the declarative Shadow DOM path: the parser turns that template into a shadow root on the parent. Everyday reusable fragments do not need it.
Yes. Storing a tr (or several cells) in a template is a common way to add table rows with JavaScript.

Next: textarea

Create a multi-line text input for forms and comments.

textarea tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful