
- HTML Home
- HTML Roadmap
- HTML Introduction
- HTML History & Evolution
- HTML Editors
- HTML Basic Tags
- HTML Elements
- HTML Attributes
- HTML Headings
- HTML Paragraphs
- HTML Fonts
- HTML Blocks
- HTML Style Sheet
- HTML Formatting
- HTML Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - <template> Tag
Introduction to <template> Tag
The HTML <template> tag is used to hold HTML or client-side content hidden from the user when the page loads. The browser evaluates the content of the template tag to ensure it is valid, but the contents are not displayed. The template's content will only be shown if activated using JavaScript.
If we want to use the same content multiple times in our HTML document without any changes, we can use the <template> tag.
The <template> tag can be used anywhere within an HTML document, including inside <head>, <body>, <frameset>, or <table> elements.
Syntax
Following is the syntax of <template> tag −
<template>.....</template>
Attributes
The HTML <template> tag supports Global and Event attributes.
Example: Hiding Content
We can hide any content inside the browser will loads the page and use a button as a trigger to render that element later. In the following example, we use the <template> tag to hold the content when the page loads, and later display the hidden content using JavaScript.
<!DOCTYPE html> <html> <body> <h1>The template Element</h1> <p> When you click the button below, JavaScript is activated, and hidden content will become visible! </p> <button onclick="showContent()">Show hidden content</button> <template> <h2>Tutorialspoint</h2> <p>Tutorialspoint: <q> is an EdTech organisation that provides courses related to CSE and so many tutorials and DSA solutions. <q> </p> <p>Easy to learn!</p> </template> <script> function showContent() { let temp = document.getElementsByTagName("template")[0]; let clon = temp.content.cloneNode(true); document.body.appendChild(clon); } </script> </body> </html>
Example: Hiding Image
We can hide any content inside of template tag when the browser loads the page and use a button as a trigger to render that element later. In the following example, we use the <template> tag to hide the image when the page loads, and later display the hidden content using JavaScript.
<!DOCTYPE html> <html> <head> <title>HTML Template tag</title> </head> <body> <h2>Example of template tag</h2> <button onclick="clickMe()">Click Me</button> <br> <template id="mytemplate"> <img src="https://www.tutorialspoint.com/images/logo.png?v2" alt="logo"> <script> alert("Thank you for choosing template. Click OK for tutorialspoint Logo.") </script> </template> <script> function clickMe() { var x = document.getElementsByTagName("template")[0]; var clon = x.content.cloneNode(true); document.body.appendChild(clon); } </script> </body> </html>
Supported Browsers
Tag | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
template | Yes 26.0 | Yes 13.0 | Yes 22.0 | Yes 8.0 | Yes 15.0 |