title attribute on any element to render a smooth, styled tooltip. The JavaScript automatically converts title attributes to data-tooltip for custom styling.
Basic Tooltips
Replaced Elements
Replaced elements like<img>, <iframe>, <video>, etc. need to be wrapped in a parent element with the title attribute:
Replaced elements are elements whose content is outside the scope of CSS styling (like
<img>, <video>, <iframe>, etc.). They require a wrapper element for tooltips to work properly.Features
Progressive Enhancement
Works without JavaScript - falls back to native browser tooltips
Automatic Conversion
JavaScript automatically converts
title to data-tooltip for custom stylingARIA Support
Automatically adds
aria-label if not present for screen reader supportSmooth Animations
Fade and slide transitions with 700ms delay on hover
How It Works
JavaScript Enhancement
The tooltip script runs on page load and continuously monitors the DOM:- Finds all elements with
titleattribute - Converts
titletodata-tooltip - Adds
aria-labelif not already present - Removes original
titleattribute to prevent browser tooltip - Watches for new elements and applies enhancement automatically
Progressive Enhancement
Without JavaScript:- Browser’s native tooltip displays
- Functionality works, but without custom styling
- Custom styled tooltip with smooth animations
- Better positioning and appearance
- Enhanced accessibility with
aria-label
Styling Details
Tooltip Box
Tooltip Box
- Content: comes from
data-tooltipattribute - Position: centered above element with 10px offset
- Padding:
var(--space-2)vertical,var(--space-3)horizontal - Font size:
var(--text-7) - Background:
var(--foreground) - Color:
var(--background)(inverted for contrast) - Border radius:
var(--radius-medium) - White-space: nowrap (single line)
- Z-index: 1000
Tooltip Arrow
Tooltip Arrow
- Triangle shape using borders
- Color: matches tooltip background (
var(--foreground)) - Size: 8px border width
- Position: centered below tooltip box
- Points downward toward element
Animations
Animations
- Initial state: opacity 0, translated up 4px
- Hover state: opacity 1, translated to normal position
- Delay: 700ms before showing
- Duration: smooth transition with
var(--transition-fast) - Applies to opacity, transform, and visibility
Positioning
Positioning
- Horizontal: centered using
left: 50%andtranslateX(-50%) - Vertical: above element with
bottom: calc(100% + 10px) - Arrow: 5px offset from element with
bottom: calc(100% - 5px) - Z-index: 1000 to appear above other content
Behavior
Shows tooltip after 700ms delay with fade-in animation
Shows tooltip for keyboard users when element receives focus
Hides tooltip immediately with fade-out animation
Accessibility
- Automatically adds
aria-labelfor screen readers - Works with keyboard focus (
:focus-visible) - Pointer events disabled on tooltip to prevent interference
- Progressive enhancement ensures functionality without JavaScript
- Semantic
titleattribute provides fallback
Dynamic Content
The tooltip script uses a MutationObserver to automatically handle:- New elements added to the DOM
- Changes to
titleattributes - Dynamically loaded content
title attribute and it works.
JavaScript API
The tooltip enhancement runs automatically. No API methods to call - just use thetitle attribute:
/home/daytona/workspace/source/src/js/tooltip.js:1-37
Limitations
For more complex tooltip positioning or multi-line content, consider using the dropdown component withpopover attribute.
CSS Variables
--space-2,--space-3- Padding values--text-7- Font size--foreground- Tooltip background color--background- Tooltip text color--radius-medium- Border radius--transition-fast- Animation duration