BS4 Tooltip


Tooltips are small, interactive popups that provide contextual information when a user hovers over, focuses on, or taps an element. In Bootstrap 4, tooltips are highly useful for giving hints, explanations, or brief instructions without cluttering the interface. They can be applied to buttons, links, icons, form elements, or any element that might need extra guidance. Properly implemented tooltips improve usability while keeping the design clean.

Enabling Tooltips

By default, Bootstrap 4 tooltips require JavaScript to work. Before using tooltips, you need to initialize them, either globally or for specific elements using jQuery.

$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})
  • This snippet initializes all elements with the data-toggle="tooltip" attribute.

  • Without this initialization, tooltips will not display, even if the HTML markup is correct.

  • Tooltips rely on the Popper.js library for positioning, which is included in Bootstrap 4.

Adding a Tooltip

A tooltip can be added to any HTML element using data-toggle="tooltip" along with a title attribute that defines the tooltip text.

<button type="button" class="btn btn-secondary" data-toggle="tooltip" title="Click here to submit">
  Submit
</button>
  • The title attribute contains the message displayed inside the tooltip.

  • Tooltips appear when the user hovers over the element on desktop, and on focus for keyboard users.

  • They provide guidance without taking up additional space in the layout.

Tooltip Placement

Bootstrap 4 allows you to control where tooltips appear relative to the target element using the data-placement attribute. The main options are top, bottom, left, and right.

<button type="button" class="btn btn-info" data-toggle="tooltip" data-placement="top" title="Tooltip on top">
  Top
</button>
<button type="button" class="btn btn-info" data-toggle="tooltip" data-placement="right" title="Tooltip on right">
  Right
</button>
  • Placement ensures that tooltips do not obstruct important content.

  • Tooltips automatically adjust to prevent overflow off the screen.

  • Using different placements can improve readability and user experience.

HTML Content in Tooltips

Bootstrap tooltips can display HTML content for richer formatting. This is done using the html: true option in JavaScript.

$('#exampleTooltip').tooltip({
  html: true,
  title: '<b>Bold Text</b> and <i>Italic Text</i>'
})
  • HTML tooltips can include links, bold or italic text, and simple formatting.

  • Avoid complex HTML to maintain clarity and accessibility.

  • Overly long tooltips can become hard to read, so keep content concise.

Tooltip Triggers

By default, tooltips are triggered on hover and focus. You can customize triggers using data-trigger or JavaScript options.

<button type="button" class="btn btn-warning" data-toggle="tooltip" data-trigger="click" title="Click triggered tooltip">
  Click Me
</button>
  • Available triggers include hover, focus, click, and manual.

  • Using manual allows full programmatic control, useful for interactive applications.

  • Multiple triggers can be combined for better accessibility.

Tooltip Delay

Bootstrap allows setting delays to control when a tooltip shows or hides. This is useful for preventing flickering when the mouse moves quickly.

$('#exampleTooltip').tooltip({
  delay: { "show": 500, "hide": 100 }
})
  • This configuration shows the tooltip 500ms after hover and hides it 100ms after mouse leaves.

  • Proper delay timing improves readability and ensures users have time to see the tooltip.

Styling Tooltips

Tooltips can be customized through CSS for color, font, and size.

.tooltip-inner {
  background-color: #343a40;
  color: #fff;
  font-size: 14px;
  padding: 8px 12px;
}
.tooltip-arrow {
  border-top-color: #343a40;
}
  • tooltip-inner styles the content area, while tooltip-arrow styles the pointer.

  • Custom styling helps integrate tooltips with your site’s color scheme and branding.

Accessibility Considerations

  • Tooltips should supplement visible content, not replace it.

  • Ensure tooltip messages are concise and meaningful.

  • Include aria-label or proper text alternatives for screen readers.

  • Tooltips triggered by hover must also be accessible via keyboard focus.

  • Avoid overusing tooltips to prevent cognitive overload for users.

Responsive Considerations

  • Tooltips automatically reposition if they would overflow the viewport.

  • On small touch devices, tooltips may require tap to trigger instead of hover.

  • Test tooltips across multiple screen sizes to ensure visibility and usability.

  • Keep content short on small screens to avoid truncated tooltips.

Real-World Use Cases

  • Explaining button icons or form input requirements.

  • Providing additional context on dashboard metrics or charts.

  • Highlighting new features or tips for users in web applications.

  • Offering guidance in admin panels, toolbars, or e-commerce sites.

Best Practices

  • Use tooltips for brief hints or supplemental information.

  • Avoid including critical content exclusively inside a tooltip.

  • Keep messages concise and legible.

  • Ensure tooltips are fully accessible via keyboard and screen readers.

  • Test visual placement and responsiveness on all devices.

Summary of the Tutorial

This tutorial covered Bootstrap 4 tooltips and their implementation. You learned:

  • How to initialize tooltips with jQuery and the importance of Popper.js.

  • Adding tooltips to elements using data-toggle="tooltip" and title.

  • Controlling placement with data-placement for better visibility.

  • Using HTML content for rich formatting inside tooltips.

  • Customizing triggers, delays, and manual control.

  • Styling tooltips with CSS for color, font, and size.

  • Accessibility considerations for screen readers and keyboard users.

  • Responsive design and real-world use cases for tooltips.

Bootstrap 4 tooltips provide an elegant way to enhance user experience by giving contextual information without adding clutter. When implemented correctly, they improve usability, accessibility, and interface clarity across devices.


Practice Questions

  1. Create a button with a tooltip that appears on hover and displays “Submit your form.”

  2. Add a tooltip to an icon that appears on the right side using data-placement="right".

  3. Create a link with a tooltip that triggers on focus for keyboard accessibility.

  4. Build a button with an HTML tooltip containing bold and italic text.

  5. Create a tooltip that only appears when the button is clicked using data-trigger="click".

  6. Build a button with a tooltip that has a 500ms show delay and 100ms hide delay.

  7. Create a tooltip for a form input field explaining the expected format of the input.

  8. Style a tooltip with a custom background color and font size using CSS.

  9. Add tooltips to multiple buttons on a page and initialize them using jQuery.

  10. Create a tooltip on a small icon that adjusts its placement automatically to remain visible on small screens.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top