How to Build Accessible Pure CSS Tooltips & Popovers (Zero JavaScript)
To build a pure CSS tooltip without JavaScript, declare a data-tooltip="..." attribute on the trigger element and render the bubble using an ::after pseudo-element with content: attr(data-tooltip). Position it using position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%). Animate opacity and visibility on :hover and :focus-visible, and draw the directional arrow using a border-based ::before pseudo-element.
Step 1: The data-tooltip Attribute & attr() Content Generator
Pure CSS tooltips avoid bloated third-party JavaScript libraries by reading custom HTML5 data attributes directly into the CSS generated content engine. Placing content: attr(data-tooltip) on an ::after pseudo-element renders the tooltip dynamically:
[data-tooltip] {
position: relative;
cursor: pointer;
}
[data-tooltip]::after {
content: attr(data-tooltip);
position: absolute;
bottom: calc(100% + 8px);
left: 50%;
transform: translateX(-50%) translateY(4px);
background: #0f172a;
color: #f8fafc;
padding: 6px 12px;
border-radius: 6px;
font-size: 12px;
white-space: nowrap;
opacity: 0;
visibility: hidden;
transition: opacity 0.2s ease, transform 0.2s ease;
pointer-events: none;
z-index: 100;
}
[data-tooltip]:hover::after,
[data-tooltip]:focus-visible::after {
opacity: 1;
visibility: visible;
transform: translateX(-50%) translateY(0);
}
Step 2: Drawing Directional Pointing Arrows with CSS Borders
Directional arrows connect the tooltip bubble to the trigger element without SVG icons. By setting width and height to 0 and declaring three transparent borders with one solid border on the opposite edge, you create a sharp CSS arrow triangle:
[data-tooltip]::before {
content: '';
position: absolute;
bottom: calc(100% + 2px);
left: 50%;
transform: translateX(-50%);
border: 6px solid transparent;
border-top-color: #0f172a; /* Arrow pointing down */
opacity: 0;
visibility: hidden;
transition: opacity 0.2s ease;
}
[data-tooltip]:hover::before {
opacity: 1;
visibility: visible;
}
Step 3: Modern Positioning with CSS Anchor Positioning
Traditional absolute positioning can clip tooltips inside parent containers that have overflow: hidden. In modern browsers, you can bind tooltips to trigger buttons across DOM boundaries using CSS Anchor Positioning (anchor-name and position-anchor), eliminating overflow clipping completely.
.trigger-btn {
anchor-name: --my-tooltip;
}
.tooltip-bubble {
position: fixed;
position-anchor: --my-tooltip;
bottom: anchor(top);
justify-self: anchor-center;
margin-bottom: 8px;
}
Step 4: Accessibility: Keyboard Focus & aria-describedby
Mouse hover alone fails accessibility audits. Always trigger tooltips on :focus-visible so keyboard tab users receive identical context. In your HTML, link the trigger to the tooltip via aria-describedby="tooltip-id" and ensure the tooltip has role="tooltip".
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Why should you animate opacity and visibility instead of display: none on tooltips?
The CSS display property cannot be animated smoothly with transitions because it is binary. Animating opacity alongside visibility allows for silky 0.2s fades while visibility: hidden ensures the tooltip cannot accidentally intercept mouse clicks when faded out.
How do you make pure CSS tooltips work for keyboard users?
Add :focus-visible::after and :focus-visible::before selectors with identical rules to your :hover states. Ensure the trigger element is focusable (a <button>, <a>, or element with tabindex="0").
Can you put HTML markup or links inside a pure CSS tooltip?
No. The CSS content: attr(...) property only supports plain text strings. If your tooltip requires formatted HTML, images, or interactive links, use a dedicated HTML child element or the native HTML Popover API instead of a pseudo-element.
What happens if a CSS tooltip overflows the screen edge?
Traditional absolutely positioned tooltips can get cut off by screen boundaries or parent containers with overflow: hidden. To fix this, use CSS Anchor Positioning with position-try-fallbacks: flip-block, flip-inline to automatically flip the tooltip to the opposite side.
How does the native HTML Popover API compare to pure CSS tooltips?
The native HTML Popover API (popover="auto") renders floating dialogs into the browser's top layer, completely bypassing z-index stacking conflicts and overflow: hidden clippings. Pure CSS pseudo-element tooltips remain ideal for simple, zero-DOM text labels.