Template, slot & custom elements
7 min lesson
- The <template> tag holds HTML that is not shown until your script clones it, ideal for repeated pieces like cards.
- The <slot> tag defines a placeholder inside a custom element where the user's content goes.
- A custom element is a tag you invent, like <user-card>, whose behavior is defined with JavaScript's class syntax.
- Think of a template as a cookie cutter: it shapes many identical copies without rewriting the recipe.
- This builds on HTML with APIs & data attributes: scripts now create and control whole chunks of HTML.
- The example clones a template three times and defines a tiny custom element; you will see three cards and a greeting.
- Next, Modern HTML: popover, inert & new features adds newer built-in controls.
- Use template for content you reuse; use custom elements for reusable behavior.
- Template content is inert: scripts do not run and images do not load until it is cloned.
- Custom element names must contain a dash, like <user-card>.
- Never wrap slotted content in a plain div inside your custom element's shadow root and call it done; the slot is the real entry point.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Templates & custom elements</title>
</head>
<body>
<!-- The template is invisible until cloned -->
<template id="card">
<div class="card">
<h3></h3>
<p></p>
</div>
</template>
<!-- A target area for the cloned cards -->
<div id="list"></div>
<script>
// Clone the template and fill in the slots
const card = document.getElementById('card').content;
const list = document.getElementById('list');
['Tea', 'Coffee', 'Juice'].forEach(function (name) {
const copy = card.cloneNode(true);
copy.querySelector('h3').textContent = name;
copy.querySelector('p').textContent = 'Price: 100';
list.appendChild(copy);
});
</script>
</body>
</html>What happens: A page showing three cards, each with a heading ("Tea", "Coffee", "Juice") and the paragraph "Price: 100".
Breaking it down
template
<template id="card">...</template>Holds HTML that is not rendered or run until script clones it. The content lives in .content.
cloneNode
card.cloneNode(true)Makes a real copy of the template content. true means copy child nodes too; false copies only the wrapper.
querySelector
copy.querySelector('h3')Finds the heading inside the copy so you can set its text. Each copy is filled independently.
slot
<slot></slot>In a custom element, this is the place where the user's own content goes.
Template & slot tags
| Tag | Purpose |
|---|---|
| template | Holds HTML to be cloned |
| slot | A placeholder inside a custom element |
| shadow root | An isolated DOM inside a custom element |
| custom element | A tag you define with a dash |
A helpful analogy
A template is a cookie cutter, and cloning is pressing it into dough. Each cookie looks the same, but you can add a different icing (the data) to each one.
Nesting rules
- 1.Custom element names must contain a dash, like <user-card>, so they never clash with built-in tags.
- 2.Slot content comes from the user's HTML; the custom element decides where it lands.
Common mistakes
Adding a template without a script to clone it
Avoid
<template>...</template> aloneUse instead
Clone it with JavaScript and append it to the pageTemplate content is invisible. Without a script to clone and insert it, nothing appears.
Using a custom element name without a dash
Avoid
<usercard>Use instead
<user-card>Names without a dash are reserved for built-in elements. The custom element will not register.
Quick tips
- •Keep the template markup simple; logic belongs in the script, not inside the template.
- •Use DevTools to inspect the shadow root of a custom element to see how slot content is placed.