Tooltips

Source: https://getbootstrap.com/docs/6.0/components/tooltip

Custom replacement for browser tooltips—themed with CSS variables and powered by Floating UI.

Overview

Hover the button below to see a tooltip. By default, the tooltip will appear above their trigger element (anything with the data-bs-toggle="tooltip" attribute).

<Example addStackblitzJs class="tooltip-demo" code={&lt;button type="button" class="btn-solid theme-primary" data-bs-toggle="tooltip" data-bs-title="Tooltip on top"> Example tooltip &lt;/button>} />

Things to know when using the tooltip plugin:

  • Tooltips rely on the third party library Floating UI for positioning. Use bootstrap.bundle.min.js which includes Floating UI, or include Floating UI separately via an import map.
  • Tooltips are automatically initialized when using data-bs-toggle="tooltip".
  • Tooltips with zero-length titles are never displayed.
  • Specify container: 'body' to avoid rendering problems in more complex components (like our input groups, button groups, etc).
  • Triggering tooltips on hidden elements will not work.
  • Tooltips for .disabled or disabled elements must be triggered on a wrapper element.
  • When triggered from hyperlinks that span multiple lines, tooltips will be centered. Use white-space: nowrap; on your <a>s to avoid this behavior.
  • Tooltips must be hidden before their corresponding elements have been removed from the DOM.
  • Tooltips can be triggered thanks to an element inside a shadow DOM.

Got all that? Great, let’s see how they work with some examples.

Examples

Hover over the links below to see tooltips:

<Example addStackblitzJs class="tooltip-demo" code={<p class="fg-3">Placeholder text to demonstrate some <a href="#" data-bs-toggle="tooltip" data-bs-title="Default tooltip">inline links</a> with tooltips. This is now just filler, no killer. Content placed here just to mimic the presence of <a href="#" data-bs-toggle="tooltip" data-bs-title="Another tooltip">real text</a>. And all that just to give you an idea of how tooltips would look when used in real-world situations. So hopefully you’ve now seen how <a href="#" data-bs-toggle="tooltip" data-bs-title="Another one here too">these tooltips on links</a> can work in practice, once you use them on <a href="#" data-bs-toggle="tooltip" data-bs-title="The last tip!">your own</a> site or project.</p>} />

Custom tooltips

You can customize the appearance of tooltips using CSS variables. We set a custom class with data-bs-custom-class="custom-tooltip" to scope our custom appearance and use it to override a local CSS variable.

<Example addStackblitzJs class="tooltip-demo" code={&lt;button type="button" class="btn-solid theme-secondary" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-custom-class="custom-tooltip" data-bs-title="This top tooltip is themed via CSS variables."> Custom tooltip &lt;/button>} />

Placement

Set data-bs-placement to change the direction. Hover over the buttons below to see the four tooltips directions: top, right, bottom, and left. Directions are mirrored when using Bootstrap in RTL.

<Example class="tooltip-demo" code={&lt;button type="button" class="btn-solid theme-secondary" data-bs-toggle="tooltip" data-bs-placement="left" data-bs-title="Tooltip on left"> Tooltip on left &lt;/button> &lt;button type="button" class="btn-solid theme-secondary" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Tooltip on top"> Tooltip on top &lt;/button> &lt;button type="button" class="btn-solid theme-secondary" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Tooltip on bottom"> Tooltip on bottom &lt;/button> &lt;button type="button" class="btn-solid theme-secondary" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-title="Tooltip on right"> Tooltip on right &lt;/button>} />

Responsive placement

Change placement at different breakpoints using responsive prefixes. The syntax is breakpoint:placement, where breakpoint is one of sm, md, lg, xl, or 2xl.

<Example class="tooltip-demo" code={&lt;button type="button" class="btn-solid theme-secondary" data-bs-toggle="tooltip" data-bs-placement="top md:right lg:bottom" data-bs-title="Responsive tooltip"> Top → md:right → lg:bottom &lt;/button> &lt;button type="button" class="btn-solid theme-secondary" data-bs-toggle="tooltip" data-bs-placement="left lg:top" data-bs-title="Responsive tooltip"> Left → lg:top &lt;/button>} />

Resize your browser to see the tooltip placement change at different breakpoints.

Custom HTML

And with custom HTML added:

<Example class="tooltip-demo" code={&lt;button type="button" class="btn-solid theme-secondary" data-bs-toggle="tooltip" data-bs-html="true" data-bs-title="&lt;em>Tooltip&lt;/em> &lt;u>with&lt;/u> &lt;b>HTML&lt;/b>"> Tooltip with HTML &lt;/button>} />

