Tooltip

No class, no wrapper: a single data-text attribute is the whole API for tooltips.

Description

Any element with a data-text attribute shows a tooltip above it on hover and on keyboard focus. The bubble, its arrow and the fade transition are generated with the ::after and ::before pseudo-elements from the attribute's content.

An attribute-only API means zero classes in your markup, and focus support makes tooltips keyboard-accessible by default. The bubble inverts the text color ($color-text background, contrast text), uses the headings font family at 0.8em, and the radius follows $border-radius-button.

Tooltip on hover

The attribute is the whole API: no classes on the buttons, just data-text. The tooltip appears on hover and also when the button receives keyboard focus.

Move your mouse over the buttons:

HTML
<p>Move your mouse over the buttons:</p>
<div class="flex-row" style="align-items: center">
  <button data-text="Saves the current state">Save</button>
  <button class="secondary" data-text="Discards all changes">Reset</button>
</div>

Tooltip on a link

Tooltips work on any element that supports pseudo-elements, like links inside running text. Give the link its accessible name and let data-text add the decoration on top.

The results will be published next week. You can follow the updates on our changelog page or subscribe to the newsletter.

HTML
<p>The results will be published next week. You can follow the updates on our <a href="#" data-text="Opens the changelog in a new tab">changelog page</a> or subscribe to the newsletter.</p>

Long text wrapping

Long texts wrap up to 12rem wide instead of overflowing the viewport, so a full sentence still fits. Keep them short anyway: tooltips are pure decoration over accessible names.

HTML
<button data-text="This tooltip shows a longer text that wraps instead of overflowing the viewport">Hover for a long tooltip</button>