Modern Interactive HTML · 10 min read · Beginner
<template>, <slot> & Web Components
Reuse HTML fragments with <template> and build encapsulated custom elements with Shadow DOM and <slot>, using pure browser standards.
What you'll learn
- Store inert, reusable HTML markup inside <template>
- Define a native Custom Element with customElements.define()
- Project user content into a Shadow DOM using <slot>
HTML has its own built-in component system (Web Components) powered by three native primitives:
<template>: Holds HTML markup that the browser parses but does not render until your JavaScript clones it (template.content.cloneNode(true)).- Custom Elements: Register your own HTML tag (must include a hyphen, like
<course-badge>). - Shadow DOM +
<slot>: Encapsulates component CSS so styles never leak in or out, while<slot>lets callers inject their own inner HTML.
Fill-in-the-Blank Exercise
Template & Slot Basics
Define an inert HTML template with a default slot for projected content.
< id="badge-tpl">
<span class="pill"><></slot></span>
</template>Try It Yourself: Build a Native <tip-card> Web Component
Notice how <tip-card> has scoped CSS inside its Shadow DOM and accepts custom content through <slot>:
Native Web Component with <template> & <slot>
Live Result
Console Output
No console output yet. Try calling console.log(...) in JS.
Check Your Understanding
Score: 0 / 1
1. Which of the following is a valid Custom Element tag name?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.