template
Stores inert reusable markup.
Learn HTML Template with practical examples for rich HTML features, native browser behavior, accessibility, performance, SEO and live practice.
HTML advanced
A template is inert. The browser parses its contents, but it does not display them and scripts inside it do not run immediately.
Templates are useful for repeated cards, list items, table rows, modals and Web Component internals.
The key idea is separation: HTML can define a reusable structure, while JavaScript clones and fills it when needed.
Stores inert reusable markup.
The document fragment inside the template.
Copies the template content.
A placeholder often used inside Web Components.
Syntax and behavior
A template is not hidden UI. It is a source for future UI.
<template id="lesson-template"> <article class="lesson-card"> <h2></h2> <p></p> </article> </template> <div id="lessons"></div>
<template> <h2>This will not appear automatically</h2> </template>
HTML quick reference
Use these patterns when you need the syntax quickly. Each example has its own anchor, so search engines and readers can land directly on the exact pattern instead of only at the top of the lesson.
A clean version of the markup from this lesson. Use it when you need the correct HTML shape quickly.
<template id="lesson-template"> <article class="lesson-card"> <h2></h2> <p></p> </article> </template> <div id="lessons"></div>
Meaningful markup that stays understandable before CSS and JavaScript are added.
The starting point from the practice lab. Change the HTML first, then use CSS only for presentation.
<main class="demo-card"> <h1>Lesson list</h1> <template id="lesson-template"> <article class="lesson-card"><h2></h2><p></p></article> </template> <div id="lessons"></div> </main> <script> const template = document.querySelector("#lesson-template"); const clone = template.content.cloneNode(true); clone.querySelector("h2").textContent = "HTML Template"; clone.querySelector("p").textContent = "Reusable markup, filled safely."; document.querySelector("#lessons").append(clone); </script> <style> body { margin: 0; min-height: 100vh; display: grid; place-items: center; font-family: Inter, system-ui, sans-serif; background: #07111f; color: white; } .demo-card { width: min(820px, calc(100% - 32px)); padding: 34px; border-radius: 24px; background: #101a2d; box-shadow: 0 30px 80px rgba(0, 0, 0, 0.3); } h1 { margin: 0 0 16px; font-size: clamp(34px, 7vw, 58px); line-height: 1; } h2 { margin: 22px 0 10px; } p, li { color: #cfd8e6; line-height: 1.7; } button { border: 0; border-radius: 999px; padding: 12px 16px; background: #8cffc1; color: #07111f; font-weight: 900; } a { color: #8cffc1; font-weight: 900; } .lesson-card { border: 1px solid rgba(140, 255, 193, 0.28); border-radius: 18px; padding: 18px; } </style>
A complete practice snippet that shows how the HTML behaves in context.
A weak pattern from the lesson. Use it as a warning sign when reviewing real pages.
<template> <h2>This will not appear automatically</h2> </template>
A recognizable mistake you can search for and refactor.
Rules that matter
These elements can create rich interfaces, but they still need clear purpose, safe fallbacks and production discipline.
Templates are ideal when JavaScript creates many similar items.
It should be understandable before JavaScript fills it.
Use template.content.cloneNode(true).
Do not inject unsafe user HTML.
Use hidden or CSS when you only need to hide existing content.
Template content should still use meaningful elements.
Production thinking
This matters because template gives you reusable HTML without rendering it too early or building strings by hand.
Cloned template content must still contain headings, labels, button names and focus behavior like normal HTML.
Templates are useful in small vanilla JavaScript apps and inside Web Components, but data escaping still matters.
Content that only exists after JavaScript cloning may not be as reliable for indexing as server-rendered HTML.
Live code lab
Edit the HTML or CSS, then use Run to refresh the preview. The preview is isolated, so links and forms stay inside this practice area.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read this page. Answer these questions out loud or write the answers in your own notes. If one answer feels vague, revisit the examples before moving on.