Open In App

Bootstrap Tooltips

In this article, we would be discussing the tooltip plugin provided by bootstrap. Tooltip is quite useful for showing the description of different elements in the webpage. Tooltip can be invoked on any element in a webpage. Tooltips on bootstrap depends on the 3rd party library Tether for positioning. Hence, we need to include tether.min.js before bootstrap.js Now let’s see an example of a tooltip.  

Now we will examine the code which generates the above tooltip 






<!-- Tooltip on a header -->
<h3 data-toggle="tooltip" title="Hey! Tooltip here!">
    Hover Over me to see a tooltip
</h3>

In order to introduce tooltip, we add the data-toggle attribute to an element and we need to initialize the tooltip with jQuery. jQuery code for initializing a tooltip: 




<script>
    // jQuery code for initializing a tooltip
    $(document).ready(function () {
        // jQuery Attribute value selector to 
        // select the specified element and 
        // call the tooltip method on it
        $('[data-toggle="tooltip"]').tooltip();
    });
</script>

We can even customize this tool-tip according to our requirement, let’s explore different ways in which we can customize the tool-tip.



Example:

  

Code for the above example: 




<!-- Tooltips on simple buttons -->
<!-- Placement of tooltips -->
<div class="row" style="margin:30px">
    <div class="col-2">
        <button type="button" class="btn btn-info" 
            data-toggle="tooltip" data-placement="top"
            title="Information Button">
            Information
        </button>
    </div>
    <div class="col-2">
        <button type="button" class="btn btn-success" 
            data-toggle="tooltip" data-placement="right"
            title="Success Button">
            Success
        </button>
    </div>
    <div class="col-2">
        <button type="button" class="btn btn-danger" 
            data-toggle="tooltip" data-placement="bottom"
            title="Danger button">
            Danger
        </button>
    </div>
    <div class="col-2">
        <button type="button" class="btn btn-warning" 
            data-toggle="tooltip" data-placement="left"
            title="Warning button">
            Warning
        </button>
    </div>
</div>

Example:

  

Code for the above example: 




<!-- Tooltip with html -->
<div class="row" style="margin:40px">
    <button type="button" class="btn btn-warning" 
        data-toggle="tooltip" data-placement="left" 
        data-html="true" title=
        "<h4>Hey!</h4>
<p>Tooltip with html</p>
">
        Warning
    </button>
</div>

Example:

 

 Code for the above example: 




<div class="row" style="margin:40px">
    <button type="button" class="btn btn-warning" 
        data-toggle="tooltip" data-placement="right" 
        data-offset="20 0"
        title="Don't click on Warning Button">
        Warning
    </button>
</div>

Note: The output of all the code below is non-static hence, the output is not shown here.




<!-- Removing animation from the tooltip -->
<div class="row" style="margin:40px">
    <button type="button" class="btn btn-warning" 
        data-toggle="tooltip" data-placement="right" 
        data-animation="false"
        title="Don't click on Warning Button">
        Warning
    </button>
</div>




<!-- Delay in tooltip -->
<div class="row" style="margin:40px">
    <button type="button" class="btn btn-warning" 
        data-toggle="tooltip" data-placement="right" 
        data-delay="1000"
        title="Don't click on Warning Button">
        Warning
    </button>
</div>

  1. In the above code we have used the data-delay attribute to delay the tool-tip number assigned to this attribute is in ms i.e tool tip will be delayed for 1000 ms.
  2. We can add different delay time intervals for showing and hiding tool-tip. 

HTML code: 




<!-- Delay in tooltip -->
<div class="row" style="margin:40px">
    <button type="button" class="btn btn-warning" 
        data-toggle="tooltip" data-placement="right"
        title="Don't click on Warning Button">
        Warning
    </button>
</div>

  1. There is no changes in the html code. 

jQuery code: 




<script>
    $(document).ready(function () {
        // jQuery Attribute value selector
        $('[data-toggle="tooltip"]').tooltip({
            delay: { "show": 1000, "hide": 2000 }
        });
    });
</script>




<!-- triggering the  tooltip -->
<div class="row" style="margin:40px">
    <button type="button" class="btn btn-warning" 
        data-toggle="tooltip" data-placement="right" 
        data-trigger="click"
        title="Don't click on Warning Button">
        Warning
    </button>
</div>


Article Tags :