← Back to HTML Course | Chapter 8: Modern HTML & Components | Lesson 5 of 10

HTML Template Tag

Imagine buying a cookie cutter in the shape of a star. By itself, the cookie cutter is just a tool sitting in your kitchen drawer. You do not eat the cookie cutter. Instead, you use it to cut out identical star-shaped cookies whenever you want to bake. The HTML template tag is that cookie cutter. It allows you to define a block of HTML code (like a formatted user review card or a tool status list) that stays completely hidden when your page loads. Whenever you need to display new data, you write a JavaScript script to grab the template, fill in the details, and clone it onto your page dynamically.

The template Element

The template tag defines a block of HTML markup that remains inert and completely hidden when the webpage loads. It serves as a reusable template container that you can copy and instantiate dynamically using JavaScript.

Note: Use the template tag to hold complex component markups that you want to clone dynamically.

Warning: The browser will not download any images or load any scripts inside template tags on page load, as the content is inert.

Example: The template Element

markup
<template id="card-template">
  <p>Card content</p>
</template>

Cloning Template Content

To display template content on your page, you write a JavaScript script to capture the template's ID, access its content, clone its node structure using cloneNode(true), and append it to your page layout.

Note: Passing the true argument to cloneNode ensures the browser copies the element's entire nested child tree.

Warning: If you do not clone the template content correctly, you can accidentally move the original template nodes, making them unavailable for future use.

Example: Cloning Template Content

markup
<template id="card-template">
  <p>Card content</p>
</template>
<script>
  const clone = document.getElementById('card-template').content.cloneNode(true);
  document.body.appendChild(clone);
</script>

Injecting Data into Templates

Once you have cloned the template content, you can easily target its internal elements (like heading or paragraph tags) in your scripts to inject custom data values before appending the clone to your page layout.

Note: Target internal elements in your cloned fragments using querySelector to keep your data injection clean.

Warning: Always target the cloned fragment, and do not target the original template node directly when updating values.

Example: Injecting Data into Templates

markup
<template id="card-template">
  <p class="name"></p>
</template>
<script>
  const clone = document.getElementById('card-template').content.cloneNode(true);
  clone.querySelector('.name').textContent = 'Alex';
  document.body.appendChild(clone);
</script>

Performance Benefits of template

Before the template element was introduced, developers had to build dynamic layouts by creating elements manually inside long, slow JavaScript loops or using innerHTML strings, which forced browsers to rebuild their layouts and slowed down page performance. Templates are parsed by the browser once, making them incredibly fast and efficient to clone.

Note: Use template tags instead of innerHTML strings to improve your website's performance and loading speed.

Warning: Overusing innerHTML strings inside heavy loops can trigger layout-thrashing performance bottlenecks on mobile devices.

Example: Performance Benefits of template

markup
<template id="row-template">
  <li></li>
</template>
<script>
  for (let i = 0; i < 100; i++) {
    document.body.appendChild(document.getElementById('row-template').content.cloneNode(true));
  }
</script>

Conditional Layout Toggles

You can use template tags to store entire layout sections (like interactive modal forms, warning notices, or tool panels) that you only want to load when a user clicks a button or triggers an action.

Note: Use template tags to keep complex, secondary layout components hidden until they are needed.

Warning: Since template contents are inert, they do not appear in the default page DOM, making them inaccessible to search crawlers until cloned.

Example: Conditional Layout Toggles

markup
<template id="modal-template">
  <div class="modal">Warning notice</div>
</template>
<button onclick="document.body.appendChild(document.getElementById('modal-template').content.cloneNode(true))">
  Show Warning
</button>
Common Mistakes
  1. Writing template code and expecting it to display on page load without cloning it.
  2. Attempting to target template contents using standard document.getElementById, which will return null.
  3. Updating values in the original template node directly instead of targeting your cloned fragments.
Chapter Summary
  • The template tag defines an inert block of HTML markup that stays hidden on page load.
  • Capture the template ID in your scripts and use content.cloneNode(true) to clone its structure.
  • Inject custom data into your cloned fragments before appending them to your webpage layout to keep your site fast and optimized.
Browser Support

Standard HTML5 template elements are supported natively by all modern web browsers.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.