Accessible tooltips with smart positioning and keyboard support
Add helpful hints to any element with the tooltip component. Tooltips automatically appear on hover and focus, with full keyboard and screen reader support.
<button data-tooltip="This is a helpful hint">
Hover or focus me
</button>
<!-- In <head> -->
<link rel="stylesheet" href="styles/tooltip.css" />
<!-- Before </body> -->
<script src="javascript/tooltip.js"></script>
| Class | Description |
|---|---|
.rsl-tooltip |
Base tooltip container (automatically added by JavaScript) |
| Class | Description |
|---|---|
.rsl-tooltip-light |
Light background variant |
.rsl-tooltip-primary |
Primary brand color variant |
.rsl-tooltip-success |
Success/green variant |
.rsl-tooltip-warning |
Warning/yellow variant |
.rsl-tooltip-danger |
Danger/red variant |
| Class | Max Width | Font Size |
|---|---|---|
.rsl-tooltip-sm |
180px | 0.75rem |
.rsl-tooltip (default) |
250px | 0.875rem |
.rsl-tooltip-lg |
350px | 0.9375rem |
| Attribute | Values | Default | Description |
|---|---|---|---|
data-tooltip |
string | - | Required. The tooltip text content |
data-tooltip-position |
top, bottom, left, right |
top |
Tooltip position relative to trigger element |
data-tooltip-variant |
default, light, primary, success, warning, danger |
default |
Tooltip color variant |
data-tooltip-size |
sm, md, lg |
md |
Tooltip size |
<button
data-tooltip="Click to submit your form"
data-tooltip-position="right"
data-tooltip-variant="success"
data-tooltip-size="lg">
Submit
</button>
Tooltips are automatically initialized on page load for all elements with data-tooltip attribute.
| Method | Parameters | Description |
|---|---|---|
RSL.Tooltip.init() |
- | Initialize all tooltips on the page |
RSL.Tooltip.attach(element, options) |
element (HTMLElement|string) options (Object, optional) |
Attach tooltip to a specific element |
RSL.Tooltip.show(element) |
element (HTMLElement) | Show tooltip for an element |
RSL.Tooltip.hide(element) |
element (HTMLElement) | Hide tooltip for an element |
RSL.Tooltip.toggle(element) |
element (HTMLElement) | Toggle tooltip visibility |
RSL.Tooltip.destroy(element) |
element (HTMLElement) | Remove tooltip functionality from element |
| Option | Type | Default | Description |
|---|---|---|---|
position |
string | 'top' |
Tooltip position: 'top', 'bottom', 'left', 'right' |
variant |
string | 'default' |
Color variant |
size |
string | 'md' |
Size: 'sm', 'md', 'lg' |
showDelay |
number | 300 |
Delay in ms before showing tooltip |
hideDelay |
number | 200 |
Delay in ms before hiding tooltip |
offset |
number | 8 |
Distance in px from trigger element |
autoPosition |
boolean | true |
Auto-flip position if near viewport edge |
// Attach tooltip with custom options
RSL.Tooltip.attach('#myButton', {
position: 'right',
variant: 'primary',
showDelay: 500,
hideDelay: 100
});
// Programmatically show/hide
var button = document.querySelector('#myButton');
RSL.Tooltip.show(button);
setTimeout(function() {
RSL.Tooltip.hide(button);
}, 2000);
role="tooltip" and aria-describedby to create proper relationshipstabindex="0" to non-focusable elementsprefers-reduced-motion preferenceprefers-contrast: high mode| Key | Action |
|---|---|
| Tab | Focus trigger element and show tooltip |
| Escape | Hide tooltip and return focus to trigger |
| Shift + Tab | Move focus away and hide tooltip |
Tooltip component works in all modern browsers: