On this page9 sections
  1. Learning Objectives
  2. 1. <details> and <summary>
  3. 2. Accordions: Only One Open at a Time
  4. 3. <dialog>: Pop-up Windows
  5. 4. <template>: Reusable HTML
  6. Common Beginner Mistakes
  7. Practice Questions
  8. Practice Answers
  9. Key Takeaways

Lecture 038: Details, Dialog & Template

Category7: Interactive Elements, Graphics & Media
Lecture038 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

Some interactive pieces used to need a lot of JavaScript: a section that opens and closes, a pop-up window, a block of HTML you reuse. HTML now has elements for each of them: <details>, <dialog> and <template>.


Learning Objectives

  • Make open/close sections with <details> and <summary>
  • Build accordion groups with the name attribute
  • Show modal and non-modal pop-ups with <dialog>
  • Store reusable HTML in <template> and copy it with JavaScript

1. <details> and <summary>

Example: click the summary line:

html
<details>
  <summary>What is your refund policy?</summary>
  <p>You can return any item within 14 days for a full refund.</p>
</details>

<details open>
  <summary>Do you deliver outside Lahore?</summary>
  <p>Yes, to every city in Pakistan.</p>
</details>
  • <summary> is the always-visible line; the rest shows when opened.
  • open starts it expanded.
  • No JavaScript needed, and it works with the keyboard.

2. Accordions: Only One Open at a Time

Give several <details> the same name. Opening one closes the others.

Example

html
<details name="faq"><summary>Shipping</summary><p>2–4 working days.</p></details>
<details name="faq"><summary>Payment</summary><p>Card, bank transfer or cash on delivery.</p></details>
<details name="faq"><summary>Returns</summary><p>Within 14 days.</p></details>

3. <dialog>: Pop-up Windows

Example

html
<dialog id="confirm">
  <p>Delete this photo?</p>
  <form method="dialog">
    <button value="cancel">Cancel</button>
    <button value="delete">Delete</button>
  </form>
</dialog>

<button type="button" onclick="document.getElementById('confirm').showModal()">Delete photo</button>
<p id="result"></p>

<script>
  const dlg = document.getElementById('confirm');
  dlg.addEventListener('close', () => {
    document.getElementById('result').textContent = 'You chose: ' + dlg.returnValue;
  });
</script>
MethodBehaviour
showModal()Opens on top, dims the page, traps focus, Esc closes it
show()Opens without blocking the rest of the page
close()Closes it

A <form method="dialog"> inside closes the dialog, and the clicked button's value becomes returnValue.

Style the dimmed background with CSS ::backdrop (CSS Lecture 047).


4. <template>: Reusable HTML

Content inside <template> isn't shown. JavaScript copies (clones) it whenever needed.

Example

html
<template id="card">
  <div style="border: 1px solid #dee2e6; border-radius: 8px; padding: 10px; margin: 6px 0;">
    <strong class="name"></strong> - <span class="city"></span>
  </div>
</template>

<div id="list"></div>

<script>
  const people = [['Ayesha', 'Karachi'], ['Bilal', 'Multan'], ['Sana', 'Gilgit']];
  const tpl = document.getElementById('card');
  for (const [name, city] of people) {
    const copy = tpl.content.cloneNode(true);
    copy.querySelector('.name').textContent = name;
    copy.querySelector('.city').textContent = city;
    document.getElementById('list').append(copy);
  }
</script>

Common Beginner Mistakes

1. A clickable <div> to toggle content. <details> does it with better accessibility.

2. Forgetting <summary>. The browser then shows a generic "Details" label.

3. show() when you mean showModal(). Only showModal() blocks the page and supports Esc.

4. Expecting <template> content to appear. It's inert until JavaScript clones it.


Practice Questions

  1. Which element is always visible inside <details>?
  2. How do you make a <details> start open?
  3. How do you build an accordion where only one item is open?
  4. Which method opens a dialog that blocks the page?
  5. What does <form method="dialog"> do?
  6. Is the content inside <template> shown on the page?

Practice Answers

  1. <summary>
  2. Add the open attribute.
  3. Give every <details> the same name.
  4. showModal()
  5. Submitting it closes the dialog and sets returnValue to the button's value.
  6. No. It's only shown after JavaScript clones it into the page.

Key Takeaways

  • <details> + <summary> = an open/close section with no JavaScript.
  • Same name on several <details> = an accordion.
  • <dialog> with showModal() = an accessible pop-up.
  • <template> holds HTML to clone with JavaScript.