Skip to main content

The <template> Element

As defined in the HTML Living Standard, the <template> element holds HTML content that is not rendered when the page loads, but can be instantiated later via JavaScript.

Example:
<template id="row-template">
  <tr>
    <td>Data 1</td>
    <td>Data 2</td>
  </tr>
</template>

Templates are highly efficient for creating repetitive UI elements without cluttering the initial DOM.

View HTML Living Standard: The template element

Accessibility Impact: The "Invisible" Content

Because content inside a <template> is not part of the document until it is cloned, it is completely invisible to screen readers and search engines.

WCAG Requirement: Predictable Navigation

Success Criterion 3.2.3 (Level AA): Consistent Navigation.

When you use a template to inject new content into the page, you must ensure that the new content follows the same structural and navigational patterns as the rest of the site. If a template injects a new form or a set of controls, you must manage the focus so the user knows where the new content has appeared.

Understand SC 3.2.3: Consistent Navigation

Best Practices for Accessible Templates

A11y Tip: Template vs. Hidden

Unlike display: none;, which hides an element but keeps it in the DOM, the <template> tag is truly inert. This is better for performance and ensures that screen readers don't accidentally find "hidden" content that isn't yet ready for interaction.