Bs5 Tooltip in Bootstrap
Absolutely! Here’s a quick guide on Bootstrap 5 Tooltips — small popups that display informative text when users hover, focus, or tap on an element.
? Bootstrap 5 Tooltips
Tooltips are used to provide additional context or information about an element on hover or focus.
1. Basic Tooltip Example
<button type="button" class="btn btn-secondary" data-bs-toggle="tooltip" data-bs-placement="top" title="Tooltip on top"> Hover me</button>Add
data-bs-toggle="tooltip"to enable tooltip on that element.Use
data-bs-placementto specify position:top,bottom,left, orright.The
titleattribute contains the tooltip text.
2. Initialize Tooltips with JavaScript
Bootstrap 5 requires you to initialize tooltips via JS for them to work:
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'))var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) { return new bootstrap.Tooltip(tooltipTriggerEl)})This activates all tooltips on the page with data-bs-toggle="tooltip".
3. Tooltip Options
You can customize tooltips with options, e.g.,
var tooltip = new bootstrap.Tooltip(document.querySelector('#myTooltip'), { placement: 'right', trigger: 'hover focus', delay: { "show": 500, "hide": 100 }});placement: where the tooltip shows (top,bottom,left,right,auto)trigger: how tooltip is triggered (hover,focus,click,manual)delay: delay time in ms before showing/hiding
4. Example with Manual Trigger
<button id="manualTooltip" type="button" class="btn btn-info" title="Manual tooltip"> Click me</button><script> var manualTooltip = new bootstrap.Tooltip(document.getElementById('manualTooltip'), { trigger: 'manual' }); document.getElementById('manualTooltip').addEventListener('click', function () { manualTooltip.toggle(); });</script>5. HTML Content in Tooltips
By default, tooltips escape HTML. To allow HTML:
var tooltip = new bootstrap.Tooltip(document.querySelector('#htmlTooltip'), { html: true, title: '<em>Italic</em> <u>and underlined</u>'});Summary Table
| Attribute/Class | Description |
|---|---|
data-bs-toggle="tooltip" | Enables tooltip on element |
data-bs-placement | Position of tooltip (top, bottom, etc.) |
title | Tooltip text |
bootstrap.Tooltip | JS API to programmatically control tooltips |
trigger | Events to trigger tooltip |
delay | Delay before showing/hiding |
html | Enable HTML inside tooltip content |
If you want, I can help you build interactive tooltip demos or customize styles!