🌐
W3Schools
w3schools.com › tags › tag_template.asp
HTML template tag
More "Try it Yourself" examples below. The <template> tag is used as a container to hold some HTML content hidden from the user when the page loads.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Reference › Elements › template
<template>: The Content Template element - HTML | MDN
Note: The HTML parser creates a ShadowRoot object in the DOM for the first <template> in a node with this attribute set to an allowed value. If the attribute is not set, or not set to an allowed value — or if a ShadowRoot has already been declaratively created in the same parent — then an HTMLTemplateElement is constructed. A HTMLTemplateElement cannot subsequently be changed into a shadow root after parsing, for example, by setting HTMLTemplateElement.shadowRootMode.
🌐
DEV Community
dev.to › arafat4693 › html-template-tag-explained-3859
HTML Template Tag Explained - DEV Community
January 15, 2023 - For example, you might use the template tag to define a form that can be cloned and added to the page multiple times, or to create a list of items that can be filtered and sorted by the user.
🌐
Medium
medium.com › @gargsourabh › how-template-tag-works-in-html-js-code-23836dfbd9d2
How <template> Tag works in HTML/JS code? | by Sourabh Garg | Medium
October 29, 2023 - Here’s a simple example of how to use the <template> tag to create and render content dynamically. You can expand on this idea to create more complex templates and dynamic content: <!DOCTYPE html> <html> <head> <title>Using Template Tag</title> </head> <body> <template id="my-template"> <h2>This is a template</h2> <p>Content inside the template.</p> </template> <script> const myTemplate = document.getElementById('my-template'); const clone = document.importNode(myTemplate.content, true); // Modify the cloned content const h2 = clone.querySelector('h2'); h2.textContent = 'Updated Template'; document.body.appendChild(clone); </script> </body> </html>
🌐
GeeksforGeeks
geeksforgeeks.org › html › html-template-tag
HTML template Tag - GeeksforGeeks
July 11, 2025 - Example 3: This example illustrates the uses of JavaScript to display the template tag content. ... <!DOCTYPE html> <html> <body> <h1>GeeksforGeeks</h1> <h3>HTML template tag</h3> <p> Click the button to get the content from a template, and display it in the web page.
🌐
OpenReplay
blog.openreplay.com › understanding-the-template-element-in-html
Understanding the Template Element in HTML
Remember, using a <template> makes it very clear that the HTML inside of it needs JavaScript to render content dynamically. Here, we used the tag to hide our content.
🌐
DhiWise
dhiwise.com › post › how-to-use-the-html-template-tag-for-dynamic-web-applications
Guide to the HTML Template Tag: Everything You Need to Know
August 7, 2024 - The template tag's ability to store ... For example, a news website might use templates to display articles, where each article's content is inserted into a clone of the template and then added to the page....
🌐
npm
npmjs.com › package › html-template-tag
html-template-tag - npm
var html = require("html-templ... ["Antonio", "Megan"], }; var template = ({ names }) => html` <ul> ${names.map((name) => html` <li>Hello, ${name}!</li> `)} </ul> `; var string = template(data); /* " <ul> <li>Hello, Antonio!</li> ...
      » npm install html-template-tag
    
Published   Dec 28, 2024
Version   4.1.1
Author   Antonio Villagra De La Cruz
🌐
W3docs
w3docs.com › learn-html › html-template-tag.html
HTML <template> Tag
The tag is used to store code fragments that can be cloned and used in the HTML document again and again. See examples.
Find elsewhere
🌐
Javatpoint
javatpoint.com › html-template-tag
HTML template tag - javatpoint
HTML template tag with examples, html5, forms, input, text, anchor, image, heading, marquee, textarea, paragraph, title, quotes, code etc.
🌐
DotFactory
dofactory.com › html › template
HTML template Tag
<table id="user-table"> <tr> <th>First Name</th> <th>Last Name</th> <th>Email</th> </tr> <tr> <td><input type="textbox" name="firstname" value="Anna"></td> <td><input type="textbox" name="lastname" value="Kroger"></td> <td><input type="textbox" name="emailname" value="[email protected]"></td> </tr> </table> <button onclick="addRow()">Add Row</button> <template id="user-row"> <tr> <td><input type="textbox" name="firstname"></td> <td><input type="textbox" name="lastname"></td> <td><input type="textbox" name="emailname"></td> </tr> </template> <script> let counter = 0; let addRow = () => { if (++counter > 6) { alert("Max rows reached"); return; } let template = document.getElementById("user-row"); let newRow = template.content.cloneNode(true); let table = document.getElementById("user-table"); table.appendChild(newRow); } </script> Try it live · Note: This example is not entirely complete.
🌐
HTML.am
html.am › tags › html-template-tag.cfm
HTML Tag
In this example, the <template> tag is used to provide additional content for the page whenever the user clicks the Use Template!
🌐
Quackit
quackit.com › html › tags › html_template_tag.cfm
HTML Tag
Here's an example of adding rows to an editable table using the <template> tag. Attributes can be added to an HTML element to provide more information about how the element should appear or behave.
🌐
JavaScript.info
javascript.info › tutorial › web components
Template element
The <template> element does not feature any iteration mechanisms, data binding or variable substitutions, but we can implement those on top of it. ... If you have suggestions what to improve - please submit a GitHub issue or a pull request instead of commenting. If you can't understand something in the article – please elaborate. To insert few words of code, use the <code> tag, for several lines – wrap them in <pre> tag, for more than 10 lines – use a sandbox (plnkr, jsbin, codepen…)
🌐
web.dev
web.dev › articles › html's new template tag
HTML's New Template Tag | Articles | web.dev
February 26, 2013 - They're a place to put a big wad of HTML that you don't want the browser to mess with at all…for any reason. Rafael Weinstein (spec author) To feature detect <template>, create the DOM element and check that the .content property exists:
🌐
Web Dev Simplified
blog.webdevsimplified.com › 2020-06 › template-tag
Why Use The HTML Template Tag?
You can think of it kind of like a div with display none that has HTML inside of it, but the template tag has a few handy features that make it easier to work with than a div with display none.
🌐
Codemotion
codemotion.com › home › frontend › web developer › content and templates in html
Content and templates in HTML - Codemotion Magazine
December 16, 2020 - This example defines a list item as the template for the markup that describes a book. The details of the book description consist of an image (<img>), a title (<h5>), and a text (<p>). Notice that an id has been assigned to the template: ...
🌐
Quackit
quackit.com › html_5 › tags › html_template_tag.cfm
HTML 5 Tag
Attributes are added to a tag to provide the browser with more information about how the tag should appear or behave. Attributes consist of a name and a value separated by an equals (=) sign, with the value surrounded by double quotes. Here's an example, style="color:black;". There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.
🌐
danvega.dev
danvega.dev › blog › html-template-tag
HTML Template Tag in Vanilla JavaScript and Vue
In a more complex example where there is a lot more markup involved it can become very cumbersome to create elements, nodes as well as dealing with attributes and classes. In this instance, a great solution is The Content Element Template. I also realized that a lot of developers (beginning and vets) might not know what this is or why we would use it. In this article, I am going to take a look at the <template/> tag in HTML & Vanilla JavaScript.
Top answer
1 of 2
8

There are some major differences:

  1. Script (<script>) in a <template> element is not executed immediately.

  2. Media content (<audio>, <video>, <img>) inside the template won't be loaded immediately.

    Instead they will be executed or loaded once they are inserted in the rendered DOM tree.

  3. querySelector() method calls and CSS selectors won't explore the content of a <template>, but you can still access the elements inside by using querySelector() on its content property.

  4. With HTML5 template, you can access all its content in the form of a Document Fragment, and insert it in the DOM tree using appendChild(), instead of modifying innerHTML.

  5. <template> elements can be placed in the <head> element.

  6. You access the template content as a string using .innerHTML or as a DocumentFragment using .content.

It's quite difficult then to fake all thses behaviors. There are some polyfills that can partially do that. I'd recommend Neovov's one.


Look at this illustrative example:

//explore the content of the template
var script = T1.content.querySelector( 'script' )
console.log( script.innerHTML )

function insert() {
  //insert the real templete
  Target1.appendChild( T1.content.cloneNode( true ) )
  //insert the fake template
  Target2.innerHTML += D1.innerHTML
}
<template id=T1>
  Real Template -
  <script>console.log( 'executed at each insertion' )</script>
</template>

<div hidden id=D1>
  Fake Template -
  <script>console.log( 'executed only at parsing' )</script>
</div>

<button onclick="insert()">insert</button>

<div id=Target1><div>
<div id=Target2><div>
2 of 2
1

In short, the template tag is exactly what it sounds like. It is just a template for the JavaScript to create in the future.

It is slightly different from a hidden <div> though, but basically you are right and it can be faked with a hidden div

Imagine this scenario where you have a webpage with 2 inputs, one that enters your name and one that enters your age. By using a template tag to create a simple table, you can then use JavaScript to populate the table with your inputs, in the same page.

This article has a good read on it: https://www.sitepoint.com/html5-template-tag/