Lesson 43 of 55
HTMLThe `<dialog>` Element – Native Modal Dialogs
9 min
Course progress78%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 43 of 55
HTML9 min
Keep these ideas in mind as you work through the lesson.
<dialog> Element – Native Modal DialogsHTML5 introduced <dialog> for creating accessible modal windows without JavaScript polyfills.
<dialog id='myDialog'>
<h2>Subscribe</h2>
<form method='dialog'>
<label for='email'>Email:</label>
<input type='email' id='email' required />
<menu>
<button type='submit'>Send</button>
<button type='reset'>Cancel</button>
</menu>
</form>
</dialog>
<button id='openBtn'>Open Dialog</button>
<script>
const dialog = document.getElementById('myDialog');
document.getElementById('openBtn').addEventListener('click', () => dialog.showModal());
dialog.addEventListener('close', () => console.log('Dialog closed with value:', dialog.returnValue));
</script>
aria-modal – Implicit when using showModal(); screen readers recognise the modal state.Esc automatically closes the dialog.returnValue – Allows the form to pass back a value when the dialog is closed.If you need to support browsers that lack <dialog>, include a small polyfill (e.g., dialog-polyfill.js). The polyfill mimics the API and provides similar accessibility features.
<dialog>showModal() (modal) or show() (modeless)? ✅<h2> or aria-label)? ✅Tip: Use the
openattribute (<dialog open>) during development to see the dialog without JavaScript.