TExcelTAD
Home › Bootstrap › BS5 Tooltip

Bootstrap 5 Tooltip

A tooltip is a small dark label that appears when the user hovers over or focuses an element — "Copy to clipboard", "Delete", "Last edited 2 days ago". Bootstrap's tooltips are positioned by Popper (included in bootstrap.bundle.min.js).

Tooltips are opt-in

Unlike most components, tooltips don't work from data attributes alone. For performance reasons you must initialise them with JavaScript. The usual pattern is: mark elements with data-bs-toggle="tooltip", then create a bootstrap.Tooltip for each one in a script placed after the bundle.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-5 text-center">
  <button type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-title="Copy the link to your clipboard">
    📋 Copy
  </button>
  <p class="mt-4">Hover over <a href="#" data-bs-toggle="tooltip" data-bs-title="I'm a link tooltip">this link</a> too.</p>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    document.querySelectorAll('[data-bs-toggle="tooltip"]')
      .forEach((el) => new bootstrap.Tooltip(el));
  </script>
</body>
</html>

The text comes from data-bs-title (or the regular title attribute, which Bootstrap moves into the tooltip so the browser's own tooltip doesn't show as well).

Placement

Use data-bs-placement with top (default), bottom, left or right. If there isn't room, Popper flips the tooltip to the opposite side automatically.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-5">
  <div class="d-flex flex-wrap gap-2 justify-content-center">
    <button class="btn btn-outline-primary" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="On top">Top</button>
    <button class="btn btn-outline-primary" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-title="On the right">Right</button>
    <button class="btn btn-outline-primary" data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="Below">Bottom</button>
    <button class="btn btn-outline-primary" data-bs-toggle="tooltip" data-bs-placement="left" data-bs-title="On the left">Left</button>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach((el) => new bootstrap.Tooltip(el));
  </script>
</body>
</html>

Options

Options can be set as data-bs-* attributes or passed as an object to the constructor.

Option Default Meaning
title '' Tooltip text (or a function returning it)
placement 'top' top, bottom, left, right, auto
trigger 'hover focus' Any of click, hover, focus, manual
html false Allow HTML in the title (sanitised)
delay 0 ms, or { show: 500, hide: 100 }
customClass '' Extra class(es) for the tooltip element
container false Append the tooltip to e.g. 'body'

HTML content, custom style and the API

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <style>
    .purple-tip { --bs-tooltip-bg: #7952b3; --bs-tooltip-color: #fff; }
  </style>
</head>
<body class="p-5 text-center">
  <button class="btn btn-dark" id="rich">HTML tooltip</button>
  <button class="btn btn-primary" id="click">Click me</button>
  <button class="btn btn-link" id="toggle">Toggle the first from JS</button>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    const rich = new bootstrap.Tooltip(document.getElementById('rich'), {
      title: '<strong>Bold</strong> and <em>italic</em> 🎉',
      html: true,
      customClass: 'purple-tip',
      delay: { show: 300, hide: 100 },
    });
    new bootstrap.Tooltip(document.getElementById('click'), { title: 'Shown on click', trigger: 'click', placement: 'bottom' });
    document.getElementById('toggle').addEventListener('click', () => rich.toggle());
    document.getElementById('rich').addEventListener('shown.bs.tooltip', () => console.log('tooltip shown'));
  </script>
</body>
</html>

Each tooltip instance has show(), hide(), toggle(), enable(), disable(), setContent() and dispose(). Use bootstrap.Tooltip.getInstance(el) to get an existing one.

Tooltips on disabled elements

A disabled button doesn't fire mouse or focus events, so its tooltip never shows. Wrap it in a focusable <span tabindex="0"> and put the tooltip on the wrapper.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-5 text-center">
  <span class="d-inline-block" tabindex="0" data-bs-toggle="tooltip" data-bs-title="Fill in all fields first">
    <button class="btn btn-success" type="button" disabled>Submit</button>
  </span>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach((el) => new bootstrap.Tooltip(el));
  </script>
</body>
</html>

Tip: Tooltips are for short, non-essential hints. Touch-screen users can't hover, and keyboard users only see them on focus — never hide important information in a tooltip.

Test yourself

Why does a tooltip with only data-bs-toggle="tooltip" not appear?

  1. Tooltips need jQuery
  2. Tooltips are opt-in and must be initialised with JavaScript
  3. You must add .tooltip to the element
  4. The title must be in data-bs-content

Which attribute sets the side the tooltip appears on?

  1. data-bs-side
  2. data-bs-placement
  3. data-bs-position
  4. data-bs-align

How can you show a tooltip for a disabled button?

  1. Use trigger: 'disabled'
  2. Wrap the button in a focusable span and attach the tooltip to the span
  3. Add .tooltip-disabled
  4. It's impossible