How to Build a Tooltip With the Popover API
Build a tooltip with the Popover API: popover attribute, popovertarget, hover and focus triggers, anchor positioning, accessibility and when to use a dialog.

Tooltips look trivial, a small box next to a control, and they are one of the most frequently broken components on the web. They fail keyboard users because they appear only on hover, fail touch users because there is no hover, cover other content, trap focus, never close or announce nothing to a screen reader. For years, the standard answer was a library that managed positioning, layers and events. The Popover API gives the browser the hard parts: showing an element in the top layer, light dismissal with outside clicks and the Escape key, correct focus return and accessibility wiring. Positioning, which used to need JavaScript, is moving to CSS anchor positioning. This guide shows how to build a tooltip with the Popover API: the popover attribute and its three states, opening with popovertarget and with script, the difference between auto and manual popovers, hover and focus triggers, positioning with CSS anchor positioning and a fallback, accessibility with aria-describedby, escape and light dismissal, styling and animation, when a popover is not a tooltip and testing. The API is documented on MDN and is supported by all current browsers, while anchor positioning is newer.
What a popover is
Adding the popover attribute to an element hides it by default and makes it a popover: an element that, when shown, goes into the top layer above everything else on the page, so it is never clipped by an overflow: hidden parent or covered by a high z-index. The browser also provides behavior you would otherwise code: light dismiss, which closes an auto popover when the user clicks outside it or presses Escape, and the accessible relationship between a button and its popover.
<button popovertarget="menu-info">What is a slug?</button>
<div id="menu-info" popover>
A slug is the readable part of a URL that identifies a post.
</div>(clicking the button toggles the popover with no JavaScript)The popovertarget attribute on a button points to the id of the popover and toggles it. No script is needed for a click-triggered popover. With popovertargetaction, set to show, hide or toggle, you can have separate buttons for each action.
Auto and manual popovers
The value of the attribute chooses the behavior. popover with no value, equal to popover="auto", is closed by light dismiss, and showing one closes other auto popovers that are not its ancestors, so only one tooltip or menu is open at a time. popover="manual" is not closed by outside clicks or Escape and does not close others; your code controls it, which is what you want for something shown on hover, since a hover tooltip must disappear when the pointer leaves, not wait for a click elsewhere.
<button id="save" aria-describedby="save-tip">Save</button>
<div id="save-tip" popover="manual" role="tooltip">Saves your changes (Ctrl+S)</div>(a manual popover that script shows and hides)Show and hide with script
The methods showPopover(), hidePopover() and togglePopover() control a popover from code, and the toggle and beforetoggle events report state changes. The :popover-open pseudo-class matches an open popover for styling.
const button = document.querySelector("#save");
const tip = document.querySelector("#save-tip");
function show() {
if (!tip.matches(":popover-open")) tip.showPopover();
}
function hide() {
if (tip.matches(":popover-open")) tip.hidePopover();
}
button.addEventListener("pointerenter", show);
button.addEventListener("pointerleave", hide);
button.addEventListener("focus", show);
button.addEventListener("blur", hide);
button.addEventListener("keydown", (event) => {
if (event.key === "Escape") hide();
});
show();
console.log(tip.matches(":popover-open"));trueCalling showPopover() on an element that is already open throws, so check state first, as the code does. Listening to focus and blur alongside the pointer events gives keyboard users the same tooltip, which is the main accessibility requirement: information available on hover must be available on focus. Escape should dismiss a tooltip without moving focus, which a manual popover leaves to you.
Position it next to the trigger
A popover is placed in the top layer without any relation to its trigger, by default centered in the viewport like a dialog. For a tooltip you need it next to the button. CSS anchor positioning, in current Chromium-based browsers and arriving elsewhere, ties it to the trigger with no script.
#save {
anchor-name: --save-button;
}
#save-tip {
position-anchor: --save-button;
position-area: block-start;
margin: 0;
margin-block-end: 8px;
position-try-fallbacks: flip-block;
}(the tooltip appears above the button, and flips below it when there is no room)anchor-name marks the trigger, position-anchor links the popover to it and position-area picks where it goes relative to the anchor: block-start is above in a horizontal writing mode. position-try-fallbacks lists alternatives when the first choice would overflow the viewport. The popover's default margin: auto centers it, so reset it. Where anchor positioning is unavailable, compute the position in script by reading getBoundingClientRect() of the trigger and setting top and left on the popover, or use the small Floating UI library, and check support with CSS.supports("anchor-name: --a").
function placeTooltip(trigger, tip) {
const rect = trigger.getBoundingClientRect();
tip.style.position = "fixed";
tip.style.margin = "0";
tip.style.left = `${rect.left + rect.width / 2 - tip.offsetWidth / 2}px`;
tip.style.top = `${rect.top - tip.offsetHeight - 8}px`;
}
if (!CSS.supports("anchor-name: --a")) {
tip.addEventListener("beforetoggle", (event) => {
if (event.newState === "open") requestAnimationFrame(() => placeTooltip(button, tip));
});
}(a script fallback positions the tooltip above the button in browsers without anchor positioning)The fallback does not flip near edges, so it is a basic route; the CSS version handles that.
Accessibility
A tooltip supplements a control's name and does not replace it. The trigger needs its own accessible name, such as a visible label or aria-label, and the tooltip adds a description. Connect them with aria-describedby on the trigger pointing to the tooltip's id, so that a screen reader reads the description after the name, and give the tooltip role="tooltip". For a popover that is a button's menu or a disclosure, popovertarget already exposes the expanded state, but a hover tooltip with popover="manual" is better described with aria-describedby.
<button id="delete" aria-describedby="delete-tip">
<svg aria-hidden="true" width="16" height="16"><!-- trash icon --></svg>
<span class="visually-hidden">Delete</span>
</button>
<div id="delete-tip" role="tooltip" popover="manual">Delete this item permanently</div>(a screen reader announces "Delete, button, Delete this item permanently")Rules that follow from the WCAG success criterion on content on hover or focus: the tooltip must stay visible when the pointer moves onto it, so do not hide it the moment the pointer leaves the button if the user can move to the bubble to read it, it must be dismissible with Escape without moving the pointer or focus and it must stay until the user dismisses it or moves away. Tooltip text must be short and not contain interactive elements, since those cannot be reached reliably. For anything with links or buttons, use a non-modal dialog or a popover that opens on click instead.
Keep it open while the pointer is on it
A common detail is the gap between the button and the tooltip: moving the pointer toward the tooltip crosses empty space, triggering pointerleave, which closes it. Handle that with a short delay, or make the tooltip a hover target as well.
let timer;
function scheduleHide() {
timer = setTimeout(hide, 120);
}
function cancelHide() {
clearTimeout(timer);
}
button.addEventListener("pointerenter", () => { cancelHide(); show(); });
button.addEventListener("pointerleave", scheduleHide);
tip.addEventListener("pointerenter", cancelHide);
tip.addEventListener("pointerleave", scheduleHide);(the tooltip stays open for 120 ms after the pointer leaves, so the user can move onto it)A show delay of a few hundred milliseconds also stops tooltips from flashing as the pointer crosses the page.
Touch devices
Touch has no hover. A tooltip that is the only place where important information lives is unusable on phones. Use visible text for essential information, and treat tooltips as extras. On touch, a tap on the button triggers its action, so a tooltip tied to hover never shows; tap-and-hold tooltips are unreliable. Where the tip matters, use a small info button with popovertarget, which works with a tap.
Style and animate
Style the popover with ordinary CSS. The ::backdrop pseudo-element exists for popovers too, though a tooltip normally has none. To animate the appearance, current browsers support @starting-style and transition-behavior: allow-discrete, so a popover can fade in and out.
[popover] {
padding: 0.4rem 0.6rem;
border: 0;
border-radius: 6px;
background: #1f2937;
color: #fff;
font-size: 0.875rem;
max-width: 18rem;
opacity: 0;
transition: opacity 0.15s, display 0.15s allow-discrete, overlay 0.15s allow-discrete;
}
[popover]:popover-open {
opacity: 1;
}
@starting-style {
[popover]:popover-open {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
[popover] {
transition: none;
}
}(the tooltip fades in over 150 ms, and not at all for users who prefer reduced motion)Make sure text and background meet the contrast ratio of 4.5 to 1, and that the text can be resized and does not clip.
Popover or dialog or title
Choose the right element. The title attribute gives a native tooltip, but it is not available to keyboard or touch users and cannot be styled, so avoid it for anything that matters. A popover suits menus, small cards, lists and tooltips that are not modal. A <dialog> opened with showModal() is for content that must be answered before the user continues. Use a disclosure (<details>) for content the user expands inline.
Test it
Operate every tooltip with the keyboard alone: Tab to the trigger and check that it appears, press Escape to dismiss it and move on. Hover with a mouse, including the move onto the bubble. Use a screen reader to confirm that the description is announced. Check the placement at the edges of the viewport, in right-to-left layouts and when the page is zoomed to 200 percent. Verify there is a fallback for browsers without anchor positioning, and that nothing important is available only in a tooltip.


