Table of contents
1.
Introduction
2.
Definition and Usage
2.1.
Key Points
3.
Syntax
4.
Example
5.
Browser Support
6.
Global Attributes
6.1.
Example
7.
More Examples
7.1.
Example 1: Creating a List Using Templates
7.2.
Example 2: Adding Dynamic Content
8.
Frequently Asked Questions
8.1.
What is the <template> tag used for in HTML? 
8.2.
Can we use <template> in all browsers?
8.3.
What are the advantages of using HTML templates? 
9.
Conclusion
Last Updated: Dec 23, 2024
Easy

HTML Template

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

HTML templates serve as the foundation for creating structured web pages. They provide a reusable structure that web developers use to design, organize, and standardize the layout of web content. 

HTML Template

In this article, we will discuss the definition, usage, browser support, and global attributes of HTML templates. Examples will be provided to make the concepts clear.

Definition and Usage

The <template> tag is a container for holding HTML code that is not rendered immediately when the page loads. This content can be later inserted into the DOM using JavaScript. It is particularly useful for creating reusable components or delaying the rendering of specific sections of a page.

Key Points

  • Not Rendered by Default: Content inside the <template> tag is inert, meaning it doesn't affect the document's visual presentation.
     
  • Reusable Components: Helps in creating modular and reusable UI components.
     
  • Improves Performance: Avoids unnecessary rendering and enhances load times.

Syntax

<template>
    <!-- Your reusable HTML content here -->
</template>

Example

An example of an HTML template helps us understand its structure and how it functions within a webpage.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Template Example</title>
    <style>
        .content {
            font-family: Arial, sans-serif;
            color: #333;
        }
    </style>
</head>
<body>
    <template id="myTemplate">
        <div class="content">
            <h2>Welcome to Coding Ninjas!</h2>
            <p>This is an example of using an HTML template.</p>
        </div>
    </template>

    <script>
        const template = document.getElementById('myTemplate');
        const clone = template.content.cloneNode(true);
        document.body.appendChild(clone);
    </script>
</body>
</html>


Output

Output

Explanation

  1. The <template> tag defines a block of HTML that isn't rendered immediately on the page.
     
  2. The id="myTemplate" uniquely identifies the template.
     
  3. JavaScript is used to clone and append the content of the template to the webpage.
     
  4. When you run this code, it dynamically adds the content inside the <template> tag to the page.

Browser Support

The <template> tag is widely supported by modern browsers. However, older versions may not support it fully. Below is a table of browser compatibility:

BrowserVersion Supported
Google Chrome26+
Mozilla Firefox22+
Microsoft Edge13+
Safari6.1+
Opera15+

Global Attributes

The <template> tag supports all global attributes. Some commonly used ones include:

  1. id Attribute: Used to uniquely identify a template for referencing in JavaScript.
     
  2. class Attribute: Adds CSS styles to templates if necessary.

Example

<template id="exampleTemplate">
    <div class="styled">
        <p>Reusable content with styles.</p>
    </div>
</template>
<style>
    .styled {
        background-color: #f0f0f0;
        padding: 10px;
        border: 1px solid #ccc;
    }
</style>

More Examples

Example 1: Creating a List Using Templates

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Template Example</title>
</head>
<body>
    <template id="listTemplate">
        <li></li>
    </template>

    <ul id="list"></ul>
    <script>
        const items = ['HTML', 'CSS', 'JavaScript'];
        const template = document.getElementById('listTemplate');
        const list = document.getElementById('list');


        items.forEach(item => {
            const clone = template.content.cloneNode(true);
            clone.querySelector('li').textContent = item;
            list.appendChild(clone);
        });
    </script>
</body>
</html>


Explanation

This example dynamically generates a list of items (HTML, CSS, JavaScript) using the <template> tag. The JavaScript code clones the template's content and appends it to the list.
 

Output

Output

Example 2: Adding Dynamic Content

<template id="dynamicTemplate">
    <div>
        <h3></h3>
        <p></p>
    </div>
</template>

<script>
    const data = [
        { title: 'Learn HTML', description: 'HTML forms the backbone of the web.' },
        { title: 'Learn CSS', description: 'CSS styles web content.' },
        { title: 'Learn JavaScript', description: 'JavaScript makes webpages interactive.' }
    ];

    const template = document.getElementById('dynamicTemplate');

    data.forEach(item => {
        const clone = template.content.cloneNode(true);
        clone.querySelector('h3').textContent = item.title;
        clone.querySelector('p').textContent = item.description;
        document.body.appendChild(clone);
    });
</script>

Frequently Asked Questions

What is the <template> tag used for in HTML? 

The <template> tag is used to define HTML content that is not rendered on page load but can be reused or rendered later using JavaScript.

Can we use <template> in all browsers?

 Yes, <template> is supported by most modern browsers. For older browsers, you may need to use polyfills.

What are the advantages of using HTML templates? 

HTML templates improve code reusability, reduce redundancy, and enhance performance by delaying rendering until necessary.

Conclusion

HTML templates are a powerful tool for creating reusable, dynamic, and efficient web components. By leveraging the <template> tag, developers can reduce redundancy, enhance performance, and create modular designs. Mastering this feature will make you a more efficient web developer.

Live masterclass