Cloned from <template>
HTML <template> Tag

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.
Not deprecated. The template element has been stable across browsers since about November 2015. Some extras (declarative shadow roots, streaming for) have narrower support.
With shadowrootmode, the parser can turn a <template> into a shadow root on its parent. Skip this until you need Web Components.
The streaming for patch attribute is experimental. The old non-standard shadowroot attribute was removed — use shadowrootmode instead.
HTML Syntax
<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:
| Attribute | Description | Notes |
|---|---|---|
shadowrootmode | open or closed — declarative shadow root on the parent | Advanced Web Components |
shadowrootdelegatesfocus | Delegates focus into the declarative shadow tree | With shadowrootmode |
shadowrootclonable | Cloned hosts keep a shadow root copy | With shadowrootmode |
for | Matches a streaming patch marker name | Experimental. 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.
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.
<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> How It Works
- Define
<template id="…">with the HTML you want to reuse. - Read
tpl.content— that is the inertDocumentFragment. - Clone with
document.importNode(…, true)(deep clone into this document). - Append the clone so the markup becomes visible.
Example 2 — Fill Data, Then Clone
Clone once per item and set text before appending.
<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> How It Works
- Keep a blank structure inside the template (here, an empty
<li>). - Loop your data and clone once per value.
- Write into the clone with
querySelector+textContent. - Append each clone to the live list.
Example 3 — Table Row Template
Store a <tr> in a template and append clones to <tbody>.
<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> How It Works
- Build the table shell with an empty
<tbody>. - Put one
<tr>(with empty<td>cells) in the template. - Clone and fill each cell from your data array.
- Append to
tbodyso the row joins the table correctly.
Edge Cases & Pitfalls
Most template bugs come from reading the wrong nodes or mutating the fragment incorrectly.
Use content, not children
template.childNodes is empty for nested source markup. Always clone from template.content.
Prefer document.importNode
Importing into the current document is safer for custom elements than cloning with the template’s own document context.
Do not append into the template element
Calling appendChild on the <template> itself does not update content. Append clones to a live parent instead.
Avoid duplicate ids in clones
If the fragment contains an id, change it (or remove it) on each clone so the document stays unique.
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.
<template>
Widely available since about November 2015. Cloning template.content works everywhere modern. Declarative shadowrootmode and streaming for have narrower support.
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.contentwithdocument.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:
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
Next: textarea
Create a multi-line text input for forms and comments.
12 people found this page helpful
