CSS tooltip demos

Back to the article

Hover over or tab to each trigger. Demos 3 to 5 use the Popover API and CSS anchor positioning, which work in Chrome 125, Firefox 147, Safari 26 and later.

1. Pure CSS tooltip from a data attribute

The text comes from data-tooltip through content: attr(data-tooltip). The arrow is a CSS triangle on ::before. Each trigger has tabindex="0" so keyboard users can reach it.

Tooltip on top Tooltip on bottom Tooltip on left Tooltip on right
<span class="tip-top" data-tooltip="I'm on top" tabindex="0">Tooltip on top</span>

[data-tooltip] { position: relative; }
[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute; z-index: 10;
  bottom: calc(100% + 10px); left: 50%; transform: translateX(-50%);
  width: max-content; max-width: 220px;
  opacity: 0; pointer-events: none; transition: opacity .15s;
}
[data-tooltip]:hover::after,
[data-tooltip]:focus-visible::after { opacity: 1; }

2. Accessible tooltip

A real element with role="tooltip", linked to the button with aria-describedby, so screen readers announce it. It shows on hover and focus. Three lines of JavaScript let Escape hide it, which WCAG 1.4.13 asks for.

Copies the page address
<span class="tip">
  <button aria-describedby="tip-copy">Copy link</button>
  <span role="tooltip" id="tip-copy">Copies the page address</span>
</span>

.tip { position: relative; }
.tip [role=tooltip] { position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%); opacity: 0; }
.tip:hover [role=tooltip], .tip:focus-within [role=tooltip] { opacity: 1; }

3. Tooltip inside overflow: hidden

Both cards have overflow: hidden. On the left, the absolutely positioned tooltip is cut off by the card. On the right, the tooltip is position: fixed and tied to its button with CSS anchor positioning, so the card can't clip it.

absolute (clipped)Saves a copy to your drafts
fixed + anchor (escapes)Saves a copy to your drafts
button { anchor-name: --save; }
.tooltip {
  position: fixed;              /* not clipped by the card's overflow: hidden */
  position-anchor: --save;
  position-area: top span-right;
  margin-bottom: 8px;
}

4. Flip when there's no room

The frame is a small page. Both tooltips ask for position-area: top with position-try-fallbacks: flip-block. The button at the very top has no room above it, so the browser flips its tooltip below. Scroll the frame and watch the lower one flip as it reaches the top.

.trigger { anchor-name: --a; }
.tooltip {
  position: fixed;
  position-anchor: --a;
  position-area: top;
  position-try-fallbacks: flip-block;   /* go below if it doesn't fit above */
  margin: 8px 0;
}

5. Click toggletip with the Popover API

No JavaScript. The button opens the popover with popovertarget. The popover sits in the browser's top layer, above every z-index. Clicking outside or pressing Escape closes it.

Delivery time
Orders placed before 2 pm ship the same day. Delivery takes 2 to 4 working days.
<button popovertarget="info-tip" aria-label="More about delivery time">i</button>
<div id="info-tip" popover>Orders placed before 2 pm ship the same day…</div>

button { anchor-name: --info; }
#info-tip {
  position-anchor: --info;
  position-area: bottom;
  position-try-fallbacks: flip-block;
  inset: auto; margin: 8px 0 0;        /* clear the popover's default centring */
}