Skip to content
Web Design & Development

How to Get The HTML dialog Element Right

A native HTML element for modal and non-modal dialogs, with built-in focus handling, a backdrop and closing with the Escape key.

MediaScaleUp Editorial Team 2 min read 13 views
How to Get The HTML dialog Element Right

At a glance

  • Modal opened with showModal(), which makes the rest of the page inert
  • Non-modal opened with show()
  • Escape key closes a modal dialog by default
  • Backdrop styled with the ::backdrop pseudo-element

Why it matters

Why it matters: Custom modal scripts frequently break keyboard and screen reader access, while the dialog element provides much of the correct behavior by default.

Best practice

  • Use dialog instead of custom modal code
  • Give each dialog an accessible name
  • Return focus to the triggering control when closed
  • Keep dialog content short and focused
  • Test with keyboard and screen readers
What to do and what to avoid with the HTML dialog element, side by side
Good practice against the usual mistakes, from the sources listed below.

Common pitfalls

Watch out for:

  • Custom modals that do not trap or restore focus
  • Nested modal dialogs
  • Dialogs that cannot be closed with the keyboard
  • Using dialogs for content that belongs on the page

When to call in a specialist

Bottom line Bring in help when replacing legacy modal libraries, or when dialogs fail accessibility testing.

Where this comes from

The figures and practices above come from the sources listed.

Working on something like this?

We take on Web Design & Development work for teams who want it done once, properly. Tell us what you are building and we will tell you honestly whether we are the right studio for it. Start a project.

Where to go next

Spotted something wrong? Report an error on this page. We correct on the page and say what changed.

All services

The work behind this article, and what it costs.

MediaScaleUp Editorial Team

Articles here are researched, written and checked by the MediaScaleUp studio team before they are published, and dated when they are reviewed.

Keep reading

More in Web Design & Development