Open In App

Bootstrap 5 Tooltips Usage

Bootstrap 5 Tooltips usage is used to create the markup and the text on the elements when required, and the tooltip created is placed after the element that was triggered.

Using Javascript to launch Tooltips:



var example = document.getElementById('...')
var ... = new bootstrap.Tooltip(example, value)

Bootstrap 5 Tooltip usage :

Example 1: In this example, we will have a disabled element with a tooltip placed on the top and a button with a tooltip placed on the left of the element.






<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1">
    <link href=
          rel="stylesheet">
    <script src=
    </script>
</head>
  
<body class="m-5">
    <center>
        <h1 class="text-success">
            GeeksforGeeks
        </h1>
        <strong>Bootstrap 5 Tooltips Usage</strong>
        <br />
        <span class="d-inline-block" 
              data-bs-toggle="tooltip" 
              title="Disabled tooltip" tabindex="0">
            <button class="btn btn-primary" 
                    type="button" disabled>
                    Disabled button
            </button>
        </span>
        <button type="button" title="tooltip"
                class="btn btn-primary"  
                data-bs-toggle="tooltip"
                Button 
        </button>
    </center>
    <script type="text/javascript">
        var tooltipTriggerList = 
        [].slice.call(document.querySelectorAll
                      ('[data-bs-toggle="tooltip"]'))
        var tooltipList = 
            tooltipTriggerList.map(function(tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl)
        })  
    </script>
</body>
</html>

Output:

 

Example 2: In this example, we will demonstrate bootstrap tooltip methods like show, hide, toggle, and destroy.




<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
</head>
  
<body class="m-5">
    <div class="container">
        <center>
            <h1 class="text-success">
            GeeksforGeeks
        </h1>
        <strong>Bootstrap 5 Tooltips Usage</strong>
        <br />
            <button class="btn btn-primary" 
                    data-toggle="tooltip" 
                    title="tooltip!">
                    Tooltip Example
            </button>
            <div>
                <p>
                    Click on the buttons to manually 
                    control the tooltip above:
                </p>
                <button type="button" 
                        class="btn btn-primary">
                        Show
                </button>
                <button type="button" 
                        class="btn btn-warning">
                        Hide
                </button>
                <button type="button" 
                        class="btn btn-success">
                        Toggle
                </button>
                <button type="button" 
                        class="btn btn-danger">
                        Destroy
                </button>
            </div>
        </center>
    </div>
  
    <script>
        $(document).ready(function () {
            $(".btn-primary").click(function () {
                $("[data-toggle='tooltip']").tooltip('show');
            });
            $(".btn-warning").click(function () {
                $("[data-toggle='tooltip']").tooltip('hide');
            });
            $(".btn-success").click(function () {
                $("[data-toggle='tooltip']").tooltip('toggle');
            });
            $(".btn-danger").click(function () {
                $("[data-toggle='tooltip']").tooltip('destroy');
            });
        });
    </script>
</body>
</html>

Output:

 

Reference: https://getbootstrap.com/docs/5.0/components/tooltips/#usage


Article Tags :