How to Create Informational Tooltips with Pure CSS

Recent Trends in CSS-Driven UI
Over the past several development cycles, the front-end community has increasingly turned to pure CSS solutions for micro-interactions that previously required JavaScript. Tooltips—those small overlay boxes that appear on hover or focus—are a prime example. Recent discussions on CSS-Tricks, developer forums, and social media highlight a growing preference for the ::before and ::after pseudo-elements combined with attribute selectors, such as data-tooltip, to build lightweight informational overlays. This approach aligns with performance-conscious design and accessibility-first thinking.

Background: From JS Libraries to Native CSS
Historically, tooltip behavior was handled by JavaScript plugins like Tippy.js, Bootstrap Tooltips, or jQuery UI. These libraries offered robust positioning and animation but required additional script loading and event handling. Over time, CSS evolved to support attr() in content properties, transition, opacity, and pointer-events—making it possible to create functional tooltips without a single line of JavaScript. The technique typically involves:

- Storing the tooltip text in a custom
data-*attribute on the target element. - Using
::afterto render the text viacontent: attr(data-tooltip). - Positioning the pseudo-element absolutely relative to the parent, with
opacity: 0by default andopacity: 1on:hoveror:focus. - Adding a small delay via
transition-delayto prevent flickering during casual mouse movement.
This pattern has been documented in many tutorials since at least the mid-2010s, but recent browser support for attr() on all elements (including images) and the widespread adoption of CSS custom properties have expanded its practical use cases.
User Concerns: Accessibility, Positioning, and Edge Cases
Despite the elegance of pure CSS tooltips, several concerns remain common among developers and content publishers:
- Keyboard accessibility: Pure CSS tooltips do not automatically appear on keyboard focus unless
:focusis explicitly added alongside:hover. Without this, users who navigate via Tab will miss the tooltip content entirely. - Content overflow and clipping: In tight layouts or responsive containers, tooltip text may overflow the viewport or get clipped by a parent with
overflow: hidden. Developers must manually adjustleft,right,top, orbottomvalues and consider usingtransformoffsets. - Text wrapping and length: The
data-tooltipattribute holds a single string; long messages may break layout. No built-in way exists to manage line breaks or apply rich formatting without additional markup. - Touch devices: On mobile or tablet, there is no persistent hover state. A touch interaction may trigger the tooltip momentarily, but the user cannot "hover away" easily. This often results in sticky tooltips unless JavaScript is used to manage tap events.
- Screen reader support: Content rendered via
::afteris not exposed to all screen readers consistently. Developers must often supplement witharia-labeloraria-describedbyto ensure the information is conveyed to assistive technology.
Likely Impact on Development Practices
The likely impact of this pure CSS trend is a shift toward simpler, more maintainable codebases for static or low-interaction tooltips. Teams that prioritize minimal dependencies may adopt CSS-only tooltips for documentation sites, dashboards, and data tables where JavaScript overhead is undesirable. However, for complex, position-critical, or highly interactive interfaces, the limitations described above will continue to drive developers back to hybrid solutions—using CSS for the visual layer and JavaScript for positioning logic, accessibility guarantees, and touch handling. Over the next few release cycles, we may see browser vendors introduce new CSS primitives (such as a native anchor() positioning function) that further reduce the need for manual coordinate tuning.
What to Watch Next
Keep an eye on the following developments to stay ahead of how this technique evolves:
- CSS Anchor Positioning API: The W3C draft for
position: anchorandanchor()functions promises to solve dynamic tooltip placement relative to arbitrary elements without JavaScript. Early browser implementations (Chrome 125+) are already appearing in developer trials. - Popover API and
popovertarget: A newer HTML/CSS feature that can show or hide elements via declarative attributes. Some developers are experimenting with usingpopoverto create tooltip-like overlays that respect the top layer and avoid clipping. - Focus-visible and reduced-motion integration: Future best practice will likely incorporate
:focus-visibleto separate mouse and keyboard focus styles, andprefers-reduced-motionto disable tooltip transitions for users sensitive to animation. - Tooltip as a design token: With the rise of design systems and CSS custom properties, tooltip styling (background, text color, border radius, offset) is being abstracted into reusable tokens, making the technique scalable across large projects.
The pure CSS informational tooltip is not a replacement for all use cases, but its simplicity and performance benefits make it a strong candidate for many static and content-rich environments. As browser capabilities continue to improve, the line between "pure CSS" and "CSS with a little JavaScript" may become increasingly blurred, but the underlying goal remains the same: deliver clear, accessible, and responsive information to the user with minimal friction.