Reading history

HTML details, dialog and popover: Interactive Elements

6 min read 2 views

Build accordions with details and summary, show popovers with no JavaScript, and open modal dialogs with dialog and showModal, with live examples.

Accordions, tooltips and confirmation boxes used to need a JavaScript library. Modern HTML has three elements that do most of this work for you. <details> makes a section that opens and closes. The popover attribute shows a small panel on top of the page. <dialog> shows a modal box that blocks the rest of the page until the user answers. All three handle the keyboard and screen readers for you, which is hard to get right by hand.

In this lesson you will learn

  • Building collapsible sections with <details> and <summary>
  • Making an exclusive accordion with the name attribute
  • Styling the open state and the marker
  • Showing a popover with no JavaScript
  • Opening a modal <dialog> and reading the user’s choice

You should know HTML elements and attributes and HTML forms.

Collapsible Sections with details

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>details and summary</title>
  <style>
    body { font-family: sans-serif; margin: 12px; max-width: 520px; }
    details { border: 1px solid #cfd8dc; border-radius: 6px; margin-bottom: 8px; }
    summary { padding: 8px 12px; cursor: pointer; font-weight: bold; background: #eceff1; }
    details[open] summary { background: #1565c0; color: white; }
    details p { margin: 0; padding: 8px 12px; }
    .plain summary { list-style: none; }            /* hide the default triangle */
    .plain summary::-webkit-details-marker { display: none; }
    .plain summary::after { content: " +"; }
    .plain[open] summary::after { content: " \2212"; }
  </style>
</head>
<body>
  <details open>
    <summary>What is included in the course?</summary>
    <p>Twenty lessons, practice exercises and a final project.</p>
  </details>

  <!-- Same name: opening one closes the others (an exclusive accordion) -->
  <details name="faq" class="plain">
    <summary>Is there a certificate?</summary>
    <p>Yes, after you finish the final project.</p>
  </details>
  <details name="faq" class="plain">
    <summary>Do I need to install anything?</summary>
    <p>Only a text editor and a web browser.</p>
  </details>
</body>
</html>

Result

  • <summary> is the clickable heading, and everything else inside <details> is hidden until the user opens it. It works with a mouse, with Enter or Space on the keyboard, and with screen readers.
  • The open attribute makes a section start open. The browser adds or removes this attribute as the user clicks, so details[open] in CSS styles the open state.
  • Several <details> elements with the same name form an exclusive accordion: opening one closes the others. Try it with the two questions marked “+”. Older browsers ignore name and simply let all sections open at once, which is still usable.
  • list-style: none on the summary removes the default triangle in most browsers; Safari also needs the ::-webkit-details-marker rule. A ::after pseudo-element then draws your own “+” and “−” signs.
  • Text inside a closed section is still part of the page, and some browsers, such as Chrome, open it automatically when the user searches the page for a word inside it.

Popovers and Dialogs

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>popover and dialog</title>
  <style>
    body { font-family: sans-serif; margin: 12px; }
    button { font-size: 14px; padding: 6px 12px; margin-right: 6px; }
    [popover] { border: 1px solid #90a4ae; border-radius: 8px; padding: 10px 14px; max-width: 240px; }
    dialog { border: 0; border-radius: 10px; padding: 18px; box-shadow: 0 10px 30px rgba(0,0,0,.3); }
    dialog::backdrop { background: rgba(0, 0, 0, .45); }
    dialog h2 { margin-top: 0; font-size: 18px; }
  </style>
</head>
<body>
  <!-- 1. Popover: no JavaScript needed -->
  <button popovertarget="tip">Show tip</button>
  <div id="tip" popover>Press Esc or click outside to close this popover.</div>

  <!-- 2. Modal dialog: one line of JavaScript opens it -->
  <button id="open">Delete file...</button>
  <dialog id="confirm">
    <h2>Delete notes.txt?</h2>
    <p>This cannot be undone.</p>
    <form method="dialog">
      <button value="cancel">Cancel</button>
      <button value="delete">Delete</button>
    </form>
  </dialog>
  <p id="result">No choice yet.</p>

  <script>
    const dlg = document.getElementById("confirm");
    document.getElementById("open").addEventListener("click", () => dlg.showModal());
    dlg.addEventListener("close", () => {
      document.getElementById("result").textContent = "You chose: " + dlg.returnValue;
    });
  </script>
</body>
</html>

Result

  • An element with the popover attribute is hidden by default. A button with popovertarget="tip" toggles it, with no script. The popover appears in the top layer, above everything else, so no z-index is needed.
  • Pressing Esc or clicking outside closes a popover. This “light dismiss” behaviour suits tips, menus and small notices.
  • A <dialog> opened with showModal() is modal: the rest of the page cannot be clicked, keyboard focus moves inside the dialog, and Esc closes it. The ::backdrop pseudo-element styles the dark layer behind it.
  • A form with method="dialog" closes the dialog instead of sending data to a server. The value of the button that was pressed becomes the dialog’s returnValue, which the close event reads.
  • Showing a dialog without script is possible with the open attribute, but that makes a non-modal dialog that does not block the page. Use showModal() for confirmations.

Which Element Should You Use?

NeedUseJavaScript?
FAQ, “show more”, accordion<details> + <summary>No
Tooltip-style help, small menupopover + popovertargetNo
“Are you sure?” confirmation<dialog> + showModal()One line to open
A box that stays open beside the content<dialog open>No

Common Mistakes

  1. Building accordions from <div> elements with click handlers, which are not reachable by keyboard unless you add a lot of extra code.
  2. Putting the summary text outside <summary>; it must be the first child of <details>.
  3. Using open when you need a modal dialog. Only showModal() blocks the page and traps focus.
  4. Hiding important information such as prices or warnings inside a closed section.

Practice Exercises

Exercise 1. Make a “Show answer” section for a quiz question, with the answer hidden at first.

Show answer
<p>What does HTML stand for?</p>
<details>
  <summary>Show answer</summary>
  <p>HyperText Markup Language</p>
</details>

Every exercise answer on this site is built this way.

Exercise 2. Give the popover a light yellow background and place a pointer-shaped cursor on its button.

Show answer
[popover] { background: #fff8e1; }
button[popovertarget] { cursor: pointer; }

Frequently Asked Questions

Do details, popover and dialog work in all browsers?

Yes, in all current versions of Chrome, Edge, Firefox and Safari. The name attribute for exclusive accordions is newer; browsers that lack it still open and close each section normally.

How do I animate a details section opening?

The content appears instantly by default. You can fade it in with a CSS animation on the content inside details[open]. Smoothly animating the height needs newer CSS features, so many sites keep the simple instant version.

Summary

  • <details> and <summary> make accessible collapsible sections.
  • The same name on several sections makes an exclusive accordion.
  • popover with popovertarget shows a dismissible panel without JavaScript.
  • <dialog> with showModal() makes a true modal; method="dialog" forms close it.

Next: CSS display and visibility.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *