Bootstrap Popovers
Learn how to display additional information in interactive popup boxes using Bootstrap 5.
What is a Popover?
A popover is a small popup panel that displays additional information when a user interacts with an element. It can contain a title and a larger description than a typical tooltip.Examples: Student details, course information, product descriptions, help panels, and additional instructions.
Learning Objectives
- Understand how Bootstrap popovers work.
- Create a basic popover with a title and content.
- Change popover placement.
- Control how a popover is triggered.
- Create popovers with HTML content.
- Build a practical course information example.
1. How Does a Popover Work?
A popover is associated with a button or another interactive element. Bootstrap JavaScript reads the configured title and content, then displays a popup panel when the trigger occurs.
Working Diagram
User Interaction
The user clicks or focuses on the trigger element.
Bootstrap JavaScript
Bootstrap processes the popover configuration.
Popover Appears
A popup displays the title and additional content.
Popover Closes
The popover closes according to its trigger settings, such as another click or a supported dismissal action.
2. Creating a Basic Popover
Use data-bs-toggle="popover" to identify the
trigger, data-bs-title for the title, and
data-bs-content for the message.
Program Code
<button type="button"
class="btn btn-primary"
data-bs-toggle="popover"
data-bs-title="Welcome"
data-bs-content="Welcome to Bootstrap Popovers!">
Click to Learn More
</button>
<script>
document.querySelectorAll('[data-bs-toggle="popover"]')
.forEach(element => {
bootstrap.Popover.getOrCreateInstance(element);
});
</script>
Program Output
Click the button to open the popover. Click it again to close it.
3. Popover Placement
Use data-bs-placement to specify the preferred
position of a popover.
top— above the trigger.bottom— below the trigger.start— at the start side.end— at the end side.
Program Code
<button class="btn btn-primary"
data-bs-toggle="popover"
data-bs-placement="top"
data-bs-title="Top Popover"
data-bs-content="This popup appears above the button.">
Top
</button>
<button class="btn btn-success"
data-bs-toggle="popover"
data-bs-placement="bottom"
data-bs-title="Bottom Popover"
data-bs-content="This popup appears below the button.">
Bottom
</button>
Program Output
4. Dismissible Popover
A dismissible popover can be closed by clicking elsewhere or
using the appropriate dismissal behavior. Bootstrap's
data-bs-trigger="focus" configuration is useful
when the user should be able to dismiss the popover by moving
focus away from its trigger.
Program Code
<button type="button"
class="btn btn-warning"
data-bs-toggle="popover"
data-bs-trigger="focus"
data-bs-title="Helpful Information"
data-bs-content="Click elsewhere to dismiss this popover.">
Show Help
</button>
Program Output
Click the button, then click another focusable element to test dismissal.
5. Popover with HTML Content
Enable data-bs-html="true" to render HTML inside
a popover. Use only trusted, controlled HTML to avoid security risks.
Program Code
<button type="button"
class="btn btn-info"
data-bs-toggle="popover"
data-bs-html="true"
data-bs-title="Course Details"
data-bs-content="<strong>Duration:</strong> 6 Months<br>Includes live projects.">
View Course Details
</button>
Program Output
6. Practical Example: Course Information
A training institute website can use popovers to display short course details while keeping the page clean.
Program Code
<button type="button"
class="btn btn-outline-primary"
data-bs-toggle="popover"
data-bs-title="Java Full Stack"
data-bs-content="Learn Java, JDBC, Spring Boot, REST APIs, and databases.">
Java Full Stack
</button>
<button type="button"
class="btn btn-outline-success"
data-bs-toggle="popover"
data-bs-title="Python Full Stack"
data-bs-content="Learn Python, web development, APIs, and databases.">
Python Full Stack
</button>
<button type="button"
class="btn btn-outline-danger"
data-bs-toggle="popover"
data-bs-title=".NET Full Stack"
data-bs-content="Learn C#, ASP.NET Core, MVC, and Web APIs.">
.NET Full Stack
</button>
Program Output
Click a course button to view its description.
7. Important Attributes
| Attribute | Purpose |
|---|---|
data-bs-toggle="popover" |
Marks an element as a popover trigger. |
data-bs-title |
Specifies the popover title. |
data-bs-content |
Specifies the popover content. |
data-bs-placement |
Sets the preferred popup position. |
data-bs-trigger |
Controls which interaction opens or closes the popover. |
data-bs-html="true" |
Allows HTML in the content. |
bootstrap.Popover.getOrCreateInstance() |
Gets an existing popover instance or creates one. |
8. Difference Between Tooltip and Popover
| Tooltip | Popover |
|---|---|
| Usually displays a short message. | Can display a title and a longer message. |
| Commonly appears on hover or focus. | Commonly appears on click. |
| Useful for brief hints. | Useful for additional contextual information. |
Uses data-bs-toggle="tooltip". |
Uses data-bs-toggle="popover". |
9. Common Mistakes
- Forgetting to initialize popovers with Bootstrap JavaScript.
- Using Bootstrap 4's
data-toggleinstead of Bootstrap 5'sdata-bs-toggle. - Using
data-bs-titlewithout configuring the popover trigger. - Enabling HTML content with untrusted input.
- Using popovers for lengthy content that would be better displayed in a modal or on a separate page.
Summary
- Popovers display additional contextual information.
data-bs-titledefines the title.data-bs-contentdefines the message.data-bs-placementcontrols the preferred position.- Bootstrap JavaScript initialization is required.
- Use tooltips for brief hints and popovers for more detailed information.