HTML <template> Tag

Beginner
⏱️ 7 min read
📚 Updated: Jun 2026
🎯 3 Examples
HTML Scripting

What You’ll Learn

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.

01

Inert Markup

Hidden until activated.

02

cloneNode

Insert into the DOM.

03

Reusable UI

Cards, list items.

04

id Attribute

Target with JavaScript.

05

vs Hidden div

True inert content.

06

Best Practices

Clean, maintainable UI.

What Is the <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.

Valid HTML5 — Inert Content Container

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.

📝 Syntax

Wrap reusable markup between opening and closing template tags:

syntax.html
<template>
  <!-- Your reusable content here -->
</template>

Template with id

template-id.html
<template id="myTemplate">
  <p>This is content inside the template.</p>
</template>

Syntax Rules

  • Content inside template is not displayed until cloned into the live DOM.
  • Use an id when you need to reference a specific template from JavaScript.
  • Access the fragment via template.content (a DocumentFragment).
  • Clone with cloneNode(true) or document.importNode(template.content, true).

⚡ Quick Reference

TopicCode SnippetNotes
Basic template<template>...</template>Inert fragment
With id<template id="card">JS selector
Get contenttemplate.contentDocumentFragment
Clonecontent.cloneNode(true)Deep copy
Insertparent.appendChild(clone)Activate
Browser supportModern browsersIE 11 partial

▶️ Activation via JavaScript

The power of template appears when you clone its content and insert it into the document:

activation.html
<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.

⚖️ <template> vs Hidden div

ApproachBehaviorBest for
<template>Truly inert; no render, no image load, no script runReusable UI fragments
display: none divStill in DOM; may load resourcesSimple show/hide toggles
Cloningtemplate.content.cloneNode()innerHTML copy or manual duplicate
SemanticsPurpose-built for fragmentsGeneric hidden container

🧰 Attributes

The <template> tag has no tag-specific attributes beyond standard global attributes. The id attribute is especially useful.

attributes.html
<template id="myTemplate">
  <!-- Your template content here -->
</template>
id Recommended

Uniquely identify a template for getElementById or querySelector.

id="cardTemplate"
class Global

Group templates for styling or selection (rare on the element itself).

class="ui-template"
content DOM property

Read-only property returning the template’s DocumentFragment.

template.content
data-* Optional

Store metadata such as component type or variant name.

data-component="card"

Examples Gallery

Inert templates, JavaScript activation, and reusable card components with copy-ready code and live previews.

👀 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.

📚 Common Use Cases

Use <template> for reusable components, conditionally inserted content, and repeatable UI patterns.

Basic Inert Template

Define a template with an id for later reference. The content stays hidden until activated:

basic-template.html
<template id="myTemplate">
  <p>This is content inside the template.</p>
</template>
Try It Yourself

Activation via JavaScript

Clone and append template content to display it on the page:

activate-template.html
<script>
  const template = document.querySelector('template');
  const clone = template.content.cloneNode(true);
  document.body.appendChild(clone);
</script>
Try It Yourself

Reusable Components

Store a card layout in a template and clone it into a display area:

reusable-components.html
<template id="cardTemplate">
  <div class="card">
    <h2>Title</h2>
    <p>Description goes here.</p>
  </div>
</template>
Try It Yourself

♿ Accessibility

  • Inert by default — Template content is not exposed to assistive tech until cloned into the live DOM.
  • Clone into accessible structure — Ensure activated content uses proper headings, labels, and ARIA when needed.
  • Do not hide critical content only in template — Essential page information should exist in the document or be activated promptly.
  • Manage focus — When inserting interactive UI from a template, move focus appropriately for keyboard users.

🧠 How <template> Works

1

Author stores markup

Reusable HTML lives inside template, remaining inert.

Markup
2

JavaScript clones content

cloneNode(true) copies the DocumentFragment.

Script
3

Clone enters the DOM

appendChild activates the fragment and renders it.

Display
=

Reusable UI on demand

The same blueprint can be cloned many times without duplicating markup in source.

Browser Support

The <template> tag is fully supported in all modern browsers. Internet Explorer 11 has partial support.

Baseline · HTML5

Templates in modern browsers

Chrome, Firefox, Safari, and Edge fully support inert templates and template.content.

95% Core tag support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 11 · Partial
Partial support
Opera Fully supported
Full support
<template> tag 95% supported

Bottom line: Use <template> confidently in modern browsers; provide fallbacks for legacy IE if needed.

Conclusion

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.

💡 Best Practices

✅ Do

  • Give templates meaningful id values for JavaScript access
  • Use cloneNode(true) for deep copies of template content
  • Keep reusable UI patterns inside template
  • Remember content is inert until inserted into the DOM

❌ Don’t

  • Expect template content to render without JavaScript
  • Put executable scripts inside templates without understanding clone behavior
  • Use template for content that should always be visible
  • Rely on display: none divs when true inert behavior is needed

Key Takeaways

Knowledge Unlocked

Six truths every developer should know about <template>

Bookmark these before you build reusable UI fragments.

6
Core concepts
🔄 02

cloneNode

Activate UI.

JavaScript
🎨 03

Reusable

Card patterns.

Use case
⚖️ 04

vs hidden div

True inert.

Comparison
05

Activate for A11y

Clone to DOM.

A11y
🌐 06

Modern Support

IE 11 partial.

Compatibility

❓ Frequently Asked Questions

It stores reusable HTML fragments that stay inert until cloned and inserted with JavaScript.
No. Content inside template is inert and does not render until activated.
Use template.content.cloneNode(true), then appendChild the clone into the DOM.
So JavaScript can select it with getElementById or querySelector.
Yes in all modern browsers. Internet Explorer 11 has partial support.

Build reusable UI fragments

Practice <template> and cloneNode in the Try It editor.

Try activation example →

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.

6 people found this page helpful