Native <dialog> Modals & Popover API
Build accessible modals, backdrops, and lightweight popovers natively in HTML, with automatic top-layer rendering, Escape dismissal, and focus return.
What you'll learn
- Open and close accessible modal dialogs with <dialog>, showModal(), and <form method="dialog">
- Style the modal overlay with ::backdrop and prevent background scroll chaining
- Create zero-JS menus and tooltips with the native popover attribute
For years, building an accessible modal required heavy JavaScript libraries to trap focus, dim the background, and handle Esc. Today, HTML’s native <dialog> and popover handle it automatically in the browser’s top layer!
Modal <dialog> vs. popover
- Use
<dialog>withshowModal()when the overlay blocks the rest of the page (confirmations, forms, search palettes). The browser automatically makes the background inert, handlesEsc, renders::backdrop, and returns focus to the button when closed. - Use
popover+popovertargetfor non-blocking overlays (dropdown menus, quick info cards, toasts). Clicking outside automatically light-dismisses it, with zero JavaScript!
Fill-in-the-Blank Exercise
Native Popover & Modal Form
Connect the button to the popover without JavaScript, and make the dialog form close the modal natively.
<button ="helpTip">Quick Tip</button>
<div id="helpTip" >Popovers light-dismiss when you click outside!</div>
<dialog id="confirm">
<form method="">
<button value="ok">Close</button>
</form>
</dialog>Try It Yourself
Test both a native Modal <dialog> (press Esc to close!) and a zero-JS popover below:
No console output yet. Try calling console.log(...) in JS.
Check Your Understanding
1. Which JavaScript method opens a <dialog> as a true blocking modal with a ::backdrop and automatic Escape handling?
Finished this lesson?
Your progress is saved locally in your browser, no account needed.