👀 Live Preview
Activated card cloned from a template (what users see after JavaScript runs):
Title
Description goes here.
Note: Raw template content is inert and invisible until cloned into the DOM.

The <template> tag stores reusable HTML fragments that stay inert until JavaScript clones them. This guide covers syntax, activation, reusable components, inert behavior, accessibility, and best practices for beginners.
Hidden until activated.
Insert into the DOM.
Cards, list items.
Target with JavaScript.
True inert content.
Clean, maintainable UI.
<template> Tag?The <template> tag is an HTML element for declaring reusable fragments of markup that can be cloned and inserted into the document later with JavaScript. Content inside a template is inert — it is not rendered, does not load images, and does not run scripts until activated.
Think of template as a blueprint stored off-stage. Clone it with template.content.cloneNode(true) when you need to display the markup.
Common uses include reusable UI components, list item patterns, conditional content, and client-side templates in single-page applications.
Wrap reusable markup between opening and closing template tags:
<template>
<!-- Your reusable content here -->
</template><template id="myTemplate">
<p>This is content inside the template.</p>
</template>template is not displayed until cloned into the live DOM.id when you need to reference a specific template from JavaScript.template.content (a DocumentFragment).cloneNode(true) or document.importNode(template.content, true).| Topic | Code Snippet | Notes |
|---|---|---|
| Basic template | <template>...</template> | Inert fragment |
| With id | <template id="card"> | JS selector |
| Get content | template.content | DocumentFragment |
| Clone | content.cloneNode(true) | Deep copy |
| Insert | parent.appendChild(clone) | Activate |
| Browser support | Modern browsers | IE 11 partial |
The power of template appears when you clone its content and insert it into the document:
<template id="greetingTemplate">
<p>This content is from the template!</p>
</template>
<div id="output"></div>
<script>
const template = document.getElementById('greetingTemplate');
const clone = template.content.cloneNode(true);
document.getElementById('output').appendChild(clone);
</script>template.content returns a DocumentFragment containing the template markup.cloneNode(true) performs a deep clone of all child nodes.document.importNode(template.content, true) is an alternative for cross-document cloning.The <template> tag has no tag-specific attributes beyond standard global attributes. The id attribute is especially useful.
<template id="myTemplate">
<!-- Your template content here -->
</template>id RecommendedUniquely identify a template for getElementById or querySelector.
id="cardTemplate"class GlobalGroup templates for styling or selection (rare on the element itself).
class="ui-template"content DOM propertyRead-only property returning the template’s DocumentFragment.
template.contentdata-* OptionalStore metadata such as component type or variant name.
data-component="card"Inert templates, JavaScript activation, and reusable card components with copy-ready code and live previews.
Activated card cloned from a template (what users see after JavaScript runs):
Description goes here.
Note: Raw template content is inert and invisible until cloned into the DOM.
Use <template> for reusable components, conditionally inserted content, and repeatable UI patterns.
Define a template with an id for later reference. The content stays hidden until activated:
<template id="myTemplate">
<p>This is content inside the template.</p>
</template>Clone and append template content to display it on the page:
<script>
const template = document.querySelector('template');
const clone = template.content.cloneNode(true);
document.body.appendChild(clone);
</script>Store a card layout in a template and clone it into a display area:
<template id="cardTemplate">
<div class="card">
<h2>Title</h2>
<p>Description goes here.</p>
</div>
</template>Reusable HTML lives inside template, remaining inert.
cloneNode(true) copies the DocumentFragment.
appendChild activates the fragment and renders it.
The same blueprint can be cloned many times without duplicating markup in source.
The <template> tag is fully supported in all modern browsers. Internet Explorer 11 has partial support.
Chrome, Firefox, Safari, and Edge fully support inert templates and template.content.
Bottom line: Use <template> confidently in modern browsers; provide fallbacks for legacy IE if needed.
The <template> tag is a valuable tool for managing reusable content in web applications. By keeping markup inert until cloned with JavaScript, you improve maintainability and build dynamic interfaces more cleanly.
id values for JavaScript accesscloneNode(true) for deep copies of template contenttemplatetemplate for content that should always be visibledisplay: none divs when true inert behavior is needed<template>Bookmark these before you build reusable UI fragments.
Hidden markup.
PurposeActivate UI.
JavaScriptCard patterns.
Use caseTrue inert.
ComparisonClone to DOM.
A11yIE 11 partial.
Compatibilitytemplate is inert and does not render until activated.template.content.cloneNode(true), then appendChild the clone into the DOM.getElementById or querySelector.Practice <template> and cloneNode in the Try It editor.
6 people found this page helpful