Accessible markup for modals, tabs, accordions, tooltips, skip links and live regions, with the keyboard behaviour you still have to write listed.
Modal dialog (native) — using the native element
The native <dialog> with showModal() gives you the focus trap, Escape handling, the backdrop and inert background content for free. A hand-built modal has to reimplement every one of those, and the focus trap in particular is very easy to get subtly wrong.
A modal dialog using the native element, which handles focus, Escape and the inert background for you.
<button type="button" id="example-open">Open the dialog</button>
<dialog id="example-dialog" aria-labelledby="example-title">
<h2 id="example-title">Confirm the change</h2>
<p>This replaces the saved address on the account.</p>
<form method="dialog">
<button type="submit" value="cancel">Cancel</button>
<button type="submit" value="confirm">Confirm</button>
</form>
</dialog>#example-dialog::backdrop {
background: rgb(0 0 0 / 0.5);
}
#example-dialog {
max-width: min(32rem, calc(100vw - 2rem));
/* So the dialog itself scrolls rather than the page behind it. */
max-height: calc(100vh - 4rem);
overflow: auto;
}const dialog = document.getElementById("example-dialog");
const opener = document.getElementById("example-open");
opener.addEventListener("click", () => {
// showModal, not show. show() gives a non-modal dialog: the page behind
// stays reachable by Tab, which contradicts what a modal looks like.
dialog.showModal();
});
dialog.addEventListener("close", () => {
// Returning focus is the one thing the native element does not do.
opener.focus();
if (dialog.returnValue === "confirm") {
// the confirmed action
}
});The native <dialog> with showModal() gives you the focus trap, Escape handling, the backdrop and inert background content for free. A hand-built modal has to reimplement every one of those, and the focus trap in particular is very easy to get subtly wrong.
Keyboard behaviour this must have
Still to check by hand
Check 1: That focus returns to the button that opened it. The native element does not restore focus, and it is the most commonly missed part.
Check 2: That the dialog has an accessible name. Without aria-labelledby or aria-label it is announced as just "dialog".
Check 3: With a screen reader: whether the heading is announced on open. Behaviour differs between browsers and screen readers.
WCAG criteria this addresses
2.1.2: No Keyboard Trap (level A) — Focus can always be moved away from a component with a keyboard.
2.4.3: Focus Order (level A) — The tab order follows an order that preserves meaning.
4.1.2: Name, Role, Value (level A) — Every interface component has a name and a role, and its state can be read and set.
Addressing a criterion is not the same as meeting it. The markup removes the structural failure; whether the result is usable depends on the content you put in it.
Generated markup is a correct starting point, not a tested component. Whether the accessible name makes sense in context, whether the focus ring is visible against your own background, and whether a screen reader announces the result sensibly cannot be generated — test it with a keyboard and with at least one screen reader before shipping it.
Accessible Pattern Generator runs entirely on your device — the markup is generated in your browser and nothing is sent.
You’re on the Free plan. Compare plans
Every pattern leads with the native element and says what you lose by not using it. A dialog element gives you focus trapping, inertness of the page behind, Escape to close and focus restoration — all four for free, and all four are things a hand-built modal gets wrong. The ARIA version is generated only when you ask for it, with a note saying what you have taken on.
The keyboard behaviour is listed separately from the markup, because that is the part that cannot be generated. A tab list is one tab stop with arrow-key navigation inside it; markup alone does not provide that, and a pattern handing over the HTML without saying so would be handing over a broken widget.
What still needs checking by hand is part of the output. Whether the accessible name makes sense in context, whether the focus ring is visible against your own background, whether a screen reader announces the result sensibly — none of those can be generated, and a pattern implying otherwise would be worse than no pattern. Visually hidden is also not display:none, which hides content from screen readers too and defeats the purpose of a skip link entirely.
Because the native element already has the behaviour. A dialog element brings focus trapping, page inertness, Escape to close and focus restoration — four things a hand-built modal has to implement and routinely gets wrong, usually the focus restoration.
Because markup cannot provide it. A tab list is a single tab stop with arrow keys choosing within it, and no amount of ARIA makes that happen — it is JavaScript you write. Handing over the HTML without saying so would hand over a broken widget.
Because display:none hides content from screen readers too. A skip link hidden that way is invisible to everybody, including the keyboard user it exists for — the pattern clips it out of view while leaving it focusable and announced.
It is a correct starting point, not a tested component. Whether the accessible name makes sense, whether the focus ring is visible against your background, and whether a screen reader announces the result sensibly all depend on your page. Test it with a keyboard and at least one screen reader.
No. The markup removes the structural failure. Whether the result is usable depends on the content you put in it — a correctly marked-up dialog with a meaningless heading still fails.
If a tool here saved you some time, you can put something towards the hosting.Buy me a coffee, on Ko-fi, opens in a new tab