Native dialog Element
Also Known As
dialog element
showModal
HTML modal
native modal
TL;DR
The HTML dialog element provides accessible modals and non-modal dialogs natively — with built-in focus trapping, backdrop rendering, and the Escape key — without JavaScript libraries.
Explanation
The dialog element renders a modal (when opened with showModal()) or non-modal dialog (show()). Features: automatic focus trapping (Tab cycles within), backdrop pseudo-element (::backdrop for overlay styling), Escape key closes modal automatically, returnValue for form results, and correct ARIA semantics (role=dialog, aria-modal). Close with dialog.close() or a form method=dialog button. The dialog element replaced dozens of JS modal libraries — it handles all the accessibility requirements that were previously hand-coded.
Common Misconception
✗ Native dialog is less flexible than a custom modal — native dialog handles all accessibility requirements out of the box; custom modals require extensive ARIA attributes, focus management, and keyboard handling that dialog provides automatically.
Why It Matters
Custom modal implementations that miss focus trapping or Escape key handling fail accessibility requirements — the native dialog element handles these correctly without any extra code.
Common Mistakes
- Using show() when a modal (focus-trapped) is needed — use showModal() for modal dialogs.
- Not styling ::backdrop — the default backdrop is transparent; add a semi-transparent background.
- Placing dialog outside the modal stack — always append dialog to document.body.
- Not calling dialog.close() on confirm/cancel buttons — dialog stays open without explicit close.
Code Examples
✗ Vulnerable
<!-- Custom modal — manual accessibility:
<div id="modal" role="dialog" aria-modal="true" style="display:none">
<!-- Manual focus trap needed -->
<!-- Manual Escape key handler needed -->
<!-- Manual ARIA management needed -->
<!-- 50 lines of JS for what dialog does natively -->
</div>
✓ Fixed
<!-- Native dialog — automatic accessibility:
<dialog id="confirm">
<h2>Confirm deletion?</h2>
<p>This cannot be undone.</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="confirm">Delete</button>
</form>
</dialog>
<button onclick="document.getElementById('confirm').showModal()">
Delete
</button>
<script>
document.getElementById('confirm').addEventListener('close', e => {
if (e.target.returnValue === 'confirm') deleteItem();
});
</script>
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
16 Mar 2026
Edited
22 Mar 2026
Views
23
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 7
Perplexity 5
Ahrefs 2
Unknown AI 2
Majestic 1
Google 1
Also referenced
How they use it
crawler 18
Related categories
⚡
DEV INTEL
Tools & Severity
🟡 Medium
⚙ Fix effort: Low
⚡ Quick Fix
Use the native <dialog> element instead of DIV-based modal implementations — it handles focus trapping, Escape key, and screen reader announcement automatically
📦 Applies To
html HTML5
web
🔗 Prerequisites
🔍 Detection Hints
Custom modal built with div role=dialog without native dialog element; missing focus trap; Escape key not handled
Auto-detectable:
✓ Yes
axe
lighthouse
⚠ Related Problems
🤖 AI Agent
Confidence: Low
False Positives: Medium
✗ Manual fix
Fix: Low
Context: File