🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Components & Islands••By Komal Nakrani

Astro Tooltip Component: Accessible, CSS-First

Build an accessible Astro tooltip component with CSS hover and focus, aria-describedby, Escape to dismiss and a few lines of script. No library, no island.

#Astro#Tooltip#Components#Accessibility

An Astro tooltip can be a single .astro component: a wrapper around the trigger, a hidden element with role="tooltip" linked through aria-describedby, and CSS that shows it on :hover and :focus-within. Add a three-line script so Escape dismisses it, and you have an accessible tooltip that ships almost no JavaScript and needs no island.

Key Takeaways

  • Tooltips describe a control; they shouldn’t hold content people must read or click.
  • Link the tip with aria-describedby so screen readers announce it with the trigger.
  • Show on hover and keyboard focus, and let Escape hide it (WCAG 1.4.13).
  • Use a dialog or popover instead if the content is interactive.

The component

---
// src/components/Tooltip.astro
interface Props {
  text: string;
  placement?: 'top' | 'bottom';
}
const { text, placement = 'top' } = Astro.props;
const id = `tip-${Math.random().toString(36).slice(2, 9)}`;
---
<span class="tooltip" data-placement={placement} data-tip-id={id}>
  <slot />
  <span id={id} role="tooltip" class="tip">{text}</span>
</span>

<style>
  .tooltip { position: relative; display: inline-flex; }
  .tip {
    position: absolute; left: 50%; translate: -50% 0; z-index: 20;
    width: max-content; max-width: 16rem; padding: 0.375rem 0.625rem;
    border-radius: 0.375rem; background: #111827; color: #fff;
    font-size: 0.8125rem; line-height: 1.4; pointer-events: none;
    opacity: 0; visibility: hidden; transition: opacity 120ms ease;
  }
  [data-placement='top'] .tip { bottom: calc(100% + 0.5rem); }
  [data-placement='bottom'] .tip { top: calc(100% + 0.5rem); }
  .tooltip:hover .tip,
  .tooltip:focus-within .tip { opacity: 1; visibility: visible; }
  .tooltip.is-dismissed .tip { opacity: 0; visibility: hidden; }
  @media (prefers-reduced-motion: reduce) { .tip { transition: none; } }
</style>

<script>
  document.querySelectorAll<HTMLElement>('.tooltip').forEach((wrap) => {
    const trigger = wrap.querySelector<HTMLElement>('button, a, input, [tabindex]');
    trigger?.setAttribute('aria-describedby', wrap.dataset.tipId!);
    wrap.addEventListener('keydown', (e) => { if (e.key === 'Escape') wrap.classList.add('is-dismissed'); });
    wrap.addEventListener('mouseleave', () => wrap.classList.remove('is-dismissed'));
    wrap.addEventListener('focusout', () => wrap.classList.remove('is-dismissed'));
  });
</script>

Use it around any focusable element:

---
// src/pages/settings.astro
import Tooltip from '../components/Tooltip.astro';
---
<Tooltip text="Copies the public link to your clipboard">
  <button type="button">Copy link</button>
</Tooltip>

The script sets aria-describedby on the first focusable child. A screen reader then reads “Copy link, button, Copies the public link to your clipboard”. Astro bundles the script once, however many tooltips are on the page.

Why each piece is there

  • :focus-within makes the tip appear for keyboard users, not only mouse users.
  • visibility: hidden rather than display: none keeps the transition working while still hiding the tip from pointer events.
  • pointer-events: none stops the tip blocking clicks on nearby elements.
  • Escape satisfies WCAG’s “dismissible” rule for content shown on hover or focus.
  • A random id keeps several tooltips on one page from colliding. If you render the same component inside a loop, each instance gets its own.

Tooltip or something else

Tooltips are for short, non-essential hints. Pick a different pattern when:

You need to show Use
A label for an icon-only button aria-label plus a visible tooltip with the same text
Several sentences or links A disclosure (<details>) or a popover
A form field’s requirements Visible help text under the field
Content on touch devices Anything except hover; touch has no hover state

On phones, the tooltip appears when the trigger receives focus on tap, but people won’t discover it. Don’t hide anything important there.

Positioning near screen edges

The CSS above centres the tip over the trigger, which can overflow near the edge of the viewport. Simple fixes are a placement prop for edge cases and max-width so text wraps. For automatic flipping, CSS anchor positioning is the modern route where browsers support it, and Floating UI is the established library if you need it everywhere today; load it only on pages that use it.

The modal dialog component covers the interactive cousin of this pattern, and when to use an island or a script explains why a plain script is enough here. The styling docs show where component styles live in a Komal theme, and every one of our Astro themes uses scoped component styles the same way.

FAQ

Do I need JavaScript for an Astro tooltip?

The show and hide behaviour is pure CSS. A few lines of script add aria-describedby and Escape-to-dismiss. You could hard-code the attribute and skip the script, but Escape support is worth keeping.

Can I use the title attribute as a tooltip?

It works as a fallback, but browsers show it after a delay, it isn’t shown on keyboard focus, and styling it is impossible. A custom tooltip with aria-describedby is more reliable.

It shouldn’t. If people need to move into it and click, use a popover or a disclosure, which can receive focus and stay open.