Bootstrap

How to Create and Position a Tooltip in Bootstrap 5

Tooltip is a small popup box that appears when a user move the cursor over a button or a link which give the user, knowledge or a hint about that specific button or link. Tooltips are useful for the new users of a website to save them from confusion or any kind of a problem while exploring your website.

This article will teach you about

How to create tooltip

To create a tooltip use, data attribute “data-bs-toggle=”tooltip”  in your <a> tag or <button> tag and the text which is shown in tooltip must be written using the title attribute.

<body>
  <div class="container" style="margin-top: 15px;">
  <div class="row">
      <div class="col-lg">
        <h2>Tooltip with Link</h2>
        <br>
        <a href="#" data-bs-toggle="tooltip" title="This tooltip is created for link">Place cursor here</a>
      </div>
      <div class="col-lg">
        <h1>Tooltip with button</h1>
        <br>
        <button type="button" class="btn btn-primary" data-bs-toggle="tooltip" title="This tooltip is created for button">Place cursor here</button>
      </div>
    </div>
  </div>

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


  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" ></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
</body>

Now let me tell you that in bootstrap 5 to create a tooltip we need to add data attributes in <a> tag or <button> tag and also write a jquery to enable it. So I break the creation process into steps which will give you better understanding.

Steps

Creating a tooltip in bootstrap 5 is a simple two step process

Step 1: Add data-bs-toggle=”tooltip” and title=”Tooltip text goes here” attributes in your <button> or <a> tag.

<div class="col-lg">
        <h2>Tooltip with Link</h2>
        <br>
        <a href="#" data-bs-toggle="tooltip" title="This tooltip is created for link">Place cursor here</a>
      </div>
      <div class="col-lg">
        <h2>Tooltip with button</h2>
        <br>
        <button type="button" class="btn btn-primary" data-bs-toggle="tooltip" title="This tooltip is created for button">Place cursor here</button>
      </div>

Step 2: Enable tooltips by writing jquery following code

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

Positioning of tooltips

So to position a tooltip as your requirement just add data-bs-placement=”top/right/left/bottom” attribute to your <a> or <button> tag to change tooltip position.

<div class="container" style="margin-top: 30px;">
    <button  class="btn btn-primary" data-bs-toggle="tooltip" data-bs-placement="top" title="Top position tooltip">Top</button>
    <button class="btn btn-primary" data-bs-toggle="tooltip" data-bs-placement="right" title="Right position tooltip">Right</button>
    <button  class="btn btn-primary" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Bottom position tooltip">bottom</button>
    <button  class="btn btn-primary" data-bs-toggle="tooltip" data-bs-placement="left" title="Left position tooltip">left</button>
  </div>

Conclusion

Tooltips are created by adding data-bs-toggle=”tooltip” and title=”Tooltip text goes here” attributes in <button> or <a> tag and enable it by writing a javascript code. For positioning just add data-bs-placement=”top/bottom/right/left” attribute to <button> or <a> tag.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.