Web design tutorials and tools since 2014Privacy
Search 151 articles

Coding

CSS Tooltips: Pure CSS, Popover API and Anchor Positioning

A CSS tooltip is a small label that appears when someone hovers over or focuses an element. You can build one with pure CSS, reading the text from an HTML data attribute. Modern browsers add two better tools: the Popover API, which puts a tooltip above everything else on the page, and CSS anchor positioning, which ties it to its trigger and flips it when there's no room.

This guide covers all three, plus the accessible version. Every demo is on our live demo page, and we tested each one in Chrome 158, Firefox 160 and WebKit 27.2 (Safari's engine). All three gave the same results.

The Short Answer

  • A short hint on a word or icon: a pure CSS tooltip with data-tooltip and ::after. No JavaScript, but screen readers treat the text as part of the element's name.
  • A tooltip on a button or form field: a real element with role="tooltip" and aria-describedby, shown on hover and focus.
  • A tooltip inside a card, table or scroll area: add CSS anchor positioning with position: fixed, so overflow: hidden can't clip it.
  • Extra information that needs a click, especially on phones: a toggletip built with the Popover API. It needs no JavaScript and closes on Escape or an outside click.

1. Pure CSS Tooltip With a Data Attribute

Four pure CSS tooltips with arrows, shown above, below, to the left and to the right of their trigger words
One data-tooltip attribute, four positions, each with a CSS arrow.

Put the tooltip text in a data-tooltip attribute on the element you want to explain:

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

Then let CSS read it with attr() and draw it in the ::after pseudo-element. The tooltip is always there but invisible, so it can fade in:

[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;
  padding: 6px 10px;
  border-radius: 6px;
  background: #1a1a1a;
  color: #fff;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .15s;
}

[data-tooltip]:hover::after,
[data-tooltip]:focus-visible::after {
  opacity: 1;
}

A few details make this work better than the usual snippet:

  • :focus-visible and tabindex="0" let keyboard users open the tooltip with Tab. A tooltip that only appears on :hover doesn't exist for them. In our test, focusing a trigger with the keyboard showed the tooltip in all three browsers.
  • width: max-content with a max-width keeps short labels on one line and wraps long ones. white-space: nowrap, used in many older examples, lets long text run off the screen.
  • pointer-events: none stops the hidden tooltip from catching clicks meant for the page underneath.
  • A plain number for z-index. The 2015 version of this tutorial used z-index: 20px. That's invalid, so browsers ignore it: the computed value was auto in all three. z-index takes a whole number with no unit.

Adding an arrow

The arrow is a CSS triangle on ::before: an empty box with a thick transparent border where only one side is coloured.

[data-tooltip]::before {
  content: "";
  position: absolute;
  z-index: 10;
  bottom: calc(100% - 2px);
  left: 50%;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top-color: #1a1a1a;
  opacity: 0;
  pointer-events: none;
  transition: opacity .15s;
}

[data-tooltip]:hover::before,
[data-tooltip]:focus-visible::before {
  opacity: 1;
}

Tooltip on the bottom, left or right

Only the offsets change. Swap these into the rules above, and move the coloured border of the arrow to the side that faces the trigger:

/* Bottom */
.tip-bottom::after  { top: calc(100% + 10px); left: 50%; transform: translateX(-50%); }
.tip-bottom::before { top: calc(100% - 2px); left: 50%; transform: translateX(-50%); border-bottom-color: #1a1a1a; }

/* Left */
.tip-left::after  { right: calc(100% + 10px); top: 50%; transform: translateY(-50%); }
.tip-left::before { right: calc(100% - 2px); top: 50%; transform: translateY(-50%); border-left-color: #1a1a1a; }

/* Right */
.tip-right::after  { left: calc(100% + 10px); top: 50%; transform: translateY(-50%); }
.tip-right::before { left: calc(100% - 2px); top: 50%; transform: translateY(-50%); border-right-color: #1a1a1a; }

The tooltip is positioned against its trigger because the trigger has position: relative. If one appears somewhere else, an ancestor is probably the containing block. Our CSS position guide explains containing blocks and z-index with live demos.

The catch: screen readers hear the tooltip as part of the name

Text in ::after is real content to assistive technology. We put data-tooltip="Copies the link" on a Copy button and read Chrome's accessibility tree. The button's name became "CopyCopies the link", run together and with no description. That's fine for a decorative hint on a word. For a button, an icon or a form field, use the accessible version below.

2. Accessible Tooltip With role="tooltip"

A Copy link button with a tooltip above it reading Copies the page address
A real element linked with aria-describedby, so it's announced as a description.

Put the tooltip in a real element, give it role="tooltip" and an id, and point the trigger at it with aria-describedby:

<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; display: inline-block; }

.tip [role="tooltip"] {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: 240px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .15s;
}

.tip:hover [role="tooltip"],
.tip:focus-within [role="tooltip"] {
  opacity: 1;
}

We tested the same pattern on a Copy button. Chrome's accessibility tree gave it the name "Copy" and the description "Copies the link", which screen readers read after the name. :focus-within shows it while the button has keyboard focus.

WCAG 1.4.13 also asks that content shown on hover or focus can be dismissed without moving the pointer or focus, usually with Escape. CSS can't do that alone, so add a few lines of JavaScript:

document.addEventListener('keydown', (e) => {
  if (e.key === 'Escape') document.querySelectorAll('.tip').forEach((t) => t.classList.add('dismissed'));
});
document.querySelectorAll('.tip').forEach((t) => {
  t.addEventListener('mouseleave', () => t.classList.remove('dismissed'));
  t.addEventListener('focusout', () => t.classList.remove('dismissed'));
});

Then add .tip.dismissed [role="tooltip"] { opacity: 0; }. In our test, pressing Escape hid the focused tooltip in all three browsers.

3. Tooltips That Escape overflow: hidden

Two cards with overflow hidden: on the left the tooltip above the Save button is cut off, on the right the anchored tooltip shows in full above the card
Left: an absolute tooltip, clipped by the card. Right: position fixed plus anchor positioning.

The most common tooltip bug is a tooltip that's cut off. If any ancestor has overflow: hidden, auto or scroll, an absolutely positioned tooltip can't paint outside it. In our demo, only 17% of the tooltip was visible inside the card. Cards with rounded images, tables and scrolling panels all cause this.

CSS anchor positioning fixes it. Give the trigger an anchor-name, make the tooltip position: fixed, and place it against the anchor with position-area:

.save-button {
  anchor-name: --save;
}

.save-tooltip {
  position: fixed;
  position-anchor: --save;
  position-area: top span-right;
  margin-bottom: 8px;
}

A fixed element isn't clipped by its ancestors' overflow, and the anchor keeps it next to the button as the page scrolls. In our test, the anchored tooltip showed in full, 8 pixels above the button, while the absolute one was clipped. One exception: an ancestor with a transform or filter traps fixed elements too, as our CSS position guide shows.

Each trigger needs its own anchor name. With several tooltips, set it inline (style="anchor-name: --t3") or use anchor-scope.

4. Flip the Tooltip When There's No Room

A frame with two buttons: the one at the top edge has its tooltip flipped below, the one in the middle has its tooltip above
Both tooltips ask to sit on top. The browser flips the one with no room above.

A tooltip set to appear above a button near the top of the screen is pushed off it. Anchor positioning can try another position automatically:

.tooltip {
  position: fixed;
  position-anchor: --a;
  position-area: top;
  position-try-fallbacks: flip-block;
  margin: 8px 0;
}

flip-block mirrors the tooltip to the other side of the anchor when it doesn't fit. In our demo, a button 4 pixels from the top of the frame got its tooltip below it, with an 8-pixel gap, while a button in the middle kept its tooltip above. When we scrolled the second button to the top, its tooltip flipped below too. Use flip-inline for left and right tooltips.

5. Click Toggletip With the Popover API

An info button next to the words Delivery time, with a dark popover below it explaining delivery times
A toggletip: the popover opens on click and sits in the top layer.

Hover doesn't exist on touch screens, and some information is too long for a hover tooltip. A toggletip opens on click instead. The Popover API builds one with no JavaScript:

<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;        /* clear the popover's default centring */
  margin: 8px 0 0;
}

A popover opens in the browser's top layer, above every z-index on the page, so stacking problems disappear. In our test it was the top element at its own position in all three browsers. The default popover (auto) value also closes on an outside click or Escape. We checked both. Popovers are centred in the viewport by default, so reset inset and margin before anchoring.

Opening a popover on hover is still uneven. The interestfor attribute does it without script, but MDN lists it only in Chrome 142 and later. popover="hint", which lets a tooltip open without closing other popovers, has full support only in Chrome 151, Firefox 153 and Safari 27.3. For hover, keep using methods 1 to 4, or call showPopover() from a mouseenter listener.

Why Not Just Use the title Attribute?

The browser shows title text as a native tooltip, so it's the quickest option, and it was the alternative in the original version of this tutorial. Its problems:

  • You can't style it, and it appears after a delay you can't control.
  • It doesn't appear on keyboard focus or on touch screens.
  • Screen reader support for title is inconsistent.

If you style your own tooltip with content: attr(title), remove the title or the browser will show its own tooltip on top of yours.

Browser Support

Versions from MDN's browser compatibility data (version 8.1.10):

FeatureChromeFirefoxSafari
attr() in content213.1
popover and popovertarget11412517
anchor-name, position-area, position-try-fallbacks12514726
popover="hint" (full support)15115327.3
interestfor142NoNo

Anchor positioning is the newest part. Older browsers ignore the anchor properties, so an anchored tooltip falls back to plain position: fixed and won't sit next to its trigger. Where those browsers still matter, keep method 1 or 2 as the fallback.

How We Tested

All five demos are on one page. We loaded it with Playwright in Chrome 158, Firefox 160 and WebKit 27.2 at 1280 × 900. We hovered and tabbed to each trigger and read the tooltip's computed opacity. For positions we used getBoundingClientRect(), and for the top layer we checked elementFromPoint(). The popover was opened by clicking the real button, then closed with an outside click and with Escape. The accessible names come from Chrome's accessibility tree, read through the DevTools protocol.

FAQ

How do I make a tooltip with only CSS?

Put the text in a data-tooltip attribute, give the element position: relative, and show content: attr(data-tooltip) in an absolutely positioned ::after on :hover and :focus-visible.

How do I add an arrow to a CSS tooltip?

Use ::before with content: "", a transparent border such as 6px solid transparent, and colour only the side that points at the trigger, for example border-top-color for a tooltip above.

Why is my tooltip cut off?

An ancestor has overflow: hidden, auto or scroll. Make the tooltip position: fixed and anchor it to its trigger with CSS anchor positioning, or use a popover, which sits in the top layer.

Why does my tooltip appear behind other elements?

Its z-index only counts inside its stacking context. Raise the ancestor that creates the context, or use a popover, which appears above every z-index. Also check that z-index is a plain number: z-index: 20px is ignored.

Are CSS tooltips accessible?

Pseudo-element tooltips are only partly accessible: keyboard users need tabindex and :focus-visible, and screen readers fold the text into the element's name. For important information, use a real element with role="tooltip" and aria-describedby, and let Escape dismiss it.

Do CSS tooltips work on mobile?

Not reliably, because touch screens have no hover. Use a toggletip that opens on tap. The Popover API with popovertarget builds one with no JavaScript.