Dialog Modal Native Show Modal

Create a native modal dialog using the dialog element with showModal and form method close for accessible interactions.

HtmlForm Validation

Code

htmlsnippet
13 lines
<dialog id="confirm-dialog">
  <form method="dialog">
    <p>Are you sure you want to delete this item?</p>
    <menu>
      <button value="cancel">Cancel</button>
      <button value="confirm" autofocus>Delete</button>
    </menu>
  </form>
</dialog>

<button onclick="document.getElementById('confirm-dialog').showModal()">
  Delete item
</button>

How the snippet works

The markup uses the semantic elements dialog instead of generic containers, giving assistive technology and search engines real structure without extra wiring.

<dialog> gives native modal semantics — focus trapping and Escape handling come for free.

Script and styles anchor onto ids confirm-dialog, so behavior and styling stay decoupled from the tag names.

Statements that matter

<dialog id="confirm-dialog">
<form method="dialog">
<p>Are you sure you want to delete this item?</p>

Interactive Tools for This Topic

FAQ

Why semantic elements instead of divs?
`<dialog>` expose document structure to assistive technology and search engines without ARIA or class-name conventions.