Skip to content

<dialog> - Modal Dialog Element

Interactive HTML5

The <dialog> element represents a dialog box or interactive component such as a dismissible alert, inspector, or subwindow. It provides native browser support for modals and popups with built-in accessibility features, keyboard handling, and focus management.

Result
<dialog id="myDialog">
<!-- Dialog content -->
</dialog>
AttributeValueDescription
openBooleanWhen present, the dialog is visible (use JavaScript methods instead)

The <dialog> element supports all global attributes.

Understanding the difference between modal and non-modal dialogs is crucial:

Result
Result

Use method="dialog" to close dialogs with form submission:

Result

The ::backdrop pseudo-element styles the background overlay:

Result
Result
Result
Result

Modal dialogs automatically trap focus and restore it on close:

Result

Modal dialogs close when the user presses Escape:

Result
Result

Add smooth animations to dialog appearance:

Result

The <dialog> element includes excellent built-in accessibility:

Result
  1. Use semantic HTML: Include proper headings and structure
  2. Label dialogs: Use aria-labelledby or aria-label
  3. Describe content: Use aria-describedby for additional context
  4. Manage focus: Let browser handle focus trap in modals
  5. Keyboard support: Ensure all actions are keyboard accessible
  6. Clear close options: Provide obvious ways to close
  7. Test with screen readers: Verify announcements are meaningful

Excellent Modern Support

The <dialog> element is now supported in all modern browsers:

  • Chrome: 37+ (2014)
  • Firefox: 98+ (2022)
  • Safari: 15.4+ (2022)
  • Edge: 79+ (2020)
  • Opera: 24+ (2014)
  • Mobile: Varies by browser version

Polyfill Available: For older browsers, use the dialog-polyfill library.

  • <details> - Disclosure widgets
  • <summary> - Details summary
  • Popover API - Alternative for non-modal overlays
  1. Use showModal() for modals: Don’t use the open attribute
  2. Always provide close mechanism: Button, form, or backdrop click
  3. Use form method=“dialog”: Simplifies form-based dialogs
  4. Style the backdrop: Make it clear the page is blocked
  5. Handle focus properly: Let browser manage for modals
  6. Test keyboard navigation: Tab, Escape, Enter should work
  7. Provide clear labels: Use descriptive headings
  8. Don’t nest dialogs: Avoid opening dialogs from dialogs
  9. Test on mobile: Ensure dialogs work on small screens
  10. Consider animations: Smooth transitions improve UX

Learn More: