AdvancedLesson 25 of 306 min read

Interactive HTML without JavaScript: details, dialog and popover

Build accordions, popups and tooltips with the native details, summary, dialog and popover features.

In this lesson you will

  • Build an FAQ with details and summary
  • Open a modal with dialog
  • Use popover for tooltips and menus
  • Choose native over custom

About 13 minutes

Interactivity the browser gives you for free

Priya wants an FAQ on her bakery site: a list of questions where clicking one reveals the answer. She has heard that needs JavaScript and a library. Good news: HTML can do it by itself, with two elements that have been supported in every major browser for years.

details and summary

<details>
  <summary>Do you deliver?</summary>
  <p>Yes, within 10 km of the shop for orders over Rs 2,000.</p>
</details>

The <summary> is the always-visible heading. Clicking it opens and closes the rest of the <details>. It works with the keyboard, is announced correctly by screen readers, and needs no code at all.

Press the questions to open them. Then add a third details with your own question, and add the open attribute to the first so it starts expanded.

<details>
  <summary>Do you deliver?</summary>
  <p>Yes, within 10 km of the shop.</p>
</details>
<details>
  <summary>Can I order a day before?</summary>
  <p>Cakes need 48 hours notice.</p>
</details>

Add the word open inside the first details tag. Copy a block for the third question.

<details open>
  <summary>Do you deliver?</summary>
  <p>Yes, within 10 km of the shop.</p>
</details>
<details>
  <summary>Can I order a day before?</summary>
  <p>Cakes need 48 hours notice.</p>
</details>
<details>
  <summary>Do you make vegan cakes?</summary>
  <p>Yes, ask when you order.</p>
</details>
  • The open attribute makes a details element start expanded.
  • To make an exclusive accordion where opening one closes the others, give the elements the same name attribute: <details name="faq">. Modern browsers support this.
  • You can style the marker triangle with summary::marker or hide it and draw your own.

Dialog: a built-in popup

Popups used to need a pile of code to trap the keyboard, dim the background and close on the Escape key. The <dialog> element does all that for you.

<button id="open" type="button">Order now</button>

<dialog id="order">
  <form method="dialog">
    <h2>Confirm your order</h2>
    <p>Pick up tomorrow at 9 a.m.?</p>
    <button value="cancel">Cancel</button>
    <button value="ok">Confirm</button>
  </form>
</dialog>

<script>
  const dlg = document.getElementById('order');
  document.getElementById('open').addEventListener('click', () => dlg.showModal());
</script>
  • showModal() opens it as a modal: the page behind is inert, focus is trapped inside, and Escape closes it.
  • show() opens a non-modal version.
  • A form with method="dialog" closes the dialog when a button is pressed, and the button’s value becomes the dialog’s returnValue.
  • Style the dimmed background with dialog::backdrop.

Press Run, open the dialog, then add a second button with the text Close that closes it. Hint: a form with method=dialog does it with no extra script.

<button id="b" type="button">Open</button>
<dialog id="d">
  <p>Hello from a dialog!</p>
</dialog>
<script>
 document.getElementById('b').onclick = function(){ document.getElementById('d').showModal(); };
</script>

Wrap a button in inside the dialog.

<button id="b" type="button">Open</button>
<dialog id="d">
  <p>Hello from a dialog!</p>
  <form method="dialog"><button>Close</button></form>
</dialog>
<script>
 document.getElementById('b').onclick = function(){ document.getElementById('d').showModal(); };
</script>
Good to know

Some practice editors block popups and modals inside their small preview frame. If the dialog does not open here, the code is still right and will work on your own page.

More built-in widgets

  • <progress value="70" max="100"></progress> shows how far something has gone.
  • <meter value="0.6"></meter> shows a value within a known range, like disk usage.
  • <input type="range"> is a slider.
  • <output> displays the result of a calculation.
  • <abbr title="HyperText Markup Language">HTML</abbr> explains an abbreviation on hover.
  • popover is a newer attribute for tooltips and menus: <button popovertarget="tip"> toggles <div id="tip" popover> with no script.
<button popovertarget="help">What is sourdough?</button>
<div id="help" popover>
  Bread raised with wild yeast and lactic bacteria instead of packaged yeast.
</div>

Why native beats custom

Building your own accordion from divs means writing code for keyboard control, focus order, screen reader announcements and touch support. Every one of those is a chance for a bug. Native elements have all of it built in and tested by browser makers. Choose the native element first, and build custom only when it truly cannot do the job.

Common mistake

Do not hide essential information in a details element that starts closed. Search engines and some visitors may not expand it. Use details for secondary information like FAQs, not for the main point of the page.

Styling the built-in widgets

Native does not mean plain. With a little CSS, details and dialog can match your brand. A few ideas to try on your own pages:

details { border: 1px solid #E6E1F5; border-radius: 12px; padding: 12px 16px; margin-bottom: 10px; }
summary { cursor: pointer; font-weight: 700; }
details[open] summary { color: #5A28C8; margin-bottom: 8px; }

dialog { border: 0; border-radius: 20px; padding: 24px; max-width: 420px; }
dialog::backdrop { background: rgba(20, 17, 46, .55); }

Notice the attribute selector details[open]. The browser adds the open attribute while the item is expanded, so you can style the open state without any script. This links straight back to the previous lesson on attributes.

Accessibility notes

  • Keep the summary text short and descriptive. Screen readers announce it as a button that can be expanded.
  • A modal dialog should always have a visible way to close it and a heading that names it.
  • After a dialog closes, focus returns to the button that opened it. Do not interfere with that behaviour.
  • Avoid popups that appear by themselves the moment someone arrives. They interrupt, annoy and often break search engine guidelines.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

Which element is the always-visible heading of an accordion item?

  1. <label>
  2. <summary>
  3. <legend>
  4. <caption>

summary sits inside details and acts as the toggle.

How do you open a dialog as a modal?

  1. dlg.open()
  2. dlg.showModal()
  3. dlg.popup()
  4. dlg.display()

showModal opens it with a backdrop and focus trapping.

What does method="dialog" on a form do?

  1. Sends data to a server
  2. Closes the dialog on submit
  3. Hides the form
  4. Reloads the page

A dialog form closes the dialog and sets its returnValue.

Which attribute makes a details element start open?

  1. expanded
  2. open
  3. show
  4. visible

The boolean open attribute shows the content.

Why prefer native elements to custom ones?

  1. They look nicer
  2. They include keyboard and accessibility support
  3. They are required
  4. They are always faster to style

Browsers have already built and tested the behaviour.

Summary

  • details and summary make accordions with no code.
  • dialog gives accessible popups for free.
  • popover and progress add more built-in widgets.
  • Native elements are accessible by default.

Next lesson

Next: help search engines understand your content with structured data.

Questions

Can I animate details?

Yes, with CSS and the details-content styling in modern browsers, or a little JavaScript. The default is instant.

Is dialog supported everywhere?

Yes, in all current major browsers.

When do I still need JavaScript?

When logic is involved, such as loading data, filtering a list or saving choices.

Progress is saved in this browser.