CSS Portal

HTML <template> Tag

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The <template> element in HTML is a mechanism for holding HTML content that is not rendered immediately when the page loads. Instead, the content inside a <template> is stored in a "template document fragment," which exists in memory but is inert until it is explicitly instantiated using JavaScript. This makes <template> ideal for defining reusable chunks of HTML that can be cloned and inserted into the DOM dynamically.

Key points about <template>:

  • Non-rendering by default: Content inside a <template> is not displayed on the page. Unlike normal HTML elements, it does not contribute to the DOM layout until it is explicitly added. This allows developers to include HTML markup in the page without affecting its visual output initially.

  • Reusable content: The content within a <template> can be cloned multiple times using JavaScript methods such as document.importNode() or by accessing the .content property. This enables dynamic generation of repeating structures like lists, cards, or form elements without duplicating code in the HTML.

  • Supports nested content: Templates can contain any valid HTML elements, including other <template> elements. Nested templates remain inert until each is explicitly activated, which provides flexibility for complex dynamic UI patterns.

  • Integration with JavaScript: The <template> element is often used in conjunction with scripting to populate web components, implement client-side rendering, or create dynamic interfaces. For example, a developer can define a template for a product card, clone it for each item in a data array, and append it to the DOM to display a list dynamically.

  • Preserves structure and styles: Since the content inside a <template> is still HTML, it retains its structure, classes, and IDs when cloned, allowing developers to style it with CSS or attach event listeners after instantiation.

  • Ideal for client-side frameworks: Modern JavaScript frameworks and libraries, like Vue, React, or Lit, often use <template> internally to manage rendering logic, though native HTML <template> can also be used in plain JavaScript projects for similar dynamic content management.

Example Concept: A <template> can hold a card structure with an image, title, and description. When new data is available, JavaScript clones the template content, fills in the data, and appends it to the page, all without needing to write the HTML markup multiple times.

Properties

Permitted Parents
Any element that accepts metadata content, phrasing content, or script-supporting elements. Also allowed as a child of a <colgroup> element that does not have a span attribute.
Content
No restrictions
Start/End Tags
Start tag: required, End tag: required

Example

<table id="producttable">
<thead>
<tr>
<td>UPC_Code</td>
<td>Product_Name</td>
</tr>
</thead>
<tbody>
<!-- existing data could optionally be included here -->
</tbody>
</table>

<template id="productrow">
<tr>
<td class="record"></td>
<td></td>
</tr>
</template>

Attributes

None

Global Attributes

The <template> tag also supports the Global Attributes in HTML5

Event Attributes

None

Browser Support

The following information will show you the current browser support for the HTML <template> tag. Hover over a browser icon to see the version that first introduced support for this HTML tag.

This tag is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 26th December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!