Bootstrap Popover
Description
Bootstrap Popover is created using a custom jQuery plugin. It can be used to display some information about any element.
In this tutorial, you will see how to use Bootstrap Popover and how to customize it with the available options.
What is required
You must reference jQuery, Bootstrap CSS, and two JavaScript files - one for Bootstrap Tooltip and one for Bootstrap Popover.
The JS file for Tooltip is located in the js folder of your Bootstrap folder, named bootstrap-tooltip.js. The JS file for Popover is located in the js folder of your Bootstrap main folder, named bootstrap-popover.js. jQuery is located under docs > assets > js in your Bootstrap main folder, named jquery.js. Alternatively, you can directly visit https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js to download jQuery.
Make sure you download bootstrap-tooltip.js before downloading bootstrap-popover.js. Popover depends on the Tooltip plugin, so the Tooltip plugin needs to be loaded first.
Using Bootstrap Popover in Your Website
Example
View the example online
Explanation
The table below explains the above code. It will help you understand how to use Bootstrap Popover.
| Code | Explanation |
|---|---|
| id="example" | The id assigned to the relevant anchor; the id value points to the JavaScript that will implement the popover later. |
| class="btn btn-danger" | Creates a button. btn btn-danger is the class used in the example. You can use any other class in Bootstrap CSS, or a class you define yourself. |
| data-content="It's so simple to create a tooltip for my website!" | The value of data-content is displayed in the body of the popover. |
| data-original-title="Bootstrap Popover" | The value of data-original-title is displayed as the title of the popover. |
| hover for popover | The anchor text. |
| References jQuery. | |
| References the JS file for Bootstrap Tooltip. | |
| References the JS file for Bootstrap Popover. | |
| $(function () | Prepares the document. jQuery command. |
| $("#example").popover(); | Accesses the id example and implements popover() on it. |
Here we do not create the popover outside the box, without any customization, that is, without using any options in popover().
Usage
So we can summarize the usage of Bootstrap Popover as:
$(function ()
{ $("identifier").popover(options);
});
where identifier is a jQuery selector used to identify the relevant container element. Next, let's look at options what there are.
Option
Below are some options that may be used when customizing the look and feel of a Popover through popover().
animation
The animation value is of type boolean, with a default of true. It is used to give the tooltip a CSS fade transition effect.
placement
The placement value can be of type string or function, with a default of 'right'; top, bottom, and left are the other available values. This option determines the position of the Popover around the anchor text. Here is an example using the placement option.
Example
a {
margin-left : 400px;
}
Creating a popover with the Bootstrap placement option
View the example online
selector
The selector value is of type string, with a default of false. By using this option, you can delegate the Tooltip object to a given target.
trigger
The trigger value can be of type string, with a default of 'hover'; focus and manual are the other available values. This option determines how the tooltip is triggered. The following example demonstrates how to use the focus option to trigger a Popover.
Example
View the example online
title
The title value can be of type string or function, with a default of ''. By default, the value of the title attribute is not displayed.
content
The content value can be of type string or function, with a default of ''. By default, the value of the data-content attribute is not displayed. Here is an example using the title and data-content options. This example also demonstrates how to use multiple options together.
Example
View the example online
delay
The delay value can be of type number or object, with a default of 0. This determines the wait time in milliseconds before showing and hiding the popover. If the value is set to a number, the wait time applies to both showing and hiding. If the value is set to an object, the structure is delay: { show: 500, hide: 100}, where 500 and 100 are in milliseconds.
Changing the Default Markup and Style of the Popover
The default markup of the Popover is located at line 92 of the bootstrap-popover.js file. It is as follows:
template: ''
You can change it and put your own markup there.
The default style of the Popover is located at lines 3027 to 3118. You can modify it and put your own styles there.
Click here to download all the HTML, CSS, JS, and image files used in this tutorial.