Tooltips·续

Dependencies

Options

Note that for security reasons the sanitize, sanitizeFn, and allowList options cannot be supplied using data attributes.

Name Type Default Description
allowList object Default value An object containing allowed tags and attributes. Those not explicitly allowed will be removed by the content sanitizer. <Callout type="warning">Exercise caution when adding to this list. Refer to OWASP’s Cross Site Scripting Prevention Cheat Sheet for more information.</Callout>
animation boolean true Apply a CSS fade transition to the tooltip. Set it to false to add .tooltip-instant to the tip, which switches the fade off.
boundary string, element 'clippingParents' Overflow constraint boundary of the tooltip (applies only to Floating UI’s shift middleware). By default, it’s 'clippingParents' and can accept an HTMLElement reference (via JavaScript only). For more information refer to Floating UI’s shift docs.
container string, element, false false Appends the tooltip to a specific element. Example: container: 'body'. This option is particularly useful in that it allows you to position the tooltip in the flow of the document near the triggering element - which will prevent the tooltip from floating away from the triggering element during a window resize.
customClass string, function '' Add classes to the tooltip when it is shown. Note that these classes will be added in addition to any classes specified in the template. To add multiple classes, separate them with spaces: 'class-1 class-2'. You can also pass a function that should return a single string containing additional class names.
delay number, object 0 Delay showing and hiding the tooltip (ms)—doesn’t apply to manual trigger type. If a number is supplied, delay is applied to both hide/show. Object structure is: delay: { "show": 500, "hide": 100 }.
fallbackPlacements array ['top', 'right', 'bottom', 'left'] Define fallback placements by providing a list of placements in array (in order of preference). For more information refer to Floating UI’s flip docs.
html boolean false Allow HTML in the tooltip. If true, HTML tags in the tooltip’s title will be rendered in the tooltip. If false, innerText property will be used to insert content into the DOM. Prefer text when dealing with user-generated input to prevent XSS attacks.
offset array, string, function [0, 6] Offset of the tooltip relative to its target. You can pass a string in data attributes with comma separated values like: data-bs-offset="10,20". When a function is used to determine the offset, it is called with an object containing the floating placement, the reference, and floating rects as its first argument. The triggering element DOM node is passed as the second argument. The function must return an array with two numbers: crossAxis, mainAxis. For more information refer to Floating UI’s offset docs.
placement string, function 'top' How to position the tooltip: top, bottom, left, right. Supports responsive prefixes like 'top md:right lg:bottom' to change placement at different breakpoints. When a function is used to determine the placement, it is called with the tooltip DOM node as its first argument and the triggering element DOM node as its second.
floatingConfig null, object, function null To change Bootstrap’s default Floating UI config, see Floating UI’s configuration. When a function is used to create the Floating UI configuration, it’s called with an object that contains the Bootstrap’s default Floating UI configuration. It helps you use and merge the default with your own configuration. The function must return a configuration object for Floating UI.
sanitize boolean true Enable content sanitization. If true, the template, content and title options will be sanitized. <Callout type="warning">Exercise caution when disabling content sanitization. Refer to OWASP’s Cross Site Scripting Prevention Cheat Sheet for more information. Vulnerabilities caused solely by disabling content sanitization are not considered within scope for Bootstrap’s security model.</Callout>
sanitizeFn null, function null Provide an alternative content sanitization function. This can be useful if you prefer to use a dedicated library to perform sanitization.
selector string, false false If a selector is provided, tooltip objects will be delegated to the specified targets. In practice, this is used to also apply tooltips to dynamically added DOM elements (jQuery.on support). See this issue and an informative example. Note: title attribute must not be used as a selector.
template string '<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>' Base HTML to use when creating the tooltip. The tooltip’s title will be injected into the .tooltip-inner. .tooltip-arrow will become the tooltip’s arrow. The outermost wrapper element should have the .tooltip class and role="tooltip".
title string, element, function '' The tooltip title. If a function is given, it will be called with the element it is attached to as its first argument, and with its this reference set to that same element.
trigger string 'hover focus' How tooltip is triggered: click, hover, focus, manual. You may pass multiple triggers; separate them with a space. 'manual' indicates that the tooltip will be triggered programmatically via the .tooltip('show'), .tooltip('hide') and .tooltip('toggle') methods; this value cannot be combined with any other trigger. 'hover' on its own will result in tooltips that cannot be triggered via the keyboard, and should only be used if alternative methods for conveying the same information for keyboard users is present.

Data attributes for individual tooltips

Options for individual tooltips can alternatively be specified through the use of data attributes, as explained above.

Using function with floatingConfig

const tooltip = new bootstrap.Tooltip(element, {
  floatingConfig(defaultBsFloatingConfig) {
    // const newFloatingConfig = {...}
    // use defaultBsFloatingConfig if needed...
    // return newFloatingConfig
  }
})

Methods

Method Description
disable Removes the ability for an element’s tooltip to be shown. The tooltip will only be able to be shown if it is re-enabled.
dispose Hides and destroys an element’s tooltip (Removes stored data on the DOM element). Tooltips that use delegation (which are created using the selector option) cannot be individually destroyed on descendant trigger elements.
enable Gives an element’s tooltip the ability to be shown. Tooltips are enabled by default.
getInstance Static method which allows you to get the tooltip instance associated with a DOM element.
getOrCreateInstance Static method which allows you to get the tooltip instance associated with a DOM element, or create a new one in case it wasn’t initialized.
hide Hides an element’s tooltip. Returns a promise that resolves once the tooltip is hidden (i.e. after the hidden.bs.tooltip event fires). This is considered a “manual” triggering of the tooltip.
setContent Gives a way to change the tooltip’s content after its initialization.
show Reveals an element’s tooltip. Returns a promise that resolves once the tooltip is shown (i.e. after the shown.bs.tooltip event fires). This is considered a “manual” triggering of the tooltip. Tooltips with zero-length titles are never displayed.
toggle Toggles an element’s tooltip. Returns a promise that resolves once the tooltip is shown or hidden (i.e. after the shown.bs.tooltip or hidden.bs.tooltip event fires). This is considered a “manual” triggering of the tooltip.
toggleEnabled Toggles the ability for an element’s tooltip to be shown or hidden.
update Updates the position of an element’s tooltip.
const tooltip = bootstrap.Tooltip.getInstance('#example') // Returns a Bootstrap tooltip instance

// setContent example
tooltip.setContent({ '.tooltip-inner': 'another title' })

The setContent method accepts an object argument, where each property-key is a valid string selector within the tooltip template, and each related property-value can be string | element | function | null

Events

Event Description
hide.bs.tooltip This event is fired immediately when the hide instance method has been called.
hidden.bs.tooltip This event is fired when the tooltip has finished being hidden from the user (will wait for CSS transitions to complete).
inserted.bs.tooltip This event is fired after the show.bs.tooltip event when the tooltip template has been added to the DOM.
show.bs.tooltip This event fires immediately when the show instance method is called.
shown.bs.tooltip This event is fired when the tooltip has been made visible to the user (will wait for CSS transitions to complete).
const myTooltipEl = document.getElementById('myTooltip')
const tooltip = bootstrap.Tooltip.getOrCreateInstance(myTooltipEl)

myTooltipEl.addEventListener('hidden.bs.tooltip', () => {
  // do something...
})

tooltip.hide()