Tooltip
A tooltip is a compact contextual label displayed on pointer hover or keyboard focus, conveying assistive hints (such as icon button labels or keyboard shortcuts).
The native HTML title attribute fails accessibility standards: it requires sustained pointer hovering, remains entirely unreachable via keyboard in most user agents, and cannot be invoked on touch screens.
Live Demo
Tab between the icon buttons below to verify that tooltips appear reliably on keyboard focus, and verify that pressing Escape dismisses the tooltip while preserving button focus:
Keyboard Interaction
| Key Interaction | Expected Behavioral Contract |
|---|---|
Tab | Focus the button; tooltips display immediately, identical to pointer hover |
Esc | Dismiss the visible tooltip immediately without moving focus away from the trigger |
Pointer Interactions
| Pointer Action | Expected Behavioral Contract |
|---|---|
| Hover Pointer | Displays the tooltip |
| Move Pointer Away | Conceals the tooltip (unless the trigger still retains keyboard focus) |
Rules
- Show the tooltip on keyboard focus too, and close it with
EscMUSTUI-R37 - A hover-only tooltip does not exist for keyboard users.
Esccloses it without moving focus, so whatever it covered can be checked at once. - Check:
TIP-01TIP-01Show on keyboard focus✕ NGNothing appears on keyboard focus✓ OKAlso shown on keyboard focus
- Give the tooltip
role="tooltip"and point to it from the trigger'saria-describedbyMUSTUI-R38 - The tooltip text is announced as a description, without replacing the trigger's name.
- Check:
TIP-02TIP-02Describe, don't rename✕ NGThe button's name is replaced✓ OKName kept, text is a description
- Do not put links or buttons inside a tooltip MUST NOTUI-R39
- Controls in an area that vanishes when the pointer leaves disappear before they can be pressed. Use a popover or a dialog for content that needs interaction.
- Check:
TIP-03TIP-03No controls inside✕ NGA control sits inside the tooltip✓ OKText only
- Do not use the
titleattribute in place of a tooltip MUST NOTUI-R40 - A
titlenever appears for keyboard or touch users. Show supplementary text with the tooltip on this page. - Check:
TIP-04TIP-04Not a title attribute✕ NGNot shown on keyboard focus✓ OKCan show on focus too
- Flip the tooltip's position near the edge of the screen SHOULDUI-R41
- Show it below when the trigger is at the top of the screen, and so on. The tooltip is never cut off.
- Check:
TIP-05TIP-05Not cut off at the edge✕ NGThe tooltip goes off screen✓ OKFlipped below, fully visible
Implementation
A complete, production-grade reference module:
View Source Code (HTML Markup and JavaScript Module)
html
<button type="button" aria-describedby="save-tip">
<svg aria-hidden="true">…</svg>
<span class="visually-hidden">Save document</span>
</button>
<div role="tooltip" id="save-tip" hidden>Save (Ctrl+S)</div>javascript
// modules/tooltip.js
export function initTooltip(trigger, tooltip) {
function show() {
tooltip.hidden = false
const spaceAbove = trigger.getBoundingClientRect().top
tooltip.classList.toggle('tooltip-below', spaceAbove < tooltip.offsetHeight + 12)
}
function hide() {
if (document.activeElement !== trigger) {
tooltip.hidden = true
}
}
trigger.addEventListener('focus', show)
trigger.addEventListener('blur', () => {
tooltip.hidden = true
})
trigger.addEventListener('mouseenter', show)
trigger.addEventListener('mouseleave', hide)
trigger.addEventListener('keydown', (event) => {
if (event.key === 'Escape') {
tooltip.hidden = true
}
})
}- Return to Keyboard & ARIA Patterns