<dialogid="confirmDialog"><h2>Confirm Deletion</h2><p>This will permanently remove the item. Continue?</p><formmethod="dialog"><buttonvalue="cancel">Cancel</button><buttonvalue="confirm">Delete</button></form></dialog><!-- Open with: document.getElementById('confirmDialog').showModal() -->
Note Use showModal() to open with a backdrop that traps focus (accessible). Use show() for a non-modal popup. The form method="dialog" approach closes the dialog and sets its returnValue to the button's value.
Note The popover attribute creates a top-layer element that dismisses on outside click or Escape key, with no JavaScript required. It handles focus trapping and accessibility automatically. Style the ::backdrop pseudo-element for the overlay behind it.
Frequently asked questions
How does HTML & CSS handle popup?
HTML & CSS covers this with 2 copy-ready snippets on this page. The "<dialog> Modal" snippet in HTML & CSS uses `<dialog id="myDialog">`.
Which code does the HTML & CSS example use?
The "<dialog> Modal" snippet uses `<dialog id="myDialog">`, from the Semantic Elements section of the HTML & CSS cheat sheet.
What other HTML & CSS snippets are shown for "popup"?
Besides "<dialog> Modal", this page also shows "Popover API (HTML + CSS)".
Is there anything to watch out for?
Yes. For "<dialog> Modal": Use showModal() to open with a backdrop that traps focus (accessible). Use show() for a non-modal popup. The form method="dialog" approach closes the dialog and sets its returnValue to the button's value.