Concept Explanation
If you have a complex UI (like a User Profile Card) that you need to use 100 times, you shouldn't write the HTML inside a JavaScript string. It's hard to read and easy to break.
The <template> tag lets you write real HTML that the browser parses but never displays. It sits in the document like a cookie cutter: inert, reusable, and waiting for you to stamp out copies with cloneNode(). Because the browser parses it once up front, stamping out the hundredth copy is dramatically cheaper than re-parsing an HTML string a hundred times.
Slots are the holes in the cookie cutter. A template defines the structure; a slot marks the spot where the caller drops in their own content. That is how <user-card> can look identical everywhere while showing a different name each time.
Syntax or Core Structure
The Template tag
<template id="user-card-template">
<style> .card { border: 1px solid silver; } </style>
<div class="card">
<h2><slot name="username">Default Name</slot></h2>
<p><slot name="bio">No bio available.</slot></p>
</div>
</template>
Cloning the Template
const template = document.getElementById('user-card-template');
const content = template.content.cloneNode(true);
document.body.appendChild(content);
Filling the Slots
The caller assigns content to a slot with the slot attribute. Anything without a slot attribute lands in the unnamed default slot.
<user-card>
<span slot="username">Ada Lovelace</span>
<span slot="bio">First programmer. Wrote loops before computers existed.</span>
</user-card>
Wiring It Into a Component
class UserCard extends HTMLElement {
connectedCallback() {
const tpl = document.getElementById('user-card-template');
this.attachShadow({ mode: 'open' })
.appendChild(tpl.content.cloneNode(true));
}
}
customElements.define('user-card', UserCard);
Key Features
template.contentis aDocumentFragment, not an element. It is a lightweight bag of nodes with no parent.cloneNode(true)means "deep copy, children included." Passfalseand you get an empty shell.- Slot fallback: the text you write inside
<slot>Default Name</slot>shows only when the caller supplies nothing. - Slotted nodes stay in the light DOM. They are projected into the shadow tree, not moved.
document.querySelectorcan still find them.
Common Mistakes
- Appending
template.contentdirectly instead of a clone. A fragment can only live in one place, so the first append empties the template and every later copy comes out blank. - Styling slotted content with a normal selector. Inside a shadow root you need
::slotted(span), and it only reaches the top-level projected node, not its descendants. - Expecting slots to work without Shadow DOM. Slots are a shadow-tree feature. In a plain template clone they render as empty
<slot>tags.
Best Practices
- Keep the template markup in HTML, not in a JS string, so your editor still gives you syntax highlighting and your linter still works.
- Look the template up once and cache it on the class rather than calling
getElementByIdper instance. - Give every slot a sensible fallback so a half-configured component still looks intentional.
Practice Exercises
- The Badge Template: Create a
<template>for a simple notification badge. - The Slot Machine: Create a Web Component that uses a template and has two slots: "title" and "description".
- The Styling Challenge: Research
::slotted()CSS selector and use it to style an element that was passed into a slot from the main document.