Menu

Tooltip

A tooltip is a short text label that appears when the user hovers over or focuses an element. It explains a control that isn't clear on its own.

Default markup

Add data-bs-toggle="tooltip" and a title to the element. data-bs-placement puts the tooltip at the top, bottom, left or right.

<div class="btn-list">
  <button type="button" class="btn" data-bs-toggle="tooltip" data-bs-placement="top" title="Tooltip on top">Tooltip on top</button>
  <button type="button" class="btn" data-bs-toggle="tooltip" data-bs-placement="right" title="Tooltip on right">Tooltip on right</button>
  <button type="button" class="btn" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Tooltip on bottom">Tooltip on bottom</button>
  <button type="button" class="btn" data-bs-toggle="tooltip" data-bs-placement="left" title="Tooltip on left">Tooltip on left</button>
</div>

Tooltip with HTML

Set data-bs-html="true" to allow HTML in the title, for example to emphasize one word.

<button type="button" class="btn" data-bs-toggle="tooltip" data-bs-html="true" title="<em>Tooltip</em> <u>with</u> <b>HTML</b>">Tooltip with HTML</button>

JavaScript

Tabler automatically initializes all elements with data-bs-toggle="tooltip" on page load. This is the code that runs:

// `data-bs-delay` on the element, or `delay` in `data-bs-config`, wins over the shorter default delay.
registerPlugin(Tooltip, {
  selector: '[data-bs-toggle="tooltip"], [data-tblr-toggle="tooltip"]',
  when: 'ready',
  config: (element) => ({
    delay: dataOption(element, 'delay') ?? { show: 50, hide: 50 },
    placement: dataOption(element, 'placement') ?? 'auto',
  }),
})

To use your own options, create the instance yourself. It replaces the one Tabler created, so only your options apply:

const el = document.getElementById('my-tooltip');
new tabler.Tooltip(el, { customClass: 'tooltip-wide', delay: 300 });

To read or control the existing instance without changing its options, use tabler.Tooltip.getInstance(el) or tabler.Tooltip.getOrCreateInstance(el).

Promise return values

show(), hide() and toggle() return a Promise that resolves after the transition ends, right after shown.bs.tooltip or hidden.bs.tooltip fires. Use it to run code after the tooltip has finished opening or closing:

const tooltip = tabler.Tooltip.getOrCreateInstance(el);
await tooltip.show();
console.log('Tooltip is visible');

The Promise never rejects. It also resolves, without doing anything, when the call has no effect. show() does nothing when the tooltip has no content, is disabled, or a listener called preventDefault() on show.bs.tooltip; on a tooltip that is already shown it rebuilds the tip. hide() does nothing when the tooltip is already hidden or hide.bs.tooltip was prevented. toggle() does nothing when the tooltip is disabled. Otherwise it waits for the delay option first. A call in the other direction during that wait - the pointer leaves or focus moves - replaces the pending one, and the earlier Promise resolves without the tooltip being shown or hidden. A repeated toggle() while the tooltip waits to open resolves at once and leaves the pending one running. While the tooltip waits to close, a repeated toggle() restarts the delay: the earlier Promise resolves at once and the new one waits until the tooltip is hidden. So await tooltip.toggle() does not guarantee that the tooltip is visible. To act only when the tooltip has changed state, listen for shown.bs.tooltip or hidden.bs.tooltip instead.

Accessibility

  • A tooltip only describes; it never replaces a name. The trigger still needs its own text or aria-label.
  • The trigger must be focusable. Bootstrap shows the tooltip on focus as well as on hover, so a span or an a without href leaves keyboard users with nothing.
  • Never put a link, a button or a form field inside a tooltip. It disappears on blur, so nothing in it can be reached. Use a popover for that.
  • Tabler enables HTML in a tooltip only when you set data-bs-html="true". Leave it off for text that comes from users.
  • Do not attach a tooltip to a disabled control - a disabled element fires no events. Wrap it in a focusable span instead.

Class reference

Every class this component ships, grouped by what it changes. A name in braces stands for a family — {color} is any base color, {breakpoint} any responsive step.

tooltip componentContainer element, positioned by Bootstrap
tooltip-inner partThe bubble holding the text
tooltip-arrow partPointer towards the trigger, positioned by Bootstrap

CSS

Variables

The component uses local CSS variables for real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

On .tooltip. Source: core/scss/bootstrap/_tooltip.scss.

--tooltip-zindex: #{$zindex-tooltip};
--tooltip-max-width: #{$tooltip-max-width};
--tooltip-padding-x: #{$tooltip-padding-x};
--tooltip-padding-y: #{$tooltip-padding-y};
--tooltip-margin: #{$tooltip-margin};
@include rfs($tooltip-font-size, --tooltip-font-size);
--tooltip-color: #{$tooltip-color};
--tooltip-bg: #{$tooltip-bg};
--tooltip-border-radius: #{$tooltip-border-radius};
--tooltip-opacity: #{$tooltip-opacity};
--tooltip-arrow-width: #{$tooltip-arrow-width};
--tooltip-arrow-height: #{$tooltip-arrow-height};

Sass variables

Override these variables before importing Tabler to change the defaults.

Source: core/scss/_variables.scss.

$tooltip-font-size: $font-size-sm;
$tooltip-max-width: 200px;
$tooltip-color: var(--text-inverted);
$tooltip-bg: var(--bg-surface-inverted);
$tooltip-border-radius: var(--border-radius);
$tooltip-opacity: 0.9;
$tooltip-padding-y: var(--spacer-1);
$tooltip-padding-x: var(--spacer-2);
$tooltip-margin: null; // TODO: remove this in v6

$tooltip-arrow-width: 0.8rem;
$tooltip-arrow-height: 0.4rem;