Tooltip

A tooltip is one attribute and needs no script

data-tooltip shows its text above the element on hover or keyboard focus. The text is plain text taken from the attribute.

data-side puts it above, below, left or right

data-align lines it up with the start, the centre or the end

On the left or right side, start is the top edge and end the bottom edge.

An icon button keeps its aria-label: the tooltip is not its name

A screen reader does not read the tooltip, so the button is named by aria-label.

It works on any element that can hold content: a badge, an icon, a word

An abbr gets a dotted underline. Give an element that is not focusable tabindex="0" when keyboard users need the text too.

Paid MRR

A field cannot draw a tooltip itself, so the attribute goes on a wrapper

input, select, textarea and img have no ::after. The wrapper shows the tooltip on hover and while the field inside it has keyboard focus.

Long text stays on one line and is cut off at 320px

Text wider than 20rem (320px) ends in an ellipsis. Use a popover for anything longer.

What a tooltip does not do

Limit What to do
It shows on hover and keyboard focus only; a touch screen has no hover. Never put information only in a tooltip.
A screen reader does not read it. Name an icon button with aria-label.
One line of plain text, cut off at 320px; no links, buttons or formatting. Use a popover.
These cannot draw one. Put it on a wrapper.
An element that already uses ::after cannot have one. Put it on a wrapper.
A scrolling area cuts it off at its edge. Choose a side that points into the area.
Near the right edge it reaches past the window. Give that element data-align="end".
It never flips to another side. Choose them for where the element sits.

Attributes

Attribute / element Values Description
data-tooltip text The text, plain, one line. On any element that can hold content.
data-side none or top (default), bottom, left, right The side of the element the tooltip is on.
data-align none or center (default), start, end Above or below: left edge, centre, right edge. Left or right: top edge, centre, bottom edge.
aria-label text Names an icon-only button; the tooltip does not.
tabindex="0" on a non-focusable element Lets keyboard users focus it and see the tooltip.
abbr[data-tooltip] A word or abbreviation with a dotted underline.