Syntax
<template id="my-template">
<p>Reusable content</p>
</template>Examples
Basic Template Definition
A template holding markup that stays hidden until cloned.
<template id="card-template">
<div class="card">
<h3></h3>
<p></p>
</div>
</template>Cloning a Template with JavaScript
Using the template to generate repeated list items dynamically.
<template id="item-template">
<li class="item"></li>
</template>
<ul id="list"></ul>
<script>
const tpl = document.getElementById('item-template');
const list = document.getElementById('list');
['HTML', 'CSS', 'JS'].forEach(text => {
const clone = tpl.content.cloneNode(true);
clone.querySelector('.item').textContent = text;
list.appendChild(clone);
});
</script>Best practices
- Use template for markup that will be cloned and reused multiple times via JavaScript, like list items or cards
- Remember that content inside a template is inert — access it via the element's .content property, not by querying it directly
- Prefer template over building HTML strings with string concatenation or innerHTML for repeated UI patterns
- Combine with document.importNode or cloneNode(true) to insert a copy without mutating the original template
At a glance
- Purpose
- Structure and meaning for web content
- File extension
- .html · .htm
- Runs in
- Web browsers
- Usually used with
- CSS and JavaScript
Specifications & further reading
Related HTML documentation
<script>
The <script> element embeds or references executable JavaScript code within an HTML document. It can contain inline code directly between its tags, or load an external file via the src attribute. Where scripts are placed and how they load (defer, async, or neither) significantly affects page load performance and execution order.<canvas>
The <canvas> element provides a blank, resizable bitmap surface for drawing graphics, animations, and visualizations via JavaScript — typically using the Canvas 2D API or WebGL. Unlike SVG, canvas content is pixel-based rather than made of individually addressable DOM elements, which makes it well-suited for games, charts, and pixel-level image manipulation.<noscript>
The <noscript> element defines content to display only when JavaScript is disabled or unsupported in the browser. It commonly contains a message explaining that the page requires JavaScript, or a fallback version of functionality that would otherwise depend on scripting.HTML
HTML (HyperText Markup Language) is the standard markup language for creating web pages. It structures web content using elements and tags, defining everything from headings and paragraphs to images and links. HTML forms the backbone of every website, working alongside CSS for styling and JavaScript for interactivity.
The <script> element embeds or references executable JavaScript code within an HTML document. It can contain inline code directly between its tags, or load an external file via the src attribute. Where scripts are placed and how they load (defer, async, or neither) significantly affects page load performance and execution order.<canvas>
The <canvas> element provides a blank, resizable bitmap surface for drawing graphics, animations, and visualizations via JavaScript — typically using the Canvas 2D API or WebGL. Unlike SVG, canvas content is pixel-based rather than made of individually addressable DOM elements, which makes it well-suited for games, charts, and pixel-level image manipulation.<noscript>
The <noscript> element defines content to display only when JavaScript is disabled or unsupported in the browser. It commonly contains a message explaining that the page requires JavaScript, or a fallback version of functionality that would otherwise depend on scripting.HTML
HTML (HyperText Markup Language) is the standard markup language for creating web pages. It structures web content using elements and tags, defining everything from headings and paragraphs to images and links. HTML forms the backbone of every website, working alongside CSS for styling and JavaScript for interactivity.