This guide summarizes best practices for accessible tooltips, drawing from leading accessibility experts.
Tooltips offer brief text hints for UI elements, appearing on hover or focus. A concise definition: a non-modal overlay providing supplemental, descriptive text about a UI control. Crucially, accessible tooltips contain only descriptive text and no interactive elements. If interactivity is needed, use a dialog instead.
Tooltips serve two main purposes:
Icon Labeling: For brief labels (one or two words), use the aria-labelledby
attribute. For additional context (e.g., a notification count), provide a space-separated list of IDs.
<button aria-labelledby="notification-count notification-label"> <span id="notification-count">3</span> <span id="notification-label">Notifications</span> </button> <div role="tooltip" id="tooltip-label">Notifications</div>
Contextual Description: For longer descriptions, use aria-describedby
. The icon itself needs an accessible name, ideally included as hidden text within the element.
<button aria-describedby="tooltip-description"> <span style="display:none;">Notifications</span> <span aria-hidden="true">?</span> </button> <div role="tooltip" id="tooltip-description">View and manage notification settings</div>
Do:
aria-labelledby
or aria-describedby
appropriately.role="tooltip"
even if current screen reader support is limited; future support may improve.Don't:
title
attribute (it has significant accessibility issues).aria-haspopup
with role="tooltip"
(tooltips are non-interactive).Tooltips are inherently inaccessible to touch devices due to the lack of hover and focus. The best solution is often to integrate the label or description directly into the design. For extensive content (including interactive elements), consider a Toggletip or a <dialog></dialog>
element. Toggletips, often indicated by an "i" icon, reveal information within a live region using the role="status"
.
<button aria-controls="toggletip-content"> <span aria-hidden="true">ⓘ</span> </button> <div id="toggletip-content" role="status" aria-live="assertive" style="display:none;">This clarifies whatever needs clarifying</div>
For more on toggletips, refer to the resources below.
The above is the detailed content of Tooltip Best Practices. For more information, please follow other related articles on the PHP Chinese website!