Skip to content

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 ​

Keyboard operations and expected behavior
Key InteractionExpected Behavioral Contract
TabFocus the button; tooltips display immediately, identical to pointer hover
EscDismiss the visible tooltip immediately without moving focus away from the trigger

Pointer Interactions ​

Pointer operations and expected behavior
Pointer ActionExpected Behavioral Contract
Hover PointerDisplays the tooltip
Move Pointer AwayConceals the tooltip (unless the trigger still retains keyboard focus)

Rules ​

Show the tooltip on keyboard focus too, and close it with Esc MUSTUI-R37
A hover-only tooltip does not exist for keyboard users. Esc closes it without moving focus, so whatever it covered can be checked at once.
Check: TIP-01TIP-01 Show on keyboard focus✕ NGNothing appears on keyboard focusFocusing the button with Tab shows no tooltip✓ OKAlso shown on keyboard focusFocusing the button with Tab shows the tooltip
Give the tooltip role="tooltip" and point to it from the trigger's aria-describedby MUSTUI-R38
The tooltip text is announced as a description, without replacing the trigger's name.
Check: TIP-02TIP-02 Describe, don't rename✕ NGThe button's name is replacedAria-labelledby points at the tooltip and overrides the button's name✓ OKName kept, text is a descriptionAria-describedby points at the tooltip and the name is kept
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-03 No controls inside✕ NGA control sits inside the tooltipThe tooltip contains an "Open settings" button✓ OKText onlyThe tooltip shows text only
Do not use the title attribute in place of a tooltip MUST NOTUI-R40
A title never appears for keyboard or touch users. Show supplementary text with the tooltip on this page.
Check: TIP-04TIP-04 Not a title attribute✕ NGNot shown on keyboard focusThe supplementary label is given only by a title attribute✓ OKCan show on focus tooA role="tooltip" element is linked with aria-describedby
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-05 Not cut off at the edge✕ NGThe tooltip goes off screenFor a trigger at the top edge, the tooltip is cut off above the screen✓ OKFlipped below, fully visibleThe tooltip flips below and is 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
    }
  })
}