Sub topic 10
Making dialogs accessible
This chapter assumes you can already open and close a dialog with showModal() and close(), and that you know basic form markup and ARIA attributes such as aria-labelledby. It covers what the browser does for accessibility and what you still have to supply.
What the browser gives you
A <dialog> has the implicit ARIA role dialog. When you open it with showModal(), the browser also does the following:
- Makes the rest of the page inert, so keyboard focus and assistive technology cannot reach it.
- Moves focus into the dialog.
- Closes the dialog on Escape.
- Returns focus to the element that opened it, once the dialog closes.
Building this by hand with a div means writing a focus trap, key handling, and aria-modal. The native element handles all of it.
The browser does not give the dialog a name, and it does not make sure the focus lands on a sensible control. Those are your job.
Give the dialog an accessible name
A screen reader announces something like "Edit profile, dialog" when focus enters. The "Edit profile" part is the accessible name. Without it, users hear only "dialog".
Point aria-labelledby at the visible heading. This keeps the label and the heading in sync.
<dialog id="dlg" aria-labelledby="dlg-title" aria-describedby="dlg-desc">
<h2 id="dlg-title">Delete project</h2>
<p id="dlg-desc">This permanently removes the project and its files.</p>
<form method="dialog">
<button value="cancel" autofocus>Cancel</button>
<button value="delete">Delete</button>
</form>
</dialog>Use aria-label="..." only when there is no visible heading to reference. aria-describedby is optional. It adds a longer explanation that is read after the name.
Choose the initial focus deliberately
When a modal opens, the browser focuses the first focusable element, or the one marked autofocus. In the example above, autofocus is on Cancel, so a stray Enter press cannot trigger a destructive action.
Use these rules to choose the starting point:
| Dialog content | Put `autofocus` on |
|---|---|
| A form | The first field |
| A destructive confirmation | The safe button (Cancel) |
| Long text the user must read | The dialog itself, using `tabindex="-1"` |
Avoid tabindex on the <dialog> in other cases. It adds a pointless tab stop.
Use alertdialog for urgent confirmations
If the dialog interrupts the user to demand a response, such as a delete confirmation, set role="alertdialog". Screen readers then announce it more assertively. Pair it with aria-describedby so the message is read as well.
<dialog id="confirm" role="alertdialog"
aria-labelledby="c-title" aria-describedby="c-desc">
<h2 id="c-title">Discard changes?</h2>
<p id="c-desc">Your edits will be lost.</p>
<form method="dialog">
<button value="keep" autofocus>Keep editing</button>
<button value="discard">Discard</button>
</form>
</dialog>Do not use alertdialog for ordinary forms. It signals urgency, so overusing it makes real alerts less noticeable.
Keep the keyboard paths open
Check these points whenever you build a dialog:
- Every action is reachable with Tab and Shift+Tab, and every button activates with Enter or Space.
- There is a visible way to close the dialog. Escape works for modals, but not everyone knows it, and touch users have no Escape key.
- Icon-only buttons have a name, such as
<button aria-label="Close">×</button>. - Focus returns to the opener after the dialog closes. This happens automatically, unless you removed the opener from the page or you moved focus yourself in the
closehandler.
A non-modal dialog opened with show() does not make the page inert, and Escape does not close it by default. Treat it more like a region of the page. Give it a visible close button and consider moving focus into it yourself.
Common accessibility mistakes
- Hiding the background yourself.
showModal()already makes the page inert. Addingaria-hidden="true"to page content is redundant, and it can leave content hidden if the dialog closes unexpectedly. - Removing the focus outline. Users who navigate by keyboard need to see where they are.
- Relying on color or placement alone. The dialog must make sense when read linearly by a screen reader.
- Opening the dialog with a click handler on a non-interactive element. Use a real
<button>so keyboard users can open it.
Exercise
A dialog asks "Delete 12 files?" with Cancel and Delete buttons. Which attributes and focus choices make it accessible?
Show answer
Give the dialog role="alertdialog". Point aria-labelledby at the heading ("Delete 12 files?") and aria-describedby at the explanatory text. Put autofocus on the Cancel button so an accidental Enter does not delete anything. Open it with showModal(), so the page is inert and focus returns to the opener on close.