X

How to Build an Accessible Modal Dialog with the HTML Dialog Element

A modal should keep keyboard and screen-reader users oriented while it is open, provide a clear way to close it, and return focus to the control that opened it. The native HTML <dialog> element handles much of that behavior for you when you open it with showModal().

This guide shows how to build a small modal dialog with a visible title, a useful initial focus target, a close button, Escape-key behavior, and focus restoration. Use a modal only when a person needs to deal with the dialog before returning to the page; brief, non-blocking information usually belongs inline.

Use showModal() for a real modal

The <dialog> element can be modal or non-modal. Calling showModal() places it in the browser's top layer and makes the rest of the same document inert while it is open. Calling show() leaves the page interactive. Adding the open attribute directly also creates a non-modal dialog, so it is not a substitute for showModal() when the interaction must block the page.

This distinction matters for confirmations, settings, and other tasks that need the user's attention. If people should be able to keep working elsewhere on the page, use a non-modal pattern instead.

Add a name, focus target, and visible close button

Connect the dialog to a visible heading with aria-labelledby. For a dialog with more than a short sentence, place initial focus on a static heading or introductory element so users can understand the content before reaching an action. Give that element tabindex="-1" so JavaScript can focus it without adding it to the normal Tab sequence.

<button id="open-help" type="button">Open help</button>

<dialog id="help-dialog" aria-labelledby="help-title">
  <h2 id="help-title" tabindex="-1">Before you continue</h2>
  <p>This action will save your current settings.</p>
  <form method="dialog">
    <button value="close">Close</button>
  </form>
</dialog>

The form's method="dialog" closes the dialog when the button is activated; it does not send the form data to a server. For a real form, use formmethod="dialog" on a cancel button when you want to close without submitting the form normally.

The heading gives the dialog an accessible name, and the button provides a visible close option for mouse, touch, and keyboard users. Do not put tabindex on the <dialog> element itself; focus its contents instead.

Open it and choose where focus starts

Load this script with defer or place it after the dialog markup so the elements exist when the selectors run:

const helpDialog = document.querySelector("#help-dialog");
const openHelp = document.querySelector("#open-help");
const helpTitle = document.querySelector("#help-title");

openHelp.addEventListener("click", () => {
  helpDialog.showModal();
  helpTitle.focus();
});

showModal() opens the modal and puts focus inside it. The example then moves focus to the heading because the dialog has explanatory text to read before the close action. For a short dialog with one obvious action, focusing that action may be a better starting point. The right focus target depends on what the dialog contains and what the person needs to do.

The browser keeps keyboard focus within a modal dialog while it is open. Tab and Shift+Tab move between its focusable controls; they do not let users interact with the inert page behind it. You do not need to add a custom focus trap to a native modal dialog.

Style the backdrop without hiding the dialog

The ::backdrop pseudo-element lets you distinguish the modal from the page behind it. Keep the dialog itself readable and its controls visible:

dialog {
  max-width: min(32rem, calc(100vw - 2rem));
  border: 0;
  border-radius: 0.5rem;
  padding: 1.5rem;
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.55);
}

The backdrop is not a close button. Keep the visible close control, and do not rely on clicking outside the dialog as the only way to dismiss it.

Close it and return focus

When a modal opened with showModal() is active, Escape closes it by default. The method="dialog" form in the example gives users another way to close it. Avoid cancelling the dialog's cancel event unless the interaction genuinely requires preventing dismissal; trapping someone in a modal is a serious usability problem.

After the dialog closes, focus normally returns to the element that opened it. If your application removes that element or advances to a new step, move focus to a sensible next location instead. For example, after a dialog adds a row to a table, focus might belong in that new row rather than back on the now-irrelevant trigger.

For a dialog with semantic content such as several paragraphs or a list, do not automatically add aria-describedby pointing at all of that content. Screen readers may announce it as one long description, making its structure harder to navigate. A short, simple sentence can be useful as a description; otherwise let people explore the content normally.

Use show() only when the dialog is intentionally non-modal. It does not make the rest of the page inert or provide the modal keyboard boundary. Also avoid opening a dialog with show() and then calling showModal() while it is still open; that state change can throw an InvalidStateError.

Test the interaction

Before shipping, check the dialog as a complete interaction:

  1. Open it using the keyboard and confirm focus moves to the intended heading or control.
  2. Use Tab and Shift+Tab to check that focus stays within the modal.
  3. Press Escape and confirm the dialog closes and focus returns to its opener.
  4. Reopen it and use the visible close button; confirm it works without relying on a keyboard.
  5. Check that the dialog has a useful name in a screen reader and that longer content remains navigable.

For a broader keyboard, screen-reader, and responsive testing routine, see our guide to testing website accessibility.

MDN lists <dialog> as widely available across browsers since March 2022, but check the browser versions your own site supports. Read the current MDN <dialog> documentation, MDN showModal() reference, and WAI-ARIA Authoring Practices dialog pattern when you need more detail about behavior and focus choices.

Categories: Development
Related Post