Template & Web Components in HTML

From the HTML cheat sheet ยท Interactive & Web Components ยท verified Jul 2026

Template & Web Components

Reusable HTML templates and Web Component building blocks

html
<!-- Template (not rendered until cloned) -->
<template id="card-template">
  <div class="card">
    <h2 class="card-title"></h2>
    <p class="card-body"></p>
  </div>
</template>

<script>
  const template = document.getElementById('card-template');
  const clone = template.content.cloneNode(true);
  clone.querySelector('.card-title').textContent = 'Hello';
  document.body.appendChild(clone);
</script>
๐Ÿ’ก <template> content is parsed but NOT rendered โ€” use cloneNode to stamp out copies
โšก Templates are perfect for list items, table rows, and any repeated HTML structures
๐Ÿ“Œ <slot> lets consumers inject content into Web Components โ€” named slots target specific areas
๐ŸŸข Web Components (custom elements + shadow DOM + templates) work in all modern browsers
templateslotweb-components

More HTML tasks

Back to the full HTML cheat sheet