CHAPTER 52 · HTML

Dialogs and popovers: show extra information with a clear way back

Build a native modal dialog and an HTML-only popover. Explain opening, closing, focus and why essential information should stay easy to reach.

Download example files (ZIP)

An extra panel should have a clear job

A college help desk might pause a conversation for a short confirmation, or simply offer an optional tip beside the main discussion. These are different interactions. A dialog is a separate interaction area. A modal dialog asks the person to deal with it before continuing with the rest of that document.

A popover is a temporary area shown above the page content. It is non-modal: the rest of the page remains usable. Do not use an overlay merely to decorate a simple sentence. Keep essential instructions available without guessing which popup contains them.

Dialog markup: content, name and close control

Dialog needs opening and closing tags. Place its heading, explanation and controls inside it. Our aria-labelledby points to its visible heading, giving it a clear name. The close control is an ordinary type=button.

Without open or a method that opens it, the dialog begins closed. Adding open displays a non-modal dialog; it does not create modal behaviour by itself. Do not write open="false" to mean closed. Presence still means open.

Show, showModal and close have different jobs

Show() opens a non-modal dialog. ShowModal() opens it modally in the browser's top layer, above ordinary content, and makes the rest of that document inert while it is open. Inert means it cannot normally be focused or interacted with. The modal boundary is within its document, not a permission to block unrelated browser tabs.

Close() closes the dialog and can supply a returnValue string. Our close button passes read. A form with method=dialog can also close an enclosing dialog and provide a return value without making an ordinary network submission. That method is not the same as GET or POST. We use explicit buttons and a small script to keep this example focused.

Focus, Escape and the way back

Opening a native modal moves focus into it. We put autofocus on its close button because this short reminder needs no other immediate input. Do not add tabindex to the dialog element itself. Test that Tab stays within the modal interaction and that closing returns focus appropriately, normally to its opener.

Escape normally dismisses this simple modal through its cancel behaviour. Do not suppress that route casually. A visible close control is still needed for people without that key. Clicking a backdrop is not a guaranteed close action in this example. Different closing policies and browser versions need deliberate testing.

Read the script and backdrop style

The opening button calls showModal. The closing button calls close. A close event updates the visible state sentence after the dialog closes, whether the ordinary close control or Escape was used. Aria-live=polite requests an appropriate announcement of that changed sentence.

The CSS ::backdrop selector styles the background layer behind a modal. Rgba(0,0,0,0.25) is black with one-quarter opacity, creating a light dimming effect. A backdrop colour alone does not create focus handling or modality. In the tutorial preview, scripts do not run; test the saved file for the complete action.

A popover can use HTML buttons without a script

Put popover=auto on an element with an id. Add popovertarget with that same id to a native button. Its normal action toggles the popover. An inner button can use popovertargetaction=hide for an explicit close action.

HTML fragment — part of a document
<button type="button" popovertarget="tip">Show tip</button>
<p id="tip" popover="auto">Save the current file first.</p>

In supporting browsers, auto popovers can also be light-dismissed by clicking outside or pressing Escape. Opening another auto popover generally closes an existing one, with exceptions such as nested popovers. Manual popovers need explicit closing controls and do not use the same light-dismiss rules.

A popover is not automatically a menu or modal

Popover changes visibility and top-layer presentation. It does not automatically implement an application menu, a modal focus trap or every keyboard rule for a complex widget. The declarative button connection gives useful native relationships, but content still needs appropriate meaning.

A popover is also not a new browser window. Browsers have different support for newer popover features, so test the intended devices. Keep essential information outside the optional panel so an unsupported enhancement does not remove the task itself.

Review the entire opening and closing journey

Start with the panel closed. Open it using the mouse and keyboard. Inspect initial focus, content and available controls. Close through its visible control and Escape where supported, then check where focus went. Repeat the action to catch one-use bugs.

Use native controls for the relevant job before writing a custom overlay. Add only the extra behaviour the product actually needs. These teaching examples are local interactions, not account changes, confirmations of purchase or real submissions.

Follow the idea

A clear way in and out
  1. 1Open from a named control
  2. 2Put focus in the right place
  3. 3Keep the intended interaction boundary
  4. 4Close and return focus appropriately

Modal dialogs and optional non-modal popovers serve different tasks.

Complete program: practice-dialog.html

Save this as practice-dialog.html. Open it in your browser.

