I'm currently focused on the core functionality. I'm prioritizing the user's need for an "advanced" tooltip, which means going beyond basic hover effects. Accessibility is also key, so I'm thinking deeply about ARIA attributes and how they will enhance usability. I’m now considering various positioning methods to accommodate dynamic content.
Analyzing CSS Implementation
I'm now wrestling with the constraints of "pure CSS" while striving for accessibility. The core problem is making the tooltip content screen reader-friendly, given that pseudo-elements aren't reliable. I'm exploring the best HTML structure for the tooltip and trigger relationship to ensure semantic correctness. Focusing on keyboard navigation is a priority, as is making the tooltip content dynamic.
Defining ARIA Relationships
I'm now determining how to connect the trigger element and tooltip content for full accessibility. While the data-attribute approach offers clean CSS, relying solely on ::before for content is insufficient. The best practice is leveraging aria-label on the trigger, which the screen reader will announce, and then connect to the tooltip with aria-describedby, ensuring a proper relationship. Both elements must have clear focus styles.
Finalizing the Data-Attribute Approach
I've refined the data-attribute approach. Now, the trigger will use aria-label to hold tooltip content, and the CSS content property renders this value. The tooltip, styled with ::after and ::before, is absolutely positioned. I’ll ensure focus states are clearly visible for keyboard navigation. I'll need to define custom properties for style control.
Refining Accessibility Strategy
I'm now revisiting accessibility. While the data-attribute approach is convenient, I'm now realizing that aria-label provides a better semantic foundation. Screen readers will directly announce the tooltip text. My focus is now on ensuring a fallback option, in case aria-label isn't optimal, particularly if the tooltip requires HTML. I will likely recommend the span sibling approach. I'll include thorough testing of both methods with a screen reader.
Addressing the "Pure CSS" Dilemma
I'm now grappling with the trade-offs between "pure CSS" and full accessibility. The original plan involving aria-label is not correct, because it overwrites the meaning of the original trigger element. I'm exploring an approach using a separate DOM element with a unique ID, referenced by aria-describedby to enhance semantics, but this may mean a sacrifice to the purity constraint. I need to make the right choice that still lets the system be as CSS-only as possible.
Structuring the "Pure CSS" Tooltip
I'm now iterating on the final code structure. Because of the "CSS-only" constraint, the best choice is to use data-tooltip for the content and manage accessibility with aria-describedby when necessary. I will highlight the limitations and design choices in the "Usage Guide." I'm also planning the scss-like structure, which will mean leveraging [data-flow] for direction and, if possible, CSS transforms.
Here is a complete, advanced CSS-only tooltip system.
Key Features:
Zero JavaScript: Logic relies entirely on CSS pseudo-elements and properties.
Fully Accessible: Uses :focus states for keyboard navigation and aria-label mapping.
Directional Control: Supports Top, Bottom, Left, and Right positioning.
Smart Animations: Includes prefers-reduced-motion support.
1. The HTML Structure
To make this accessible, we use data-tooltip for the content (visual) and aria-label (assistive tech). If the element is not natively interactive (like a span), we must add tabindex="0" so keyboard users can tab to it.
html
<div class="tooltip-container">
<!-- 1. Top Tooltip (Default) -->
<button
class="btn"
data-tooltip="Save changes to database"
data-flow="top"
aria-label="Save changes to database">
Hover Me (Top)
</button>
<!-- 2. Right Tooltip -->
<a href="#"
data-tooltip="Visit our documentation"
data-flow="right"
aria-label="Visit our documentation">
Hover Me (Right)
</a>
<!-- 3. Bottom Tooltip on non-interactive element -->
<span
class="icon"
tabindex="0"
data-tooltip="Account Settings"
data-flow="bottom"
aria-label="Account Settings">
⚙️
</span>
<!-- 4. Left Tooltip with specialized color -->
<button
class="btn warning"
data-tooltip="Danger Zone!"
data-flow="left"
aria-label="Danger Zone"
style="--tooltip-bg: #e74c3c;">
Hover Me (Left)
</button>
</div>
2. The CSS System
Copy this into your stylesheet. It uses CSS Custom Properties for easy configuration.
css
3. Explanation of Accessibility Features
aria-label: Since CSS generates the visual tooltip using data-tooltip, Screen Readers won't inherently read it. By duplicating the text in aria-label, we ensure Screen Readers announce the tip when focused.
Note: If the text in the tooltip is purely supplementary and not the name of the element, use aria-describedby with a real ID instead—though that requires JavaScript or static HTML elements rather than pure CSS injection.
:focus-visible: The tooltip appears not just on hover, but when the element receives keyboard focus (Tab key).
tabindex="0": Applied to non-interactive elements (like div or span) to ensure they can actually receive keyboard focus.
prefers-reduced-motion: The CSS detects if the OS has animations turned off (for accessibility/vestibular disorders) and disables the "swooping" motion of the tooltip, using a simple appear/disappear instead.
4. Limitations of CSS-Only Tooltips
While this system is powerful, pure CSS tooltips have one major limitation: Clipping.
If the trigger element is inside a container with overflow: hidden, the tooltip will be cut off. To solve this, you would need JavaScript (like Popper.js or Floating UI) to physically move the tooltip to the end of the <body> tag.