BOOTSTRAP BASIC
BOOTSTRAP ADVANCED
BOOTSTRAP EXAMPLES
BOOTSTRAP ARCHIVE
Advertisements

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.

<a href="#" data-toggle="tooltip" title="Some text">Hover over me</a>

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. Also, tooltips with zero-length titles are never displayed, and tooltips on hidden elements will not work.

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.

<script>
$(document).ready(function(){
    $('[data-toggle="tooltip"]').tooltip();   
});
</script>

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


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.

<a href="#" data-toggle="tooltip" data-placement="top" title="Tooltip on top">Tooltip on top</a>
<a href="#" data-toggle="tooltip" data-placement="right" title="Tooltip on right">Tooltip on right</a>
<a href="#" data-toggle="tooltip" data-placement="bottom" title="Tooltip on bottom">Tooltip on bottom</a>
<a href="#" data-toggle="tooltip" data-placement="left" title="Tooltip on left">Tooltip on left</a>

Positioning of Tooltips via JavaScript

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

<script>
$(document).ready(function(){
    $(".tip-top").tooltip({placement : 'top'});
    $(".tip-right").tooltip({placement : 'right'});
    $(".tip-bottom").tooltip({placement : 'bottom'});
    $(".tip-left").tooltip({ placement : 'left'});
});
</script>

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.
container string | element | false false

Appends the tooltip to a specific element.

Specify container: 'body' to avoid rendering problems in more complex components (like input groups, button groups, etc.)

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 }

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, jQuery's text() method will be used to insert content into the DOM.

Use simple 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.

selector string | false false

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

This is normally used to apply tooltips to dynamically added DOM elements.

template string '<div class="tooltip" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>'

Base HTML to use when creating the tooltip.

The tooltip's title will be inserted into the .tooltip-inner element, and the .arrow element will become the tooltip's arrow.

The outermost wrapper element should have the .tooltip class.

title string | element | 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. You can pass multiple triggers; separated with a space.

The value '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.

offset number | string | function 0 Offset of the tooltip relative to its target.
fallbackPlacement string | array 'flip' Allows you to specify which position Popper will use on fallback.
customClass string | function ''

Add classes to the tooltip when it is shown. Please note that these classes will be added in addition to any classes specified in the template. To add multiple classes, separate them with spaces like: 'class1 class2'.

You can also pass a function that should return a single string containing additional class names.

boundary string | element 'scrollParent' Overflow constraint boundary of the tooltip. Accepts the values of 'viewport', 'window', 'scrollParent', or an HTMLElement reference (JavaScript only).
sanitize boolean true Enable or disable the sanitization. If activated 'template' and 'title' options will be sanitized.
whiteList object value Object which contains allowed attributes and tags.
sanitizeFn null | function null Allows you to specify your own sanitize function.
popperConfig null | object null Allows you to change Bootstrap's default Popper config, see Popper's configuration.

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:

<script>
$(document).ready(function(){
    $("#myTooltips a").tooltip({
        title: "This will show in absence of title attribute."
    });
});
</script>

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

<script>
$(document).ready(function(){
    $("#myTooltip").tooltip({
        title: "<h4><img src='images/smiley.png' alt='Smiley'> Hello, <b>I'm</b> <i>Smiley!</i></h4>",  
        html: true
    }); 
});
</script>

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.

<script>
$(document).ready(function(){
    // Showing and hiding tooltip with same speed
    $(".tooltip-tiny").tooltip({
        delay: 500
    });
    
    // Showing and hiding tooltip with different speed
    $(".tooltip-large").tooltip({
        delay: {show: 0, hide: 500}
    }); 
});
</script>

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

<script>
$(document).ready(function(){
    $("#myTooltips a").tooltip({
        template: '<div class="tooltip"><div class="arrow"></div><div class="tooltip-head"><h3><i class="fa fa-info-circle"></span> Tool Info</h3></div><div class="tooltip-inner"></div></div>'
    });
});
</script>

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

<script>
$(document).ready(function(){
    // Append tooltip HTML to wrapper element
    $("#myTooltips a").tooltip({container: ".wrapper"}); 
});
</script>

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.

<script>
$(document).ready(function(){
    $(".show-tooltip").click(function(){
        $("#myTooltip").tooltip('show');
    }); 
});
</script>

.tooltip('hide')

This method hides an element's tooltip.

<script>
$(document).ready(function(){
    $(".hide-tooltip").click(function(){
        $("#myTooltip").tooltip('hide');
    }); 
});
</script>

.tooltip('toggle')

This method toggles an element's tooltip.

<script>
$(document).ready(function(){
    $(".toggle-tooltip").click(function(){
        $("#myTooltip").tooltip('toggle');
    }); 
});
</script>

.tooltip('dispose')

This method hides and destroys an element's tooltip.

<script>
$(document).ready(function(){
    $(".destroy-tooltip").click(function(){
        $("#myTooltip").tooltip('dispose');
    }); 
});
</script>

.tooltip('enable')

This method gives an element's tooltip the ability to be shown. Tooltips are enabled by default.

<script>
$(document).ready(function(){
    $(".enable-tooltip").click(function(){
        $("#myTooltip").tooltip('enable');
    }); 
});
</script>

.tooltip('disable')

This method 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.

<script>
$(document).ready(function(){
    $(".disable-tooltip").click(function(){
        $("#myTooltip").tooltip('disable');
    }); 
});
</script>

.tooltip('toggleEnabled')

This method toggles the ability for an element's tooltip to be shown or hidden.

<script>
$(document).ready(function(){
    $(".toggle-enabled-tooltip").click(function(){
        $("#myTooltip").tooltip('toggleEnabled');
    }); 
});
</script>

.tooltip('update')

This method updates the position of an element's tooltip.

<script>
$(document).ready(function(){
    $(".update-tooltip").click(function(){
        $("#myTooltip").tooltip('update');
    }); 
});
</script>

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.

<script>
$(document).ready(function(){
    $('[data-toggle="tooltip"]').on('hidden.bs.tooltip', function(){
        alert("Tooltip has been completely closed.");
    });
});
</script>
Advertisements
Bootstrap UI Design Templates