Template Slot Shadow DOM Component
Define a reusable component using template and slot elements inside a shadow DOM root for encapsulation.
Code
<template id="card-template">
<style>
.card { border: 1px solid #ccc; border-radius: 8px; padding: 16px; }
h3 { margin: 0 0 8px; }
</style>
<div class="card">
<h3><slot name="title">Default Title</slot></h3>
<p><slot>Default content</slot></p>
</div>
</template>
<my-card>
<span slot="title">Custom Title</span>
Custom content here
</my-card>How the snippet works
<template> holds inert markup that only becomes live when cloned into the document.
<slot> names the insertion point where light-DOM children land inside a shadow tree.
Script and styles anchor onto ids card-template and classes card, so behavior and styling stay decoupled from the tag names.
Statements that matter
<template id="card-template">
<h3><slot name="title">Default Title</slot></h3>
<p><slot>Default content</slot></p>
Interactive Tools for This Topic
Box Shadow GeneratorBuild CSS box-shadow values with offset, blur, spread, color, opacity, and inset controls, with a live preview of the result.DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- Which non-generic elements does the markup use?
- `<template>`, `<h3>`, `<slot>`, `<p>`, `<my-card>` — each carries native semantics (or a browser behavior) that generic containers do not.
Report a problem with this exampleOpens a pre-filled issue on GitHub.