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
Dialog markup: content, name and close control
Show, showModal and close have different jobs
Focus, Escape and the way back
Read the script and backdrop style
A popover can use HTML buttons without a script
<button type="button" popovertarget="tip">Show tip</button>
<p id="tip" popover="auto">Save the current file first.</p>A popover is not automatically a menu or modal
Review the entire opening and closing journey
Follow the idea
- 1Open from a named control
- 2Put focus in the right place
- 3Keep the intended interaction boundary
- 4Close and return focus appropriately
Modal dialogs and optional non-modal popovers serve different tasks.
Complete program: 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>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.
Complete program: 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>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.
Problem and solution
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
Which method opens a modal dialog? Does the open attribute alone create a modal? Why use autofocus on this dialog's close button? What connects the popover button to its panel? Does auto popover make the rest of the page inert? Save both programs and test opening, closing and focus with the keyboard. What should differ?
Check your practice answers
ShowModal(). No. It displays a non-modal dialog. Closing is the only immediate action the short reminder needs. Popovertarget matches the panel id. No. It is non-modal. 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?
Why is a visible close control important?
What does popover add by itself?
What should a dialog review include?
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.