Interactive demos of hcg-tooltip, a zero-dependency JavaScript tooltip
library and jQuery Tipsy alternative. Try gravity positioning, show/hide delays, HTML
content, focus and manual triggers, data attributes, and live delegation - hover and
focus the examples below.
hcg-tooltip Documentation
Default north gravity, reads text from the title attribute.
hcgTooltip('.tip-basic');
Eight positions: n, s, e, w, nw, ne, sw, se.
hcgTooltip('.tip-n', { gravity: 'n' });
hcgTooltip('.tip-s', { gravity: 's' });
hcgTooltip('.tip-e', { gravity: 'e' });
hcgTooltip('.tip-w', { gravity: 'w' });
hcgTooltip('.tip-nw', { gravity: 'nw' });
// ... ne, sw, se
offset sets the pixel gap between the target and the tooltip (default 0). All examples use gravity: 'n' so the gap is easy to see below each button.
hcgTooltip('.tip-offset-0', { gravity: 'n', offset: 0 });
hcgTooltip('.tip-offset-8', { gravity: 'n', offset: 8 });
hcgTooltip('.tip-offset-20', { gravity: 'n', offset: 20 });
hcgTooltip('.tip-offset-40', { gravity: 'n', offset: 40 });
Tooltips fade in via CSS on open. opacity sets the target level (default 0.8).
hcgTooltip('.tip-opacity-40', { opacity: 0.4 });
hcgTooltip('.tip-opacity-60', { opacity: 0.6 });
hcgTooltip('.tip-opacity-80', { opacity: 0.8 }); // default
hcgTooltip('.tip-opacity-100', { opacity: 1 });
className adds extra CSS classes to the tooltip element - string or (el) => string.
hcgTooltip('.tip-class-error', { className: 'hcg-tooltip-error' });
hcgTooltip('.tip-class-success', { className: 'hcg-tooltip-success' });
hcgTooltip('.tip-class-info', { className: 'hcg-tooltip-info' });
hcgTooltip('.tip-class-dynamic', {
className: (el) => el.dataset.tipClass
});
delayIn and delayOut in milliseconds.
hcgTooltip('.tip-delay', { delayIn: 500, delayOut: 1000 });
html: true renders markup from the title attribute. Compare with plain text when html: false (default).
hcgTooltip('.tip-html-off', { html: false }); // default
hcgTooltip('.tip-html-bold', { html: true });
hcgTooltip('.tip-html-break', { html: true });
hcgTooltip('.tip-html-color', { html: true });
hcgTooltip('.tip-html-list', { html: true });
trigger: 'focus' shows the tooltip on keyboard focus.
hcgTooltip('.tip-focus', { trigger: 'focus', gravity: 's' });
trigger: 'manual' - call show() / hide() yourself.
hcgTooltip('#tip-manual-target', { trigger: 'manual', gravity: 's' });
document.getElementById('tip-manual-show').onclick = () =>
hcgTooltip('#tip-manual-target', 'show');
document.getElementById('tip-manual-hide').onclick = () =>
hcgTooltip('#tip-manual-target', 'hide');
live: true works on elements added after init.
hcgTooltip('.tip-live', { live: true, gravity: 'n' });
document.getElementById('live-add').onclick = () => {
const btn = document.createElement('button');
btn.className = 'demo-btn tip-live';
btn.title = 'Added dynamically!';
btn.textContent = 'New';
document.getElementById('live-zone').appendChild(btn);
};
Read from another attribute or a callback function.
hcgTooltip('.tip-custom', { title: 'data-tip' });
hcgTooltip('.tip-callback', {
title: (el) => 'Computed: ' + el.textContent.trim()
});
Declare tooltips in HTML with data-hcg-tooltip - call hcgTooltip.initFromData() once, no per-element JS.
<button data-hcg-tooltip title="Hello!">Title attr</button>
<button data-hcg-tooltip="Tooltip below" data-hcg-tooltip-gravity="s">
Inline + gravity
</button>
<button data-hcg-tooltip="Delayed error tip"
data-hcg-tooltip-gravity="n"
data-hcg-tooltip-delay-in="400"
data-hcg-tooltip-class="hcg-tooltip-error">
Delayed themed
</button>
hcgTooltip.initFromData();
When title is empty, fallback text is shown.
hcgTooltip('.tip-fallback', { fallback: 'Default tooltip text' });