Template Slot Shadow DOM Component

Define a reusable component using template and slot elements inside a shadow DOM root for encapsulation.

HtmlData Integrity

Code

htmlsnippet
15 lines
<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

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.