Bootstrap 5 Tooltips: Placement, Initialization and Pitfalls

A tooltip is the small dark label that appears when you hover over or focus an element. Bootstrap’s tooltips are opt-in: unlike most components, adding the data attributes is not enough, you also have to initialize them with one line of JavaScript.

Page setup

Tooltips are positioned by Popper. bootstrap.bundle.min.js includes it; the smaller bootstrap.min.js does not, and tooltips will throw an error without it.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bootstrap tooltips</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <main class="container py-5">
    <!-- tooltip triggers go here -->
  </main>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    const tooltipTriggers = document.querySelectorAll('[data-bs-toggle="tooltip"]');
    [...tooltipTriggers].forEach(el => new bootstrap.Tooltip(el));
  </script>
</body>
</html>

The initialization script must come after the bundle, so that bootstrap exists when it runs.

Four placements

<ul class="list-inline">
  <li class="list-inline-item">
    <button type="button" class="btn btn-secondary"
            data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Tooltip on top">
      Tooltip on top
    </button>
  </li>
  <li class="list-inline-item">
    <button type="button" class="btn btn-secondary"
            data-bs-toggle="tooltip" data-bs-placement="right" data-bs-title="Tooltip on right">
      Tooltip on right
    </button>
  </li>
  <li class="list-inline-item">
    <button type="button" class="btn btn-secondary"
            data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Tooltip on bottom">
      Tooltip on bottom
    </button>
  </li>
  <li class="list-inline-item">
    <button type="button" class="btn btn-secondary"
            data-bs-toggle="tooltip" data-bs-placement="left" data-bs-title="Tooltip on left">
      Tooltip on left
    </button>
  </li>
</ul>

What changed from Bootstrap 3

The old example set up each placement with a separate jQuery call and a CSS class per direction. None of that is needed now. The placement comes from data-bs-placement, the text from data-bs-title (supported since 5.2; a plain title attribute also works), and one loop initializes every trigger on the page.

Other changes you will hit when converting old markup:

  • data-toggle and data-placement are now data-bs-toggle and data-bs-placement. The unprefixed names are silently ignored.
  • data-original-title was something Bootstrap 3 wrote internally. You never needed to set it yourself, and you can delete it.
  • Items in a .list-inline list need .list-inline-item on each <li> since Bootstrap 4. Without it they stack vertically.
  • jQuery is no longer required. If jQuery happens to be on the page, Bootstrap 5 still registers $(...).tooltip(), but there is no reason to write new code that way.

If there is not enough room on the requested side, Popper flips the tooltip to the opposite one. That is why a “top” tooltip on an element near the top of the window appears below it.

Elements added after page load

The loop above only finds triggers that exist when it runs. For content inserted later, for example rows loaded with fetch, initialize once on a container with the selector option instead. Bootstrap then creates the tooltip the first time a matching element is hovered or focused:

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

When you remove an element that has a tooltip, call bootstrap.Tooltip.getInstance(el)?.dispose() first. Otherwise a tooltip that is showing at that moment can stay stuck on the screen.

Accessibility and touch screens

Use tooltips on elements a keyboard can reach: buttons, links with an href, form controls. Bootstrap shows the tooltip on focus as well as hover, so keyboard users get the same hint, and it links the tooltip to its trigger with aria-describedby while it is visible.

Disabled buttons do not receive mouse or focus events, so a tooltip on one never appears. Wrap the button in a <span class="d-inline-block" tabindex="0"> and put the tooltip on the span.

Phones and tablets have no hover. A tap shows the tooltip, but many users will not think to tap. Do not put anything in a tooltip that people need in order to complete a task; if it matters, write it on the page.

Finally, tooltip text is treated as plain text by default. data-bs-html="true" allows markup, which Bootstrap runs through a sanitizer with an allowlist of tags. If the text comes from user input, leave HTML off.

Related examples

Leave a Comment