Full answer
A tooltip is a small box with extra text for a control, such as an icon button. A good one follows three rules from the accessibility guidelines for content that appears on hover or focus.
- Dismissible: the user can hide it without moving the pointer or focus, usually with the Escape key.
- Hoverable: the user can move the pointer onto the tooltip without it disappearing, so that people who zoom in can read it.
- Persistent: it stays until the user moves away, dismisses it or the information is no longer valid. It must not vanish on a timer.
It must also be reachable by keyboard. Show it on :hover and on :focus-visible (or :focus-within) of the trigger.
Structure:
- The trigger is a real
buttonor link. Do not make a tooltip for a non-interactive element, because keyboard users cannot reach it. - The tooltip element has
role="tooltip"and anid. The trigger points to it witharia-describedby, so a screen reader reads the text as a description after the name. - The trigger still needs its own accessible name, such as text or
aria-label. A tooltip is a description, not a name. - Do not put links, buttons or long text in a tooltip. For richer content use a popover or a dialog.
To keep the tooltip open while the pointer moves from trigger to tooltip, make the tooltip a child of a wrapper and show it on .wrap:hover. The wrapper covers both, and a small gap is bridged with padding.
Hovering or focusing the button shows the tooltip. Moving the pointer down onto the tooltip keeps it visible, because it is inside the wrapper. Pressing Escape hides it until the pointer or focus leaves.
Touch screens have no hover, so do not hide essential information in a tooltip.
<style>
.wrap { position: relative; display: inline-block; }
.tip { position: absolute; left: 0; top: 100%; padding-top: 4px; display: none; }
.tip span { display: block; background: #222; color: #fff; padding: 4px 8px; font: 14px sans-serif; }
.wrap:hover .tip, .wrap:focus-within .tip { display: block; }
.wrap.dismissed .tip { display: none; }
</style>
<div class="wrap" id="w">
<button type="button" id="b" aria-describedby="tip" aria-label="Help">?</button>
<div class="tip" id="tipbox"><span role="tooltip" id="tip">Pay by the 5th of each month.</span></div>
</div>
<script>
w.addEventListener("keydown", e => { if (e.key === "Escape") w.classList.add("dismissed"); });
for (const ev of ["mouseleave", "focusout"]) w.addEventListener(ev, () => w.classList.remove("dismissed"));
</script>Checked with mouse hover and real key presses in Chrome. No screen reader was run, and touch was not tested. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Show on hover and on focus.
- Dismissible with Escape, hoverable and persistent.
- role=tooltip with aria-describedby on the trigger.
- The trigger needs its own accessible name.
- No essential or interactive content in a tooltip.
Common mistakes
- Using the title attribute as the only tooltip, which keyboard and touch users usually cannot reach.
- Hiding the tooltip when the pointer leaves the trigger, so it cannot be hovered.
- Putting essential instructions only in a tooltip.
Follow-ups
- Why is the title attribute a poor tooltip?
- When should you use a popover instead?
Tips
- Name the three requirements: dismissible, hoverable, persistent.
- Say that aria-describedby gives a description and the control still needs a name.