Open In App

Popovers in bootstrap with examples

Last Updated : 28 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A Bootstrap Popover is an attribute in bootstrap that can be used to make any website look more dynamic. Popovers are generally used to display additional information about any element and are displayed with a click of a mouse pointer over that element. In the popover, if you click on any element that you include in your script, it will give a particular message as a popover and you can see your message as you defined in the script. 

It is easy to implement popovers on a website using Bootstrap as you just need to define a few attributes for the element as described below:

Syntax

data-toggle="popover" 
title="Popover Header" 
data-content="Some content inside the box"

The data-toggle attribute defines the Popover, the title attribute defines the Tile for the Popover and the data-content attribute is used to store the content to be displayed in the respective Popover.

Include the below javascript in your code to make it work.

Javascript




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


Example:

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Bootstrap Example</title>
 
    <!-- Link Bootstrap CSS -->
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
</head>
<body>
    <div class="container">
        <h3>Popover Example</h3>
        <a href="#" data-toggle="popover" title="Popover Header"
            data-content="Some content inside the popover">
            GeeksforGeeks</a>
    </div>
    <script>
        $(document).ready(function () {
            $('[data-toggle="popover"]').popover();
        });
    </script>
</body>
</html>


Output:


Different types of Popover orientation in Bootstrap:

  • Top Alignment: In this type of popover alignment, the popover content is displayed at the top of the element on which we have applied this attribute. To align the popover to the top, assign an attribute data-placement = “top”.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Bootstrap Example</title>
    <!-- Bootstrap CSS and JS -->
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
</head>
<body>
    <div class="container">
        <h3>Popover Example</h3>
        <ul class="list-inline">
            <li><a href="#" title="Header" data-toggle="popover"
                    data-placement="top" data-content="Content">
                    GeeksforGeeks
                </a>
            </li>
        </ul>
    </div>
    <script>
        $(document).ready(function () {
            $('[data-toggle="popover"]').popover();
        });
    </script>
</body>
</html>


Output:

  • Left Alignment: In this type of popover alignment, the popover content is displayed at the left of the element on which we have applied this attribute. To align the popover to the top, assign an attribute data-placement = “left”.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Bootstrap Example</title>
    <!-- Bootstrap CSS and JS -->
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
</head>
<body>
    <div class="container">
        <h3>Popover Example</h3>
        <ul class="list-inline">
            <li><a href="#" title="Header" data-toggle="popover"
                    data-placement="left" data-content="Content">
                    GeeksforGeeks
                </a>
            </li>
        </ul>
    </div>
    <script>
        $(document).ready(function () {
            $('[data-toggle="popover"]').popover();
        });
    </script>
</body>
</html>


Output:

  • Right Alignment: In this type of popover alignment, the popover content is displayed at the top of the element on which we have applied this attribute. To align the popover to the top, assign an attribute data-placement = “right”.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Bootstrap Example</title>
    <!-- Bootstrap CSS and JS -->
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
</head>
<body>
    <div class="container">
        <h3>Popover Example</h3>
        <ul class="list-inline">
            <li><a href="#" title="Header" data-toggle="popover"
                    data-placement="right" data-content="Content">
                    GeeksforGeeks
                </a>
            </li>
        </ul>
    </div>
    <script>
        $(document).ready(function () {
            $('[data-toggle="popover"]').popover();
        });
    </script>
</body>
</html>


Output:

  • Bottom Alignment: In this type of popover alignment, the popover content is displayed at the bottom of the element on which we have applied this attribute. To align the popover to the top, assign an attribute data-placement = “bottom”.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Bootstrap Example</title>
    <!-- Bootstrap CSS and JS -->
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
</head>
<body>
    <div class="container">
        <h3>Popover Example</h3>
        <ul class="list-inline">
            <li><a href="#" title="Header" data-toggle="popover"
                    data-placement="bottom" data-content="Content">
                    GeeksforGeeks
                </a>
            </li>
        </ul>
    </div>
    <script>
        $(document).ready(function () {
            $('[data-toggle="popover"]').popover();
        });
    </script>
</body>
</html>


Output:


Supported Browser:

  • Google Chrome
  • Microsoft Edge
  • Firefox
  • Opera
  • Safari


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads