How can I give a website dialog the right HTML semantics?
Quick answer
Use the native <dialog> element when it fits: call showModal() for a modal dialog or show() for a nonmodal one. Give the dialog an accessible name, usually by connecting it to a visible heading with aria-labelledby. For a modal, ensure outside content cannot be used, place keyboard focus appropriately, provide a visible close button, and return focus sensibly when it closes. A custom widget needs to recreate these behaviors.
The question, the details, the next step.
A practical checklist
Choose modal or nonmodal behavior before choosing how to display the panel.
Prefer
<dialog>when its native behavior suits the task; useshowModal()for modal andshow()for nonmodal display.Give the dialog a concise accessible name, preferably by referencing its visible heading with
aria-labelledby.For a modal, place focus appropriately, prevent outside interaction, and keep keyboard focus inside until it closes.
Provide a visible close button and choose a sensible focus destination after closing.
Use
aria-modal="true"only when the dialog genuinely blocks and visually obscures the background.
A few more useful answers
Does adding `role="dialog"` to a `<div>` make it behave like a modal?
No. A role communicates semantics; it does not by itself implement modal behavior. A custom modal also needs to prevent outside interaction, manage focus, and provide a way to close.
Should every dialog use `aria-describedby`?
No. It is optional and is most appropriate for simple descriptive text. Avoid using it to flatten complex, navigable content into one announcement.
Can a nonmodal dialog still have a close button?
Yes. A visible close control is useful, but a nonmodal dialog leaves the rest of the page interactive; do not claim that it is modal.
Sources
References used for this guide.
- [S1] MDN: HTML <dialog> element developer.mozilla.org · Checked
- [S2] W3C WAI-ARIA APG: Dialog (Modal) Pattern w3.org · Checked
