Problem Statement
Build a tooltip component that:
- Appears on hover over a trigger element
- Has a smooth fade-in transition
- Includes an arrow/pointer pointing to the trigger
- Can be positioned above, below, left, or right of the trigger
- Uses only HTML and CSS — no JavaScript allowed
Requirements
- Must work with just
:hoverand CSS pseudo-elements - Arrow should be created with CSS (border trick or
clip-path) - Tooltip text should be configurable via a
data-tooltipattribute - Smooth transition (opacity + transform) on show/hide
- Should not overflow the viewport (bonus: handle edge cases)
Example HTML
<span class="tooltip" data-tooltip="This is helpful info">
Hover me
</span>
Expected CSS Approach
Use:
::afteror::beforepseudo-element for the tooltip textcontent: attr(data-tooltip)to pull text from the attribute:hoverto toggle visibilityposition: absoluterelative to the trigger- Border trick for the arrow
Follow-ups
- How would you handle tooltips near the edge of the viewport without JavaScript?
- What accessibility concerns exist with CSS-only tooltips? (hint: keyboard focus, screen readers)
- How would you support multi-line tooltip content?
- What's the performance difference between
visibility: hiddenvsdisplay: nonefor the hidden state?