Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

The `<template>` Element – Declaring In | HTML Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. HTML Course

Lesson 44 of 55

·HTML

The `<template>` Element – Declaring Inert Template Content

9 min

Course progress80%

What you'll learn

Keep these ideas in mind as you work through the lesson.

5 points
  • Inert content storage with the `<template>` element
  • Cloning template content using `document.importNode`
  • Performance benefits of deferring DOM insertion
  • Use‑cases for repetitive UI (cards, list items)
  • Combining `<template>` with custom elements for component libraries

The <template> Element – Inert Markup for Cloning

<template> stores markup that isn’t rendered until you clone it via JavaScript.

<template id='cardTemplate'>
  <article class='card'>
    <h3></h3>
    <p></p>
    <a href='' class='more'>Read more</a>
  </article>
</template>

<div id='grid'></div>
<script>
  const data = [
    { title: 'Project One', desc: 'Brief description', url: '#1' },
    { title: 'Project Two', desc: 'Brief description', url: '#2' }
  ];
  const tmpl = document.getElementById('cardTemplate').content;
  const grid = document.getElementById('grid');
  data.forEach(item => {
    const clone = document.importNode(tmpl, true);
    clone.querySelector('h3').textContent = item.title;
    clone.querySelector('p').textContent = item.desc;
    clone.querySelector('a').href = item.url;
    grid.appendChild(clone);
  });
</script>

Benefits

  • Zero‑render cost – Content inside <template> isn’t parsed into the DOM until you explicitly clone it.
  • Reusable – Keep HTML DRY and separate data from markup.
  • Safe – Anything inside <template> cannot execute scripts or load resources until instantiated.

Quick checklist for <template>

  1. Template stored in a <template id='…'> element? ✅
  2. Cloned via document.importNode with deep:true? ✅
  3. Scripts or heavy resources not loaded before cloning? ✅
  4. Used for repeated UI components (cards, list items)? ✅
  5. Fallback content provided for browsers without <template> (rare)? ✅

Tip: Combine <template> with Web Components to build fully encapsulated, reusable UI patterns.

Make it stickPractice what you've learned1 quiz · 2 practical tests · Optional activities from HTML CourseBrowse activities
Previous lessonThe `<dialog>` Element – Native Modal DialogsContinue to lesson 45Inline Text TagsBack to HTML Course curriculum