With an SVG:

<Example class="tooltip-demo" code={&lt;a href="#" class="d-inline-block" data-bs-toggle="tooltip" data-bs-title="Default tooltip" aria-label="Hover or focus to see default tooltip"> &lt;svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 100 100" aria-hidden="true"> &lt;rect width="100%" height="100%" fill="#563d7c"/> &lt;circle cx="50" cy="50" r="30" fill="#007bff"/> &lt;/svg> &lt;/a>} />

Interactive tooltips

Move the pointer onto the tip to reach interactive content inside it, such as a link. The tooltip stays open while the pointer is over the trigger or the tip. It hides when the pointer leaves both, or when you press the <kbd>Escape</kbd> key. This helps satisfy the WCAG 1.4.13 “Content on Hover or Focus” success criterion.

<Example class="tooltip-demo" addStackblitzJs code={&lt;button type="button" class="btn-solid theme-primary" data-bs-toggle="tooltip" data-bs-html="true" data-bs-title="Read the &lt;a href='https://getbootstrap.com/'>full docs&lt;/a> for more."> Hover, then move onto the tip &lt;/button>} />

Disabled elements

Elements with the disabled attribute aren’t interactive, meaning users cannot focus, hover, or click them to trigger a tooltip (or popover). As a workaround, you’ll want to trigger the tooltip from a wrapper <div> or <span>, ideally made keyboard-focusable using tabindex="0".

<Example class="tooltip-demo" addStackblitzJs code={&lt;span class="d-inline-block" tabindex="0" data-bs-toggle="tooltip" data-bs-title="Disabled tooltip"> &lt;button class="btn-solid theme-primary" type="button" disabled>Disabled button&lt;/button> &lt;/span>} />

CSS

Variables

Usage

The tooltip plugin generates content and markup on demand, and by default places tooltips after their trigger element. Trigger the tooltip via JavaScript:

const exampleEl = document.getElementById('example')
const tooltip = new bootstrap.Tooltip(exampleEl, options)

Tooltips automatically attempt to change positions when a parent container has overflow: auto or overflow: scroll, but still keep the original placement’s positioning. Set the boundary option (for the shift middleware using the floatingConfig option) to any HTMLElement to override the default value, 'clippingParents', such as document.body:

const tooltip = new bootstrap.Tooltip('#example', {
  boundary: document.body // or document.querySelector('#boundary')
})

Delegated tooltips

To enable tooltips on many triggers at once—including elements added to the DOM later—initialize a single instance on a parent element and pass the selector option. Any matching descendant gets a tooltip without re-initializing:

const tooltip = new bootstrap.Tooltip(document.body, {
  selector: '[data-bs-toggle="tooltip"]'
})

Via data attributes

Attribute Description
data-bs-toggle="tooltip" Initializes the tooltip on the trigger element.
data-bs-title Text (or HTML when allowed) shown in the tooltip body.
data-bs-placement Where to place the tooltip (top, right, bottom, left, or responsive variants).
data-bs-custom-class Extra class names added to the tooltip for theming.
data-bs-html When true, allows HTML in the title (use with care; prefer plain text for user content).

Markup

The required markup for a tooltip is only a data attribute and title on the HTML element you wish to have a tooltip. The generated markup of a tooltip is rather simple, though it does require a position (by default, set to top by the plugin).

Keep tooltips accessible to keyboard and assistive technology users by only adding them to HTML elements that are traditionally keyboard-focusable and interactive (such as links or form controls). While other HTML elements can be made focusable by adding tabindex="0", this can create annoying and confusing tab stops on non-interactive elements for keyboard users, and most assistive technologies currently do not announce tooltips in this situation. Additionally, do not rely solely on hover as the trigger for your tooltips as this will make them impossible to trigger for keyboard users.

A shown tooltip can be dismissed by pressing the <kbd>Escape</kbd> key, helping satisfy the WCAG 1.4.13 “Content on Hover or Focus” success criterion. The tooltip also stays open while the pointer moves onto the tip, so users can reach interactive content inside it. As with dropdown menus, a tooltip shown inside a dialog is dismissed on its own: the first <kbd>Escape</kbd> closes the tooltip and a subsequent one closes the dialog.

<!-- HTML to write -->
<a href="#" data-bs-toggle="tooltip" data-bs-title="Some tooltip text!">Hover over me</a>

<!-- Generated markup by the plugin -->
<div class="tooltip bs-tooltip-auto" role="tooltip">
  <div class="tooltip-arrow"></div>
  <div class="tooltip-inner">
    Some tooltip text!
  </div>
</div>