Lesson 33 of 60 – Bootstrap Tooltips
55%

Bootstrap Tooltips

Bootstrap Tooltips are small pop-up messages that appear when a user interacts with an element. They are useful for providing additional information without taking extra space on the page.

Note: Bootstrap Tooltips are powered by JavaScript and Popper. In Bootstrap 5, you need to initialize Tooltips before they become active.

1. What Is a Tooltip?

A Tooltip is a small information box that appears when a user interacts with an element such as a button, link, or icon.

Tooltips are commonly used to:

  • Explain buttons.
  • Describe icons.
  • Provide additional information.
  • Show short instructions.
  • Display helpful hints.

2. Basic Tooltip

A basic Bootstrap Tooltip uses the data-bs-toggle="tooltip" attribute and a title attribute.

<button
type="button"
class="btn btn-primary"
data-bs-toggle="tooltip"
title="This is a tooltip">

Hover over me

</button>

3. The data-bs-toggle Attribute

The data-bs-toggle="tooltip" attribute tells Bootstrap that the element should behave as a Tooltip trigger.

<button
data-bs-toggle="tooltip"
title="Hello">

Hover Me

</button>

Without proper initialization, the Tooltip will not automatically appear just because the attribute is present.

4. The title Attribute

The title attribute contains the text that Bootstrap displays inside the Tooltip.

<button
type="button"
data-bs-toggle="tooltip"
title="Save your document">

Save

</button>

5. Initializing Tooltips

Bootstrap Tooltips must be initialized with JavaScript.

const tooltipTriggerList =
document.querySelectorAll(
'[data-bs-toggle="tooltip"]'
);

const tooltipList =
[...tooltipTriggerList].map(
tooltipTriggerEl =>
new bootstrap.Tooltip(tooltipTriggerEl)
);

This code finds all elements containing data-bs-toggle="tooltip" and creates a Bootstrap Tooltip for each one.

6. Tooltip on a Link

Tooltips can be used with links as well as buttons.

<a
href="#"
data-bs-toggle="tooltip"
title="Open the home page">

Home

</a>
Home

7. Tooltip on an Icon

Tooltips are especially useful for icons because icons may not always be obvious to every user.

<button
type="button"
class="btn btn-info"
data-bs-toggle="tooltip"
title="Information">

ℹ️

</button>

8. Tooltip Position

Bootstrap supports different Tooltip positions. Common positions are:

  • Top
  • Bottom
  • Left
  • Right

The default Tooltip placement is generally above the element when there is enough available space.

9. Tooltip on Top

Use data-bs-placement="top" to request a Tooltip above the element.

<button
class="btn btn-primary"
data-bs-toggle="tooltip"
data-bs-placement="top"
title="Tooltip on top">

Top

</button>

10. Tooltip on Bottom

Use data-bs-placement="bottom" to request a Tooltip below the element.

<button
class="btn btn-success"
data-bs-toggle="tooltip"
data-bs-placement="bottom"
title="Tooltip on bottom">

Bottom

</button>

11. Tooltip on Left

Use data-bs-placement="left" to request a Tooltip on the left side.

<button
class="btn btn-warning"
data-bs-toggle="tooltip"
data-bs-placement="left"
title="Tooltip on left">

Left

</button>

12. Tooltip on Right

Use data-bs-placement="right" to request a Tooltip on the right side.

<button
class="btn btn-danger"
data-bs-toggle="tooltip"
data-bs-placement="right"
title="Tooltip on right">

Right

</button>

13. Tooltip with Different Text

Each Tooltip can contain different text.

<button
class="btn btn-primary"
data-bs-toggle="tooltip"
title="Add a new student">

Add Student

</button>

<button
class="btn btn-success"
data-bs-toggle="tooltip"
title="Save student details">

Save

</button>

14. Tooltip on Images

A Tooltip can also be attached to an image.

<img
src="student.jpg"
alt="Student"
data-bs-toggle="tooltip"
title="Student Profile"
width="100">

When using images, always provide meaningful alt text for accessibility.

15. Tooltip on Disabled Elements

Disabled elements such as disabled buttons generally cannot trigger interaction-based Tooltips themselves.

A common solution is to place the disabled element inside a wrapper such as a span and attach the Tooltip to the wrapper.

<span
data-bs-toggle="tooltip"
title="This option is unavailable">

    <button
    class="btn btn-secondary"
    disabled>

    Disabled

    </button>

</span>

16. Tooltip with HTML

Bootstrap Tooltips can be configured to allow HTML content, but this should be done carefully.

const tooltip =
new bootstrap.Tooltip(
element,
{
html: true
}
);

If HTML content is accepted from users or an untrusted source, it can create security risks. Trusted and properly sanitized content should be used.

17. Tooltip Delay

Bootstrap Tooltips can be configured with a delay before they appear or disappear.

const tooltip =
new bootstrap.Tooltip(
element,
{
delay: {
show: 500,
hide: 100
}
}
);

The values are specified in milliseconds.

18. Tooltip Trigger

A Tooltip can be triggered using different interactions such as hover, focus, or click.

const tooltip =
new bootstrap.Tooltip(
element,
{
trigger: 'hover'
}
);

Bootstrap supports trigger values such as click, hover, focus, and combinations of them.

19. Tooltip Using JavaScript

Tooltips can be initialized using JavaScript by selecting an element.

const button =
document.querySelector('#myButton');

const tooltip =
new bootstrap.Tooltip(button);

This method is useful when Tooltip configuration needs to be controlled through JavaScript.

20. Initializing Multiple Tooltips

When a page contains several Tooltips, you can initialize all matching elements at once.

const tooltipTriggerList =
document.querySelectorAll(
'[data-bs-toggle="tooltip"]'
);

