Sub topic 10
Focus, keyboard use, and accessibility
This chapter assumes you can write the basic popover and popovertarget markup from earlier chapters, and that you know how Tab, Enter, Space, and Escape work on buttons and links. It covers what the browser does for keyboard and screen reader users, and what you still have to do yourself.
What the browser provides
A popover opened with an invoker button gets several behaviors without extra code.
- Button semantics. A
<button>withpopovertargetis focusable and works with Enter and Space. - Relationship exposure. The browser tells assistive technology that the button controls the popover, and it reports whether the popover is expanded. You don't need to add
aria-expandedoraria-controlsby hand. - Escape. An
autopopover closes when the user presses Escape. - Focus return. When an
autopopover closes, focus goes back to the element that had focus before it opened, usually the invoker. This only happens if focus was inside the popover when it closed. - Keyboard order. The popover is reachable by Tab after its invoker, even if it sits elsewhere in the DOM.
What the browser does not provide
A popover is not modal. The rest of the page stays interactive, and focus is not trapped. Opening a popover also does not move focus into it. Keyboard users stay on the invoker, and the next Tab press should enter the popover.
A <div popover> has no semantic role. Screen readers announce it as a generic group, so add the role that matches its content.
<button popovertarget="actions">Actions</button>
<div id="actions" popover role="menu" aria-label="Document actions">
<button role="menuitem" autofocus>Rename</button>
<button role="menuitem">Duplicate</button>
<button role="menuitem" popovertarget="actions" popovertargetaction="hide">
Close
</button>
</div>The autofocus attribute on a child tells the browser to move focus there when the popover opens. Use it when the popover needs an immediate response, such as a menu or a form field. Skip it for passive content like a tooltip, where moving focus would interrupt the user.
Roles such as menu promise arrow-key navigation, which you would have to script. If your content is just a list of links, leave the role off and use plain <a> elements.
Keyboard behavior by type
| Behavior | `popover` (auto) | `popover="manual"` |
|---|---|---|
| Escape closes it | Yes | No |
| Opening another auto popover closes it | Yes, unless nested | No |
| Focus returns to invoker on close | Yes | Yes, if focus was inside |
| Needs a visible close control | Recommended | Required |
A manual popover has no built-in keyboard exit, so always include a close button that is reachable by Tab. Otherwise a keyboard user can get stuck with a notice they cannot dismiss.
Common mistakes
- Using a
<div>as the invoker. Only buttons (<button>and<input type="button">) work withpopovertarget. A clickabledivhas no keyboard support. - Hiding the focus outline. Removing
outlineon buttons inside the popover leaves keyboard users unable to see where they are. - Relying on color or hover alone. Show the open state with more than a color change, and make sure the popover content is not available only on hover.
- Skipping the accessible name. If the popover is a region or menu, give it
aria-labeloraria-labelledby.
Testing checklist
- Put your mouse away and open the popover with Enter or Space.
- Press Tab and confirm focus lands inside the popover.
- Press Escape (auto popovers) and confirm focus returns to the invoker.
- Repeat with a screen reader and confirm the invoker announces its expanded state.
Exercise
A keyboard user opens an auto popover from a button, tabs to a link inside it, and presses Escape. Where should focus be afterward, and what must you add to a popover="manual" element so the same user can close it?
Show answer
Focus returns to the invoker button, because focus was inside the popover when it closed. A manual popover ignores Escape, so it needs a keyboard-reachable close control, for example a button with popovertarget pointing at the popover and popovertargetaction="hide".