Tooltip Examples
The examples below assume that you have imported the component and set up the theme.
With the default triggers, tooltips never reveal on touch devices. To reach touch users, use trigger="click" on a tooltip icon, or trigger="long-press" on a control that already does something.
Basic
Describe a button with a tooltip that shows on hover or focus. data-sv-tooltip on the button names the id of the tooltip, and aria-describedby links the two for assistive technology.
<div class="tooltip-basic"> <button type="button" class="btn btn-surface" aria-describedby="tooltip-save" data-sv-tooltip="tooltip-save" > Save </button> <sv-tooltip id="tooltip-save">Writes the draft to disk</sv-tooltip></div>
<style> .tooltip-basic { display: flex; justify-content: center;
sv-tooltip { font-size: 0.875em; white-space: nowrap; } }</style>Trigger
Reveal a tooltip from an info button, also on touch devices. trigger="click" opens it on a click or a tap, and aria-label names the icon-only button.
Pick long-press in the trigger control to open the tooltip on press and hold, for a control whose tap already does something.
<div class="tooltip-trigger"> <span>Storage region</span>
<button type="button" aria-describedby="tooltip-info" aria-label="About storage region" data-sv-tooltip="tooltip-info" > <svg aria-hidden="true" fill="currentColor" fill-rule="evenodd" viewBox="0 0 24 24"> <path d="M12 2a10 10 0 1 0 0 20 10 10 0 1 0 0-20Zm-1 5h2v2h-2V7Zm0 4h2v6h-2v-6Z" /> </svg> </button>
<sv-tooltip id="tooltip-info" trigger="click"> Data is stored in the region closest to you </sv-tooltip></div>
<style> .tooltip-trigger { display: flex; gap: 0.25em; align-items: center; justify-content: center;
button { display: inline-flex; padding: 0.2em; color: var(--sv-gray-3); cursor: pointer; user-select: none; background: none; border: none; border-radius: 50%;
svg { inline-size: 1.4em; } }
sv-tooltip { font-size: 0.875em; white-space: nowrap; } }</style>Scrolling
Keep a tooltip beside its button while a scroll container scrolls. data-sv-tooltip anchors it to the button, and reveal-delay="0" shows it with no wait. It flips to another side only near the edge of the window, not of the scroll container.
<div class="tooltip-scrolling"> <div class="tooltip-scroller"> <div class="tooltip-canvas"> <button type="button" class="btn btn-surface" aria-describedby="tooltip-scroll" data-sv-tooltip="tooltip-scroll" > Scroll me around </button> </div> </div>
<sv-tooltip id="tooltip-scroll" reveal-delay="0">Anchored with CSS only</sv-tooltip></div>
<style> .tooltip-scrolling { .tooltip-scroller { block-size: 12rem; overflow: auto; overscroll-behavior: contain; background-color: var(--sv-gray-7); border: solid 1px var(--sv-gray-6); border-radius: var(--sv-bdr-rad-md); }
.tooltip-canvas { display: flex; align-items: start; justify-content: center; block-size: 30rem; padding-block-start: 4rem; }
sv-tooltip { font-size: 0.875em; white-space: nowrap; } }</style>Styling
Restyle the tooltip with CSS variables and plain CSS on the element. --sv-tooltip-offset sets the gap to the button, --sv-tooltip-bdr-clr colors the border, side="inline-end" puts the tooltip after the button, and the default slot takes an icon with text.
<div class="tooltip-styling"> <button type="button" class="btn btn-surface" aria-describedby="tooltip-delete-styled" data-sv-tooltip="tooltip-delete-styled" > Delete </button>
<sv-tooltip id="tooltip-delete-styled" side="inline-end"> <span class="tooltip-danger"> <svg aria-hidden="true" fill="currentColor" fill-rule="evenodd" viewBox="0 0 24 24"> <path d="M12 2 1 21h22L12 2Zm-1 7h2v5h-2V9Zm0 7h2v2h-2v-2Z" /> </svg> Cannot be undone </span> </sv-tooltip></div>
<style> .tooltip-styling { display: flex; justify-content: center;
sv-tooltip { --sv-tooltip-offset: 1em; --sv-tooltip-bdr-clr: crimson;
font-size: 0.875em; white-space: nowrap; }
.tooltip-danger { display: flex; gap: 0.4em; align-items: center;
svg { inline-size: 1em; color: crimson; } } }</style>Animation
Change the tooltip animation, or replace it with a spring or a slide. --sv-tooltip-dur and --sv-tooltip-ease set the timing, and :popover-open with @starting-style and transition-property builds a custom transition.
Since the arrow points at the button only while the tooltip has no transform, end a scale or a translate at none, not at 1 or 0 0.
Setting --sv-tooltip-dur on the page also overrides the reduced motion fallback, and you need to restore it yourself.
This animation will not play for you because your system is set to reduce motion.
<div class="tooltip-animation"> <button type="button" class="btn btn-surface" aria-describedby="tooltip-slide" data-sv-tooltip="tooltip-slide" > Slide </button> <sv-tooltip id="tooltip-slide" class="slide" reveal-delay="0">Slides in from below</sv-tooltip>
<button type="button" class="btn btn-surface" aria-describedby="tooltip-spring" data-sv-tooltip="tooltip-spring" > Spring </button> <sv-tooltip id="tooltip-spring" class="spring" reveal-delay="0" side="block-end"> Bounces in with a spring easing </sv-tooltip></div>
<style> .tooltip-animation { display: flex; gap: 1em; justify-content: center;
sv-tooltip { font-size: 0.875em; white-space: nowrap; }
sv-tooltip.spring { --sv-tooltip-dur: 200ms; --sv-tooltip-ease: linear(0, 0.6 15%, 1.15 35%, 0.97 55%, 1.01 75%, 1);
scale: 0.5; transition-property: opacity, scale, display, overlay; }
sv-tooltip.spring:popover-open { scale: none;
@starting-style { scale: 0.5; } }
sv-tooltip.slide { opacity: 0; translate: 0 0.75rem; transition-property: opacity, translate, display, overlay; }
sv-tooltip.slide:popover-open { opacity: 1; translate: none;
@starting-style { opacity: 0; translate: 0 0.75rem; } }
@media (prefers-reduced-motion: reduce) { sv-tooltip.spring { --sv-tooltip-dur: 0ms; } } }</style>Programmatic
Attach and open a tooltip from JavaScript, for example as click feedback. targets attaches it without data-sv-tooltip, open() shows it, and beforetoggle restores the label.
<div class="tooltip-programmatic"> <button type="button" class="btn btn-surface">Save draft</button>
<sv-tooltip>Writes the draft to disk</sv-tooltip></div>
<script type="application/x-typescript" module> const tooltip = document.querySelector<Tooltip>(".tooltip-programmatic sv-tooltip")!; const button = document.querySelector<HTMLButtonElement>(".tooltip-programmatic button")!;
tooltip.targets = [button];
button.addEventListener("click", () => { tooltip.open(); tooltip.textContent = "Saved"; });
tooltip.addEventListener("beforetoggle", event => { if (event.newState === "open") { tooltip.textContent = "Writes the draft to disk"; } });</script>
<style> .tooltip-programmatic { display: flex; justify-content: center;
sv-tooltip { font-size: 0.875em; white-space: nowrap; } }</style>