Sub topic 10
Focus, keyboard use, and accessibility
This chapter assumes you can already create a popover with the popover attribute and wire it to a button with popovertarget. It also assumes you know the difference between auto and manual popovers and what light dismiss means. You should also know three accessibility basics: the tab order, the accessible name of a control, and ARIA roles.
A popover is a non-modal overlay. That one fact explains most of its focus and keyboard behavior. The browser does a useful amount of work for you, but it does not turn a div into a menu, a tooltip, or a dialog. You still choose the semantics.
What the browser gives you for free
Using popovertarget on a button gives you the following without extra code:
- The button is keyboard operable with Enter and Space, because it is a real button.
- The browser links the button to the popover. Assistive technology can expose the button's expanded or collapsed state, so you should not add
aria-expandedyourself. A hand-maintained copy tends to drift out of sync with the real state. - A closed popover is
display: nonethrough the browser's default styles. Its content is out of the tab order and out of the accessibility tree until it opens. - Pressing Escape closes an
autopopover, and closes the topmost one if several are open. - When a popover closes while focus is inside it, the browser moves focus back to the element that was focused before it opened. This is normally the invoker.
Some things are not provided. The popover gets no role, no accessible name, no focus trap, and no inert page behind it.
Focus does not move when a popover opens
A modal dialog opened with showModal() moves focus inside it. A popover does not. After you press the invoker, focus stays on the button.
This is often the right default for small content such as a help note or a list of links. Keyboard users keep their place, and the content sits next to the invoker.
If the popover contains something the user must act on at once, such as a form field, move focus there with the autofocus attribute. Put it on the popover itself or on a descendant:
<button popovertarget="rename">Rename</button>
<div id="rename" popover>
<label for="new-name">New name</label>
<input id="new-name" autofocus>
</div>When the popover opens, the browser focuses the autofocus element. If you add autofocus to more than one element, the first match in tree order wins. For popovers, focus moves only at the moment of opening.
Tab order follows the DOM, not the top layer
The top layer changes how a popover is painted. It does not change where the popover sits in the document. Sequential keyboard navigation still follows source order.
Browsers add some help here. Opening a popover from its invoker generally lets Tab continue into the popover content. Relying on that alone is fragile, so place the popover in the DOM directly after its invoker. That way Tab and Shift+Tab behave sensibly even where the extra help is missing. It also keeps the reading order in a screen reader logical.
An auto popover does not trap focus. If the user tabs past the last control, focus moves on to whatever comes next in the page, and the popover stays open. Moving focus away does not trigger light dismiss. Only a click outside or Escape does.
For a menu-like popover, you may want it to close when focus leaves it. You can do that with a small script:
const menu = document.getElementById('menu');
menu.addEventListener('focusout', (event) => {
const next = event.relatedTarget;
if (next && !menu.contains(next)) {
menu.hidePopover();
}
});The next && check skips the case where focus leaves the window, when relatedTarget is null.
Focus handling over time
The sequence below shows a keyboard user opening a popover that contains an autofocus field, then closing it with Escape.
Keyboard dismissal and the manual type
Escape is the only keyboard close mechanism that the browser provides, and only auto popovers respond to it. A manual popover ignores Escape and light dismiss. It also does not close other popovers when it opens.
That puts the burden on you. Every manual popover needs a visible, focusable close control, such as a button with popovertargetaction="hide". Otherwise a keyboard user can be left with an overlay that covers content and cannot be removed.
<div id="notice" popover="manual" role="status" aria-label="Notification">
<p>Your changes were saved.</p>
<button popovertarget="notice" popovertargetaction="hide">Dismiss</button>
</div>If your manual popover has Escape support through your own script, listen for keydown on the document and call hidePopover(). Check that the popover is open first, with popover.matches(':popover-open'). Calling hidePopover() on a closed popover does nothing harmful in current browsers, but the explicit check makes the intent clear.
Semantics: roles, names, and labels
A div with popover has no role. Screen reader users hear its contents as plain text. Choose the markup that matches what the content is:
| Content | Better markup |
|---|---|
| A list of navigation links | A `nav` with a `ul` of links, with `aria-label` if the page has other navigation |
| Supplementary help text | A `div` or `aside` with a clear heading or `aria-label` |
| A brief status message | `role="status"` on the popover, or a persistent live region |
| A true application menu | A menu pattern with `role="menu"` and full arrow-key handling |
Be careful with role="menu". It tells assistive technology that arrow keys move between items, and it is your job to implement that. A list of links in a popover is not a menu, and marking it as one makes it harder to use. A plain nav with links is usually better.
Give the invoker a clear name. Text such as "Show help" or "Open filters" works. If the button holds only an icon, add aria-label. If the popover is a labelled region, use aria-labelledby pointing at its heading.
Announcements have a gotcha. A closed popover is display: none, so it is absent from the accessibility tree. A live region that appears and fills with text in the same moment is announced unreliably in some screen reader and browser pairings. For important status messages, keep an empty live region in the page and put the text into it as the popover opens.
Common errors
- Overriding
displayon the popover. Writing[popover] { display: flex; }makes the popover visible all the time, and it never hides. Scope the rule instead:[popover]:popover-open { display: flex; }. - Hiding the focus ring. Popovers often include links and buttons with custom styles. Keep a visible
:focus-visiblestyle on every control inside. - Placing the popover far from the invoker in the DOM. Tab order and reading order then jump around the page.
- Adding a manual
aria-expanded. The invoker relationship already reports state. Your copy can disagree with it. - Using a popover where focus must be contained. If the user must not reach the page behind, the content needs a modal dialog.
- Animating without a reduced-motion rule. Wrap large movement in
@media (prefers-reduced-motion: no-preference)so users who ask for less motion get a plain fade or nothing.
[popover]:focus-visible,
[popover] :focus-visible {
outline: 3px solid #1a5fb4;
outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
[popover] {
transition: none;
}
}A complete example
This page opens a rename popover. Focus moves to the field, Escape closes the popover, and focus goes back to the button.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Rename popover</title>
<style>
[popover] { padding: 1rem; border: 1px solid #888; border-radius: 8px; }
:focus-visible { outline: 3px solid #1a5fb4; outline-offset: 2px; }
</style>
</head>
<body>
<button popovertarget="rename">Rename file</button>
<div id="rename" popover aria-labelledby="rename-title">
<h2 id="rename-title">Rename file</h2>
<label for="new-name">New name</label>
<input id="new-name" autofocus>
<button popovertarget="rename" popovertargetaction="hide">Cancel</button>
</div>
</body>
</html>Test it with the keyboard only. Tab to the button and press Enter. Check that the field is focused. Press Escape and check that focus returns to "Rename file". Then try a screen reader to confirm the button announces its expanded state.
When a popover is not enough
If the user must finish or dismiss the content before doing anything else, use a modal dialog. It makes the rest of the page inert and contains focus. A popover leaves the rest of the page active, so everything behind it stays reachable by keyboard and by screen reader.
Exercise
A popover with popover="manual" opens a form. A keyboard user opens it and presses Escape, but nothing happens. Give two things you could change so that the keyboard user can close it.
Show answer
Add a focusable close button that uses popovertarget with popovertargetaction="hide", or switch to popover="auto" so Escape closes it. A keydown listener that calls hidePopover() on Escape also works. The manual type does not respond to Escape on its own.
Exercise
You write [popover] { display: grid; } in your stylesheet and the popover is now visible on page load. Why does this happen, and how do you fix it?
Show answer
Closed popovers are hidden by the browser's default display: none rule, and the author rule overrides it. Scope the rule to the open state with [popover]:popover-open { display: grid; }.
Exercise
After pressing Enter on a popover's invoker button, the user's focus is still on the button. Is this a bug? What would you add if the popover contains a text field the user must fill in?
Show answer
It is not a bug. Popovers do not move focus on open. Add the autofocus attribute to the text field, or to the popover itself, so focus moves there when it opens.