TExcelTAD
Home › Bootstrap › BS5 Popover

Bootstrap 5 Popover

A popover is like a bigger tooltip: a floating box with an optional title and a body, usually opened by a click. Use it for short explanations, previews or a couple of quick actions. Popovers are positioned by Popper, so you need bootstrap.bundle.min.js.

Popovers are opt-in too

Just like tooltips, popovers must be initialised in JavaScript. Mark the elements with data-bs-toggle="popover" and loop over them after the bundle has loaded.

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-primary" data-bs-toggle="popover"
          data-bs-title="What is a gutter?"
          data-bs-content="The space between grid columns. Change it with g-0 … g-5 on the row.">
    Click for help
  </button>

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

Click the button again to close it. data-bs-title fills the header; data-bs-content fills the body. If the title is empty, the header is left out.

Placement

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 pt-4">
    <button class="btn btn-secondary" data-bs-toggle="popover" data-bs-placement="top" data-bs-content="Popover on top">Top</button>
    <button class="btn btn-secondary" data-bs-toggle="popover" data-bs-placement="right" data-bs-content="Popover on the right">Right</button>
    <button class="btn btn-secondary" data-bs-toggle="popover" data-bs-placement="bottom" data-bs-content="Popover below">Bottom</button>
    <button class="btn btn-secondary" data-bs-toggle="popover" data-bs-placement="left" data-bs-content="Popover 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="popover"]').forEach((el) => new bootstrap.Popover(el));
  </script>
</body>
</html>

Note: If the frame is too short for a popover, Popper flips it to the side with more room. Drag the frame's bottom-right corner down if a popover looks cramped.

Dismiss on next click

By default a popover stays open until you click its trigger again. With data-bs-trigger="focus" it closes when the user clicks anywhere else. For this to work reliably across browsers, use an <a> with tabindex="0" (or a <button>).

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">
  <a tabindex="0" class="btn btn-danger" role="button" data-bs-toggle="popover" data-bs-trigger="focus"
     data-bs-title="Dismissible popover" data-bs-content="Click anywhere else to close me.">
    Dismissible
  </a>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    document.querySelectorAll('[data-bs-toggle="popover"]').forEach((el) => new bootstrap.Popover(el));
  </script>
</body>
</html>

HTML content, custom classes and events

With html: true the content may contain HTML (Bootstrap sanitises it, removing scripts and unknown attributes). customClass adds your own class so you can restyle it with CSS variables.

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>
    .brand-pop { --bs-popover-header-bg: #7952b3; --bs-popover-header-color: #fff; --bs-popover-max-width: 220px; }
  </style>
</head>
<body class="p-5 text-center">
  <button class="btn btn-outline-dark" id="user">👤 Ana Silva</button>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    const btn = document.getElementById('user');
    new bootstrap.Popover(btn, {
      title: 'Ana Silva',
      content: '<p class="mb-1">Front-end developer 🇵🇹</p><span class="badge text-bg-success">online</span>',
      html: true,
      placement: 'bottom',
      customClass: 'brand-pop',
    });
    btn.addEventListener('shown.bs.popover', () => console.log('popover shown'));
    btn.addEventListener('hidden.bs.popover', () => console.log('popover hidden'));
  </script>
</body>
</html>

Tooltip vs popover

Tooltip Popover
Content Short text only Title + body, can hold HTML
Default trigger hover focus click
Init new bootstrap.Tooltip(el) new bootstrap.Popover(el)
Text attribute data-bs-title data-bs-title + data-bs-content
Events show.bs.tooltip, shown.bs.tooltip, … show.bs.popover, shown.bs.popover, …

Tip: Popovers share all of the tooltip options (placement, trigger, delay, container, customClass, …) and API methods (show, hide, toggle, dispose).

Test yourself

Which attribute holds a popover's body text?

  1. data-bs-body
  2. data-bs-content
  3. data-bs-text
  4. title

What is the default trigger for a popover?

  1. hover
  2. focus
  3. click
  4. manual

How do you make a popover close when the user clicks elsewhere?

  1. data-bs-trigger="focus"
  2. data-bs-dismiss="popover"
  3. data-bs-autoclose="true"
  4. trigger: 'outside'