<template>...</template>

HTML4 HTML5

The <template> tag defines client-side HTML storage mechanism for markup not rendered on initial page load[cite: 3]. Content remains hidden/passive until cloned and inserted via JavaScript[cite: 3].

Essential for dynamic interfaces and Web Components[cite: 3].

Both opening and closing tags are mandatory[cite: 3].

Attributes:

Example

Code:

<!-- Hidden template -->
<template id="notification-template">
<div style="background-color: #e6f7ff; border: 1px solid #91d5ff; padding: 8px; margin-top: 5px; border-radius: 4px;">
<strong>Notification:</strong> New item added! </div>
</template>
<div id="container"></div>
<button onclick="addNotification()" style="margin-top: 10px;">Show template notification</button>
<script>
function addNotification() {
const template = document.getElementById('notification-template');
const clone = template.content.cloneNode(true);
document.getElementById('container').appendChild(clone);
}
</script>

The browser will interpret this code as follows: