Bootstrap Tool tips

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Bootstrap Tooltips

In this tutorial you will learn how to create tooltips with Bootstrap.

Creating the Tooltips With Bootstrap

A tooltip is a small pop up that appears when user places the mouse pointer over an element such as link or buttons to provide hint or information about the element being hovered.

Tooltips can be very helpful for the new visitors of your website because they enable the user to know the purpose of icons and links by placing the mouse pointer over them.

Step 1: Adding the Tooltip Markup

To create a tooltip, you need to add the data-toggle="tooltip" attribute to an element. Tolltip text that would display on hover can be specified using the title attribute.

Here is the standard markup for adding a tolltip to a hyperlink.

Hover over me

Similarly, you can add tooltips to the other elements such as buttons, icons, etc.

 

Note:For performance reasons the tooltip data-apis are opt in, means to use tooltips you must initialize them yourself with tooltip() method.

Step 2: Triggering the Tooltips

Tooltips can be triggered via JavaScript — just call the tooltip() Bootstrap method with the id, class or any CSS selector of the target element in your JavaScript code.

You can either initialize tooltips individually or all in one go. The following jQuery code will initialize all tooltips on a page by selecting them by their data-toggle attribute.

Example

Try this code »

  •  

— The output of the above example will look something like this:

Bootstrap Tooltip


Setting the Directions of Tooltips

You can set tooltips to appear on top, right, bottom and left sides of an element.

Positioning of Tooltips via Data Attributes

The following example will show you how to set the direction of tooltips via data attributes.

Example

Try this code »

  • Tooltip

  • Another tooltip

  • Large tooltip

  • Last tooltip

Positioning of Tooltips via JavaScript

The following example will show you how to set the direction of tooltips via JavaScript.

Example

Try this code »

  •  


Options

There are certain options which may be passed to tooltip() Bootstrap method to customize the functionality of the tooltip plugin.

Name

Type

Default Value

Description

animation

boolean

true

Apply a CSS fade transition to the tooltip.

html

boolean

false

Insert html into the tooltip. If false, jQuery's text() method will be used to insert content into the DOM. Use text if you're worried about XSS attacks.

placement

string | function

'top'

Sets the position of the tooltip — top | bottom | left | right | auto. When "auto" value is specified, it will dynamically reorient the tooltip.

For example, if placement value is "auto top", the tooltip will display on the top when possible, otherwise it will display on the bottom.

selector

string

false

If a selector is provided, tooltip objects will be attached to the specified targets.

title

string | function

''

Sets the default title value if title attribute isn't present.

trigger

string

'hover focus'

Specify how tooltip is triggered — click | hover | focus | manual. Note you case pass trigger mutliple, space seperated, trigger types.

delay

number | object

0

Time to delay in showing and hiding the tooltip (ms) — does not 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 }

container

string | false

false

Appends the tooltip to a specific element container: 'body'

template

string

'

 
 
'

 

Base HTML to use when creating the tooltip. The tooltip's title will be inserted into the element having the class .tooltip-inner and the element with the class .tooltip-arrow will become the tooltip's arrow.

The outermost wrapper element should have the .tooltip class.

viewport

string | object

{ selector: 'body', padding: 0 }

Keeps the tooltip within the bounds of this element. Example: viewport: '#viewport' or { selector: '#viewport', padding: 0 }

You may set these options either through the use of data attributes or JavaScript. For setting the tooltips options via data attributes, just append the option name to data- along with the correct value, like data-animation="false", data-placement="bottom" etc.

However, JavaScript is the more preferable way for setting these options as it prevents you from repetitive work. See the tooltip's method $().tooltip(options) in the section below to know how to set the options for tooltips using the JavaScript.

Methods

These are the standard Bootstrap's tooltip methods:

$().tooltip(options)

This method attaches the tooltip handler to a group of elements. It also allows you to set the options for the tooltips, so that you can customize them according to your needs.

The following example will insert the specified text inside the tooltips if the value of the title attribute is omitted or missing from the selected elements:

Example

Try this code »

  •  

The following example will show you how to place the HTML content inside a tooltip:

Example

Try this code »

  •  

The following example will show you how to control the timing of showing and hiding of the tooltip using the tooltip's delay option via JavaScript.

Example

Try this code »

  •  

The following example will show you how you can create your own custom template for the Bootstrap tooltips instead of using the default one.

Example

Try this code »

  •  

The following example will insert the dynamically generated HTML code of the tooltip at the end of a .wrapper element instead of the

element.

Example

Try this code »

  •  

 

Note:Overriding the tooltip's default container option value does not produce any visible difference on the page. To see the actual result you need inspect the resulting DOM using the Firebug or Developer tools.

Similarly, you can set other options for the tooltips using the $().tooltip(options) method. Let's check out the other methods of the Bootstrap tooltip plugin.

.tooltip('show')

This method reveals an element's tooltip.

Example

Try this code »

  •  

.tooltip('hide')

This method hides an element's tooltip.

Example

Try this code »

  •  

.tooltip('toggle')

This method toggles an element's tooltip.

Example

Try this code »

  •  

.tooltip('destroy')

This method hides and destroys an element's tooltip.

Example

Try this code »

  •  


Events

Bootstrap's tooltip class includes few events for hooking into tooltip functionality.

Event

Description

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. It will wait until the CSS transition process has been fully completed before getting fired.

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. It will wait until the CSS transition process has been fully completed before getting fired.

inserted.bs.tooltip

This event is fired after the show.bs.tooltip event when the tooltip template has been added to the DOM.

The following example will display an alert message to the user when the fade out transition of the tooltip has been fully completed.

Example

Try this code »

  •