How To Implement Bootstrap 4 Popover

This article demonstrate Bootstrap 4 Popover. The Popover component is similar to tooltips. It is a pop-up box that appears when the user clicks on an element. The difference is that the popover can contain much more content. To create a popover, add the data-toggle="popover" attribute to an element. Use the title attribute to specify the header text of the popover, and use the data-content attribute to specify the text that should be displayed inside the popover's body.

Introduction

This article demonstrates Bootstrap 4 Popover. The Popover component is similar to tooltips. It is a pop-up box that appears when the user clicks on an element. The difference is that the popover can contain much more content. To create a popover, add the data-toggle="popover" attribute to an element. Use the title attribute to specify the header text of the popover, and use the data-content attribute to specify the text that should be displayed inside the popover's body.

Important points you need to remember before using Bootstrap 4 popover

  1. Popovers rely on the 3rd party library Popper.js for positioning. You must include popper.min.js before bootstrap.js or use bootstrap.bundle.min.js / bootstrap.bundle.which contains Popper.js in order for popovers to work!
  2. Popovers require the tooltip plugin as a dependency.
  3. If you’re building our JavaScript from source, it requires util.js.
  4. Popovers are opt-in for performance reasons, so you must initialize them yourself.
  5. Zero-length title and content values will never show a popover.
  6. Specify container: 'body' to avoid rendering problems in more complex components (like our input groups, button groups, etc.).
  7. Triggering popovers on hidden elements will not work.
  8. Popovers for .disabled or disabled elements must be triggered on a wrapper element.
  9. When triggered from anchors that wrap across multiple lines, popovers will be centered between the anchors’ overall width. Use white-space: nowrap; on your <a>s to avoid this behavior.
  10. Popovers must be hidden before their corresponding elements have been removed from the DOM.
Enable via data-* Attributes

The data-toggle="popover" activates the popover.
The title attribute specifies the header text of the popover.
The data-content attribute specifies the text that should be displayed inside the popover's body.

Note
Popovers must be initialized with jQuery: select the specified element and call the popover() method.

The following code will enable all popovers in the document,

  1. <script type="text/javascript">  
  2.         $(document).ready(function () {  
  3.             $('[data-toggle="popover"]').popover();  
  4.         });  
  5. </script>  

To use a Bootstrap 4 popover in your project, you need to have the following downloaded or cdn link scripts.

  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">  
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  3. <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>  
  4. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>  

Example for Bootstrap 4 Popover

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Popover</title>  
  5.     <meta charset="utf-8" />  
  6.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">  
  7.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  8.     <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>  
  9.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>  
  10.     <script type="text/javascript">  
  11.         $(document).ready(function () {  
  12.             $('[data-toggle="popover"]').popover();  
  13.         });  
  14.     </script>  
  15. </head>  
  16. <body>  
  17.     <div class="container py-5">  
  18.         <h4 class="text-center text-uppercase">Bootstrap 4 Popover </h4>   
  19.         <br />  
  20.         <a href="#" data-toggle="popover" title="Introduction" data-content="My name is Farhan Ahmed. I am full-stack developer." >What is your name?</a>     
  21.     </div>  
  22. </body>  
  23. </html>  

Output

Implement Bootstrap 4 Popover Output

Popover Positioning

By default, the popover will appear on the right side of the element.

Use the data-placement attribute to set the position of the popover on top, bottom, left or the right side of the element,

Example for Bootstrap 4 Popover Positioning

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Popover</title>  
  5.     <meta charset="utf-8" />  
  6.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">  
  7.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  8.     <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>  
  9.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>  
  10.     <script type="text/javascript">  
  11.         $(document).ready(function () {  
  12.             $('[data-toggle="popover"]').popover();  
  13.         });  
  14.     </script>  
  15. </head>  
  16. <body>  
  17.     <div class="container py-5">  
  18.         <h4 class="text-center text-uppercase">Bootstrap 4 Popover Positioning </h4>   
  19.         <div class="row py-5">  
  20.             <div class="col-md-3">  
  21.                 <a href="#" title="Header" data-toggle="popover" data-placement="top" data-content="Content">Top</a>  
  22.             </div>  
  23.             <div class="col-md-3">  
  24.                 <a href="#" title="Header" data-toggle="popover" data-placement="bottom" data-content="Content">Bottom</a>  
  25.             </div>  
  26.             <div class="col-md-3">  
  27.                 <a href="#" title="Header" data-toggle="popover" data-placement="left" data-content="Content">Left</a>  
  28.             </div>  
  29.             <div class="col-md-3">  
  30.                 <a href="#" title="Header" data-toggle="popover" data-placement="right" data-content="Content">Right</a>  
  31.             </div>  
  32.         </div>       
  33.     </div>  
  34. </body>  
  35. </html>  

Output

Implement Bootstrap 4 Popover Output 
Closing Popovers
 
By default, the popover is closed when you click on the element again. However, you can use the data-trigger="focus" attribute which will close the popover when clicking outside the element.

Example of closing popover

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Popover</title>  
  5.     <meta charset="utf-8" />  
  6.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">  
  7.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  8.     <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>  
  9.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>  
  10.     <script type="text/javascript">  
  11.         $(document).ready(function () {  
  12.             $('[data-toggle="popover"]').popover();  
  13.         });  
  14.     </script>  
  15. </head>  
  16. <body>  
  17.     <div class="container py-5">  
  18.         <h4 class="text-center text-uppercase">Bootstrap 4 Popover Closing </h4>  
  19.         <br />  
  20.         <a href="#" title="Dismissible popover" data-toggle="popover" data-trigger="focus" data-content="Click anywhere in the document to close this popover">Click me</a>  
  21.     </div>  
  22. </body>  
  23. </html>  

Output

 Implement Bootstrap 4 Popover Output
If you want the popover to be displayed when you move the mouse pointer over the element, use the data-trigger attribute with a value of "hover".

Example for mouse hover popover
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Popover</title>  
  5.     <meta charset="utf-8" />  
  6.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">  
  7.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  8.     <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>  
  9.     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>  
  10.     <script type="text/javascript">  
  11.         $(document).ready(function () {  
  12.             $('[data-toggle="popover"]').popover();  
  13.         });  
  14.     </script>  
  15. </head>  
  16. <body>  
  17.     <div class="container py-5">  
  18.         <h4 class="text-center text-uppercase">Bootstrap 4 Popover Closing </h4>  
  19.         <br />  
  20.         <a href="#" title="Header" data-toggle="popover" data-trigger="hover" data-content="I am mouse hover popover">Hover over me</a>  
  21.     </div>  
  22. </body>  
  23. </html>  

Output

 Implement Bootstrap 4 Popover Output