Bootstrap ยท Chapter 27 of 43

Bootstrap Popover

Popovers are similar to tooltips but can show more content, including a title and a longer body of text, appearing in a small box when the user clicks or hovers over an element.

Like tooltips, popovers must be enabled manually with JavaScript. Use data-bs-toggle="popover", a title attribute for the heading, and data-bs-content for the body text.

Syntax
<button data-bs-toggle="popover" title="Title" data-bs-content="Body text">Click me</button>

Enabling popovers

Select all popover-triggering elements and call new bootstrap.Popover(el) on each one, similar to how tooltips are activated.

Popover content and dismissal

The title attribute sets the popover's heading and data-bs-content sets its body text. Add data-bs-trigger="focus" to dismiss the popover when the user clicks elsewhere.

Example 1 (html)
<button type="button" class="btn btn-primary" data-bs-toggle="popover" title="Popover Title" data-bs-content="Here is some more detailed popover content.">
  Click for info
</button>
<script>
  const pops = document.querySelectorAll('[data-bs-toggle="popover"]');
  pops.forEach(el => new bootstrap.Popover(el));
</script>
Output
Clicking the button shows a small box with a bold title and extra descriptive text

The Popover plugin reads the title and data-bs-content attributes to build the popup content.

Example 2 (html)
<a href="#" data-bs-toggle="popover" data-bs-trigger="focus" title="Info" data-bs-content="Click elsewhere to close this.">Focus me</a>
Output
A link that shows a popover on focus/click, and closes when the user clicks away

data-bs-trigger="focus" makes the popover dismiss automatically when focus leaves the element.

Key points

  • Popovers show a title and longer body content, unlike simple tooltips.
  • Popovers must be initialized with JavaScript, like tooltips.
  • data-bs-content sets the popover's body text.
  • data-bs-trigger="focus" allows dismissing the popover by clicking elsewhere.
๐Ÿ’ก Note: Use popovers sparingly โ€” too many can overwhelm users with extra popups.

๐Ÿ“ Quick Quiz

1. What attribute sets a popover's body text?

2. How do popovers differ from tooltips?

3. Which trigger value dismisses a popover when focus is lost?