practice-dialog.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>A Native Practice Dialog</title>

    <style>
      dialog {
        max-width: 24rem;
        padding: 1rem;
        border: 2px solid #2458a6;
      }
      dialog::backdrop {
        background: rgba(0, 0, 0, 0.25);
      }
    </style>
  </head>
  <body>
    <h1>Open a short practice reminder</h1>
    <main>
      <p>The useful reminder is also available here: save the page before refreshing it.</p>
      <button type="button" id="open-reminder">Open reminder dialog</button>
      <p id="dialog-state" aria-live="polite">The dialog has not been opened.</p>
      <dialog id="reminder-dialog" aria-labelledby="reminder-title">
        <h2 id="reminder-title">Before you refresh</h2>
        <p>Save the current HTML file so the browser can show your latest change.</p>
        <button type="button" id="close-reminder" autofocus>Close reminder</button>
      </dialog>
      <script>
        const dialog = document.getElementById('reminder-dialog');
        const opener = document.getElementById('open-reminder');
        const state = document.getElementById('dialog-state');
        opener.addEventListener('click', function () {
          dialog.showModal();
          state.textContent = 'The reminder dialog is open.';
        });
        document.getElementById('close-reminder').addEventListener('click', function () {
          dialog.close('read');
        });
        dialog.addEventListener('close', function () {
          state.textContent = 'The reminder dialog is closed.';
        });
      </script>
    </main>
  </body>
</html>
Browser previewDocument title: A Native Practice Dialog

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. JavaScript does not run in this preview. Save the complete example and any named script file together to test its actions or canvas drawing in your browser.

Expected result in a saved file: Open reminder dialog opens a named modal and focuses Close reminder. The rest of this document is inert until it closes. The close button and Escape close it, and the state sentence updates. The tutorial preview keeps scripts disabled.

Complete program: practice-popover.html

Save this as practice-popover.html. Open it in your browser.

practice-popover.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>An HTML-Only Practice Popover</title>

    <style>
      [popover] {
        max-width: 22rem;
        padding: 1rem;
        border: 2px solid #2458a6;
      }
    </style>
  </head>
  <body>
    <h1>Offer an optional file hint</h1>
    <main>
      <p>Essential instruction: save your work before opening the result.</p>
      <button type="button" popovertarget="file-hint">Show file hint</button>
      <div id="file-hint" popover="auto">
        <p>A useful filename describes its page, such as session-notes.html.</p>
        <button type="button" popovertarget="file-hint" popovertargetaction="hide">
          Close file hint
        </button>
      </div>
      <p>The main page remains available while this optional hint is open.</p>
    </main>
  </body>
</html>
Browser previewDocument title: An HTML-Only Practice Popover

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result in a supporting browser: Show file hint toggles the optional popover. Close file hint, Escape or an outside click dismisses this auto popover. It uses no authored JavaScript and does not make the underlying page modal.

Problem and solution

Problem: adding the open attribute displayed a dialog, but the page behind it was still interactive.

Solution: open is a visible non-modal state. Use the native modal opening mechanism when the task truly requires modality, then test focus and closing. Do not simulate modality only with a dark background.

Common mistakes and useful tips

  • Open alone does not mean modal.
  • Give dialogs useful names and visible close controls.
  • Do not add tabindex to dialog.
  • Do not assume backdrop clicks close this example.
  • A popover does not automatically become an application menu.
  • Test support and keep essential content available.

Practice questions

  1. Which method opens a modal dialog?
  2. Does the open attribute alone create a modal?
  3. Why use autofocus on this dialog's close button?
  4. What connects the popover button to its panel?
  5. Does auto popover make the rest of the page inert?
  6. Save both programs and test opening, closing and focus with the keyboard. What should differ?
Check your practice answers
  1. ShowModal().
  2. No. It displays a non-modal dialog.
  3. Closing is the only immediate action the short reminder needs.
  4. Popovertarget matches the panel id.
  5. No. It is non-modal.
  6. The modal limits interaction to its dialog until closed. The popover provides optional content while the main page remains available. Both need working close routes.

Interview questions

How do show and showModal differ?

Show opens non-modally. ShowModal opens in the modal top layer and makes the rest of that document inert while it remains open.

Why is a visible close control important?

Not everyone uses Escape or a pointing device. A clear control provides a discoverable way back and should work with native keyboard activation.

What does popover add by itself?

Temporary top-layer visibility and its associated native showing/hiding behaviour. It does not supply every semantic or interaction rule for a complex widget.

What should a dialog review include?

Both opening and closing, initial focus, keyboard containment for a modal, Escape behaviour, accessible naming and focus after dismissal.

Quick revision

  • Choose an overlay only for a real task.
  • Modal and non-modal interactions differ.
  • Name the panel and provide a way back.
  • Test focus, closing and browser support.