Bootstrap Tooltips
Learn how to display helpful information when users hover over, focus on, or interact with an element.
What is a Tooltip?
A tooltip is a small popup message that explains an element. It usually appears when the user hovers over or focuses on a button, link, or other interactive element.Examples: Help messages, button descriptions, form instructions, and information icons.
Learning Objectives
- Understand how Bootstrap tooltips work.
- Create a basic tooltip.
- Display tooltips on different sides of an element.
- Configure tooltip placement and HTML content.
- Initialize tooltips using JavaScript.
- Build a practical form-help example.
1. How Does a Tooltip Work?
Bootstrap uses JavaScript to activate tooltip behavior.
The tooltip text is commonly provided through the
data-bs-title attribute.
Working Diagram
Interactive Element
User hovers over or focuses on a button.
Bootstrap JavaScript
Bootstrap detects the interaction and reads the tooltip text.
Tooltip Appears
A small descriptive message appears near the element.
Tooltip Disappears
The message hides when the triggering interaction ends, according to the tooltip's configured triggers.
2. Creating a Basic Tooltip
Add data-bs-toggle="tooltip" to the element and
provide the message using data-bs-title.
Bootstrap 5 tooltips must also be initialized using JavaScript.
Program Code
<button type="button"
class="btn btn-primary"
data-bs-toggle="tooltip"
data-bs-title="Click to save your information">
Save
</button>
<script>
const tooltipTriggerList =
document.querySelectorAll('[data-bs-toggle="tooltip"]');
[...tooltipTriggerList].forEach(element => {
new bootstrap.Tooltip(element);
});
</script>
Program Output
Hover over the button or focus it using the keyboard to view the tooltip.
3. Tooltip Placement
The data-bs-placement attribute controls where
the tooltip appears relative to its element.
top— above the element.bottom— below the element.start— at the start side.end— at the end side.
Program Code
<button class="btn btn-primary"
data-bs-toggle="tooltip"
data-bs-placement="top"
data-bs-title="Tooltip above">
Top
</button>
<button class="btn btn-success"
data-bs-toggle="tooltip"
data-bs-placement="bottom"
data-bs-title="Tooltip below">
Bottom
</button>
<button class="btn btn-warning"
data-bs-toggle="tooltip"
data-bs-placement="start"
data-bs-title="Tooltip at start">
Start
</button>
<button class="btn btn-danger"
data-bs-toggle="tooltip"
data-bs-placement="end"
data-bs-title="Tooltip at end">
End
</button>
Program Output
Hover over each button to test its placement. Bootstrap may adjust placement if there is not enough space in the viewport.
4. Tooltip on Links
Tooltips can explain the purpose of a link before the user opens it. The tooltip is supplementary information and should not replace a clear link label.
Program Code
<a href="#course-details"
data-bs-toggle="tooltip"
data-bs-title="View the course syllabus">
Course Syllabus
</a>
Program Output
Hover over the link to view its tooltip.
5. Tooltip on Form Controls
A tooltip can provide a short hint about a form field. For important instructions, keep visible help text as well, because hover-only information may not be available on every device.
Program Code
<label for="studentEmail" class="form-label">
Email Address
</label>
<input type="email"
id="studentEmail"
class="form-control"
placeholder="Enter email"
data-bs-toggle="tooltip"
data-bs-title="Enter a valid email address">
<small class="form-text text-muted">
We use this email for course updates.
</small>
Program Output
6. Tooltip with HTML Content
Bootstrap tooltips support HTML content when
data-bs-html="true" is enabled. Only use trusted,
controlled HTML. Never insert untrusted user input as tooltip HTML.
Program Code
<button type="button"
class="btn btn-primary"
data-bs-toggle="tooltip"
data-bs-html="true"
data-bs-title="<strong>Course:</strong> Full Stack<br>Duration: 6 Months">
Course Information
</button>
Program Output
Hover over the button to see the formatted tooltip.
7. Initializing Tooltips with JavaScript
Unlike some Bootstrap components, tooltips do not become active merely by adding their HTML attributes. Bootstrap JavaScript initialization is required.
Program Code
document.addEventListener('DOMContentLoaded', function () {
const tooltipElements =
document.querySelectorAll('[data-bs-toggle="tooltip"]');
tooltipElements.forEach(function (element) {
bootstrap.Tooltip.getOrCreateInstance(element);
});
});
How It Works
- The browser loads the HTML page.
- The script finds elements with
data-bs-toggle="tooltip". - Bootstrap initializes each tooltip.
- The tooltip can then respond to hover, focus, or other configured triggers.
8. Practical Example: Course Help Buttons
A training website can use tooltips to explain course features without adding long descriptions beside every button.
Program Code
<div class="d-flex flex-wrap gap-2">
<button class="btn btn-outline-primary"
data-bs-toggle="tooltip"
data-bs-title="Learn programming fundamentals">
Core Java
</button>
<button class="btn btn-outline-success"
data-bs-toggle="tooltip"
data-bs-title="Build applications using Spring Boot">
Spring Boot
</button>
<button class="btn btn-outline-danger"
data-bs-toggle="tooltip"
data-bs-title="Create and test web APIs">
REST API
</button>
</div>
Program Output
Hover or keyboard-focus each course button to see its description.
9. Important Tooltip Attributes
| Attribute | Purpose |
|---|---|
data-bs-toggle="tooltip" |
Marks an element as a tooltip trigger. |
data-bs-title |
Specifies the tooltip text. |
data-bs-placement |
Specifies where the tooltip should appear. |
data-bs-html="true" |
Allows HTML in tooltip content. |
data-bs-trigger |
Configures how the tooltip is triggered. |
bootstrap.Tooltip() |
Creates a Bootstrap tooltip instance. |
getOrCreateInstance() |
Gets an existing instance or creates one. |
10. Common Mistakes
- Forgetting to initialize the tooltip using Bootstrap JavaScript.
-
Using Bootstrap 4 attributes such as
data-toggleinstead of Bootstrap 5'sdata-bs-toggle. - Expecting a tooltip to work when the Bootstrap JavaScript bundle is missing.
- Putting important instructions only in a tooltip instead of making them visible to all users.
- Enabling HTML tooltips with untrusted user-generated content.
Summary
- Tooltips display short, contextual information.
data-bs-titlespecifies the tooltip message.data-bs-placementcontrols its preferred position.- Bootstrap JavaScript initialization is required.
- Tooltips can be used on buttons, links, and form controls.
- Important information should remain accessible without hover.