const tooltipList =
[...tooltipTriggerList].map(
tooltipTriggerEl =>
new bootstrap.Tooltip(
tooltipTriggerEl
)
);

This is a common pattern for pages containing many Tooltip triggers.

21. Tooltip in a Navigation Menu

Tooltips can provide additional information for navigation buttons or icons.

<a
href="#"
class="btn btn-outline-primary"
data-bs-toggle="tooltip"
title="Open Dashboard">

Dashboard

</a>
Dashboard

22. Tooltip in an Admin Dashboard

Tooltips are useful in admin dashboards where icons represent actions such as edit, delete, view, and settings.

<button
class="btn btn-warning"
data-bs-toggle="tooltip"
title="Edit Student">

✏️

</button>

<button
class="btn btn-danger"
data-bs-toggle="tooltip"
title="Delete Student">

🗑️

</button>

23. Tooltip Accessibility

Tooltips should provide helpful information without making the interface difficult to use.

  • Use meaningful Tooltip text.
  • Do not put essential information only inside a Tooltip.
  • Make important controls accessible using keyboard interaction.
  • Provide accessible names for icon-only buttons.
  • Do not depend only on hover for important information.
Tip: Tooltips should supplement an interface, not replace labels or essential instructions.

24. Tooltip and Popover Difference

Tooltips and Popovers are both Bootstrap components, but they serve different purposes.

Tooltip Popover
Usually short information Can contain more detailed content
Useful for hints Useful for additional information
Usually compact Usually larger
Uses Tooltip component Uses Popover component

25. Common Tooltip Mistakes

Avoid these common mistakes when working with Bootstrap Tooltips:

  • Forgetting to initialize Tooltips.
  • Forgetting the data-bs-toggle="tooltip" attribute.
  • Using unclear Tooltip text.
  • Using Tooltips for essential information.
  • Relying only on hover interactions.
  • Using very long Tooltip messages.
  • Adding too many Tooltips to one page.
  • Using unsanitized HTML content inside Tooltips.

26. Tooltip with Bootstrap Icons

Tooltips are especially useful with icon-only buttons.

<button
class="btn btn-primary"
data-bs-toggle="tooltip"
title="Settings">

⚙️

</button>

The Tooltip provides additional information about what the icon represents.

27. Tooltip for Student Management

In a student management system, Tooltips can explain actions without making the interface crowded.

<button
class="btn btn-info"
data-bs-toggle="tooltip"
title="View Student Profile">

View

</button>

<button
class="btn btn-warning"
data-bs-toggle="tooltip"
title="Edit Student Information">

Edit

</button>

<button
class="btn btn-danger"
data-bs-toggle="tooltip"
title="Delete Student">

Delete

</button>

28. Tooltip with JavaScript Configuration

JavaScript configuration gives you more control over Tooltip behavior.

const elements =
document.querySelectorAll(
'[data-bs-toggle="tooltip"]'
);

elements.forEach(function(element){

    new bootstrap.Tooltip(
        element,
        {
            placement: 'bottom',
            delay: {
                show: 300,
                hide: 100
            }
        }
    );

});

This example requests a bottom placement and adds a small delay. Bootstrap may still adjust placement when necessary to keep the Tooltip visible.

29. Practical Tooltip Example

The following example creates a small action area with Tooltips.

<div class="btn-group">

    <button
    class="btn btn-primary"
    data-bs-toggle="tooltip"
    title="View details">

    👁️

    </button>

    <button
    class="btn btn-warning"
    data-bs-toggle="tooltip"
    title="Edit record">

    ✏️

    </button>

    <button
    class="btn btn-danger"
    data-bs-toggle="tooltip"
    title="Delete record">

    🗑️

    </button>

</div>

30. Complete Bootstrap Tooltip Example

This complete example demonstrates multiple Bootstrap Tooltips with different placements and useful action labels.

<div class="d-flex gap-2">

    <button
    class="btn btn-primary"
    data-bs-toggle="tooltip"
    data-bs-placement="top"
    title="View dashboard">

    Dashboard

    </button>

    <button
    class="btn btn-warning"
    data-bs-toggle="tooltip"
    data-bs-placement="bottom"
    title="Edit student">

    Edit

    </button>

    <button
    class="btn btn-danger"
    data-bs-toggle="tooltip"
    data-bs-placement="right"
    title="Delete student">

    Delete

    </button>

</div>


<script>

const tooltipTriggerList =
document.querySelectorAll(
'[data-bs-toggle="tooltip"]'
);

const tooltipList =
[...tooltipTriggerList].map(
tooltipTriggerEl =>
new bootstrap.Tooltip(
tooltipTriggerEl
)
);

</script>
Important: For the live example above to work, Bootstrap's JavaScript bundle must be loaded and the Tooltip initialization code must run after the Bootstrap library is available.

📌 Key Points

  • Tooltips provide short additional information.
  • data-bs-toggle="tooltip" identifies a Tooltip trigger.
  • The title attribute provides the Tooltip text.
  • Bootstrap Tooltips require JavaScript initialization.
  • Tooltips can be used with buttons, links, images, and icons.
  • Tooltips can be positioned at the top, bottom, left, or right.
  • Tooltips can be configured using JavaScript.
  • Disabled controls need special handling because they cannot normally trigger interaction-based Tooltips themselves.
  • Tooltips can be useful in admin dashboards.
  • Tooltips are useful for icon-only action buttons.
  • Do not use Tooltips as the only place for essential information.
  • Use meaningful and concise Tooltip text.
  • Be careful when allowing HTML inside Tooltips.
  • Tooltips should be accessible to keyboard users.
  • Bootstrap Tooltips use Popper for positioning behavior.

🧠 Quick Quiz

Question: Which attribute is used to identify an element as a Bootstrap Tooltip trigger?