<template>

An invisible HTML template that a script copies and inserts into the page.

Display
none
Closing tag
Required
Permitted content
Any content, stored but not displayed
On this page

Example

#
HTML
<ul id="courses"></ul>

<template id="course-row">
  <li><strong class="name"></strong> · number of lessons: <span class="lessons"></span></li>
</template>

<script>
  const courses = [
    { name: 'HTML from scratch', lessons: 24 },
    { name: 'CSS and Flexbox', lessons: 18 },
    { name: 'JavaScript for beginners', lessons: 32 }
  ]
  const template = document.getElementById('course-row')
  const list = document.getElementById('courses')

  courses.forEach((course) => {
    const item = template.content.cloneNode(true)
    item.querySelector('.name').textContent = course.name
    item.querySelector('.lessons').textContent = course.lessons
    list.append(item)
  })
</script>
Preview

Definition and usage

#

The <template> element holds a fragment of HTML that is neither displayed nor run when the page loads. Images in a template aren’t fetched, scripts don’t execute, and document.querySelector can’t find its elements. The template waits until you use it in JavaScript.

The template’s contents are available through the content property. You call template.content.cloneNode(true), fill the copy with data, and insert it into the document. This is how you build repeated elements: table rows, product cards, chat messages.

A template is also the foundation of web components. With the shadowrootmode attribute, the browser immediately turns it into the Shadow DOM of the element it sits in, without any JavaScript.

Syntax

#
Syntax
<template id="template">
  …
</template>

Attributes

#
  • shadowrootmode

    open | closed

    Turns the template into the Shadow DOM of its parent element while the page is loading (so-called declarative Shadow DOM). open lets scripts look inside through element.shadowRoot, while closed doesn’t. Styles inside the Shadow DOM don’t leak out, and page styles don’t get in, as the example shows.

The element also supports all global attributes, such as class, id, style and hidden.

Attributes in practice

#

shadowrootmode

open | closed

Turns the template into the Shadow DOM of its parent element while the page is loading (so-called declarative Shadow DOM). open lets scripts look inside through element.shadowRoot, while closed doesn’t. Styles inside the Shadow DOM don’t leak out, and page styles don’t get in, as the example shows.

HTML
<style>
  p { color: #f97316; }
</style>
<p>This paragraph is orange because the page style applies to it.</p>
<div>
  <template shadowrootmode="open">
    <p>This paragraph is in the Shadow DOM, so the page style doesn’t apply to it.</p>
  </template>
</div>
Preview

Default CSS

#

Most browsers display this element with these default styles:

CSS
template {
  display: none;
}

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • When filling a copy of the template with data, use textContent rather than innerHTML. That protects you from injecting foreign code.
  • Remember the argument in cloneNode(true). Without true you copy only the empty fragment, without the elements inside.
  • You can put a template anywhere in the document, including in <head> or inside a table. It won’t break the layout, because it isn’t displayed.

Related entries

#

Spotted a mistake or missing an example? Write to us.