Web development
Open a native modal dialog
Use dialog.showModal for a native modal dialog. Give it a meaningful heading and an obvious close action, and test focus movement as part of the interaction.
Worked example
HTML example · save as example.html and open in a browser
<button id="open" type="button">Read notice</button>
<dialog id="notice" aria-labelledby="notice-title">
<h2 id="notice-title">Before you continue</h2>
<p>Your local files remain on this device in this example.</p>
<form method="dialog"><button>Close</button></form>
</dialog>
<script>document.querySelector('#open').onclick = () => document.querySelector('#notice').showModal();</script>How it works
ShowModal places the dialog in the top layer and makes the surrounding page inert while it is open. A form with method dialog closes the dialog through its native mechanism. The heading supplies the dialog’s accessible name.
Limits and mistakes
A modal interrupts the task, so use it only when that interruption is justified. Opening a dialog is different from merely adding its open attribute. Test scrolling, long content, and initial focus rather than assuming the native element resolves every usability issue.
Check the result
Open the notice using the keyboard, close it with the button, and try Escape. Confirm focus returns to the opener and background controls cannot be activated while the modal is open.
Sources
Reference documentation for the behavior used in this guide: