Bootstrap 5 Tutorial

v5.3.0

Bootstrap 5 Tutorial

Tooltips in Bootstrap 5

Tooltips: Small popup boxes that appear when hovering over or focusing on an element.

Basic Tooltip

Simple Tooltip Example
<!-- Basic tooltip -->
            <button type="button" 
                    class="btn btn-secondary" 
                    data-bs-toggle="tooltip" 
                    data-bs-placement="top" 
                    data-bs-title="Tooltip on top">
            Hover over me
            </button>

            <!-- JavaScript initialization -->
            <script>
            const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
            const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
            </script>

Tooltip Directions

All Placement Options
<!-- Placement options -->
            <button data-bs-placement="top">Top</button>
            <button data-bs-placement="right">Right</button>
            <button data-bs-placement="bottom">Bottom</button>
            <button data-bs-placement="left">Left</button>
            <button data-bs-placement="auto">Auto (default)</button>

Tooltip Triggers

Hover (Default)
data-bs-toggle="tooltip"
Click
data-bs-trigger="click"
Focus
data-bs-trigger="focus"

HTML Content in Tooltips

Rich Content Tooltips
<!-- HTML content in tooltip -->
            <button type="button" 
                    class="btn btn-info" 
                    data-bs-toggle="tooltip" 
                    data-bs-html="true" 
                    data-bs-title="<strong>HTML Content</strong><br><small>With multiple lines</small><br><span class='badge bg-success'>New</span>">
            HTML Tooltip
            </button>

            <!-- Using data-bs-template -->
            <button type="button" 
                    class="btn btn-secondary" 
                    data-bs-toggle="tooltip" 
                    data-bs-custom-class="custom-tooltip" 
                    data-bs-title="Custom styled tooltip">
            Custom Template
            </button>

Delay Options

Show Delay
data-bs-delay='show:1000'
Hide Delay
data-bs-delay='hide: 1000'

Custom Styling

Custom Class
Gradient Tooltip
<!-- Custom class -->
            <button data-bs-custom-class="custom-tooltip">
            Custom Styled
            </button>

            <!-- Custom CSS -->
            .custom-tooltip .tooltip-inner {
            background-color: #6f42c1;
            color: white;
            border-radius: 10px;
            border: 2px solid #4a2d8a;
            }

            .custom-tooltip.bs-tooltip-top .tooltip-arrow::before {
            border-top-color: #6f42c1;
            }

            .gradient-tooltip .tooltip-inner {
            background: linear-gradient(45deg, #FF6B6B, #FF8E53);
            color: white;
            border-radius: 20px;
            }

Practical Examples

Real-World Usage
Form Field Help
Disabled Button Tooltip

Tooltip appears on hover even when disabled

Table Actions
NameActions
John Doe
Status Indicators
Active
User Status
Pending
Approval Status
Blocked
Account Status

JavaScript API

JavaScript Methods and Events
MethodDescriptionExample
.show()Shows tooltipmyTooltip.show()
.hide()Hides tooltipmyTooltip.hide()
.toggle()Toggles tooltipmyTooltip.toggle()
.dispose()Destroys tooltipmyTooltip.dispose()
.enable()Enables tooltipmyTooltip.enable()
.disable()Disables tooltipmyTooltip.disable()
.toggleEnabled()Toggles enabled statemyTooltip.toggleEnabled()
.update()Updates tooltip positionmyTooltip.update()
Events
  • show.bs.tooltip - Fires immediately when show is called
  • shown.bs.tooltip - Fires when tooltip is visible
  • hide.bs.tooltip - Fires immediately when hide is called
  • hidden.bs.tooltip - Fires when tooltip is hidden
  • inserted.bs.tooltip - Fires after tooltip content is added to DOM
// JavaScript initialization and usage

            // Initialize all tooltips
            const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
            const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))

            // Initialize specific tooltip with options
            const myTooltip = new bootstrap.Tooltip(document.getElementById('myTooltip'), {
            title: 'Custom tooltip',
            placement: 'right',
            trigger: 'hover',
            delay: {show: 300, hide: 100},
            html: true,
            customClass: 'custom-tooltip'
            })

            // Programmatic control
            myTooltip.show()     // Show tooltip
            myTooltip.hide()     // Hide tooltip
            myTooltip.toggle()   // Toggle tooltip
            myTooltip.dispose()  // Destroy tooltip

            // Event handling
            document.getElementById('myTooltip').addEventListener('shown.bs.tooltip', function () {
            console.log('Tooltip was shown')
            })

            // Update tooltip content dynamically
            function updateTooltip(elementId, newContent) {
            const tooltip = bootstrap.Tooltip.getInstance(document.getElementById(elementId));
            if (tooltip) {
                tooltip.setContent({'.tooltip-inner': newContent});
                tooltip.update();
            }
            }

            // Create tooltip dynamically
            function addTooltip(element, content, options = {}) {
            const defaultOptions = {
                placement: 'top',
                trigger: 'hover',
                html: true
            };
            
            const mergedOptions = {...defaultOptions, ...options};
            mergedOptions.title = content;
            
            return new bootstrap.Tooltip(element, mergedOptions);
            }

Accessibility Features

Accessibility Best Practices
  • Add aria-describedby for screen readers
  • Use proper focus management for keyboard users
  • Ensure tooltips are dismissible with keyboard
  • Add tabindex="0" to non-focusable elements
  • Test with screen readers
  • Provide alternative ways to access information

Common Issues and Solutions

Troubleshooting
  • Tooltip not showing: Check Bootstrap JS is loaded
  • Wrong positioning: Check container has proper positioning
  • HTML not rendering: Ensure data-bs-html="true" is set
  • Multiple tooltips: Ensure proper initialization
  • Mobile issues: Test touch interactions
  • Performance: Avoid too many tooltips on one page

Comparison with Popovers

AspectTooltipsPopovers
PurposeShort descriptions/hintsDetailed content/actions
ContentText only (can have HTML)Rich content with header/body
SizeSmallerLarger
TriggerHover/focus/clickClick only (by default)
HeaderNo headerHas header with title
Use CaseForm hints, button labelsDetailed info, confirmations