Template & Slot
<template> holds inert HTML fragments. Inside, <slot> marks where dynamic content plugs in — the declarative core of Web Components.
Template Markup (inert)
<template id="card-tpl">
<article class="tpl-card">
<header>
<slot name="title">Default title</slot>
</header>
<p><slot></slot></p>
<footer><slot name="meta">—</slot></footer>
</article>
</template>
Rendered result (simulated)
Reusable UI component with its own shadow DOM and scoped styles.
Server-rendered initial HTML hydrated by client-side JS later.
Three things to know
- Inert: content inside
<template>isn't parsed as DOM — no images load, no scripts run. - Named vs default slots:
<slot name="x">matchesslot="x"; unnamed<slot>captures all remaining children. - Fallback content: what's between
<slot>and</slot>shows only when nothing fills it.
Static HTML can show only the structure — activating <template> and distributing children into slots requires JavaScript. This example visualizes what the DOM would look like after activation.