Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap Modals

Bootstrap Modals

Description

Bootstrap Modals are created using a custom jQuery plugin. They can be used to create modal windows to enrich the user experience or add useful functionality for users. You can use Popovers and Tooltips inside Modals.

In this tutorial, we will discuss how to create modal windows with Bootstrap through examples and explanations. We will also discuss the various options available for customization.

What is required

You need jQuery, Bootstrap CSS, and the JavaScript file bootstrap-modal.js. This js file is located in the js folder of the Bootstrap main folder you downloaded.

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.

What Bootstrap Modals Look Like

The following example demonstrates what Bootstrap Modals look like.

Bootstrap Modals

Using Bootstrap Modals in Your Website

The following example demonstrates how to use Bootstrap Modals in a web page. Note that you do not need to write any JavaScript code. An explanation is provided after the example.

Example

    

Creating a Modal with Bootstrap

    

    

Launch demo modal

View the Bootstrap Modals example online

Explanation

The table below explains the above code. It will help you understand how to use Bootstrap Modals.

Code Explanation
div id="example" The id assigned to the relevant div; the id value points to the JavaScript that will implement the modal later.
class="modal hide fade in" Four Bootstrap CSS classes - modal, hide, fade, and in - used to set the layout of the modal.
style="display: none; Used to keep the modal window visible until the trigger is fired (such as clicking the relevant button).
modal-header is the class used to define the style of the modal window header.
a class="close" The CSS class close is used to style the modal window's close button.
data-dismiss="modal" data-dismiss is a custom HTML5 data attribute used to close the modal window.
class="modal-body" modal-body is a Bootstrap CSS class used to style the body of the modal window.
class="modal-footer" modal-footer is a Bootstrap CSS class used to style the footer of the modal window.
class="btn btn-success" The CSS classes btn and btn-success are used to create a large button in the footer of the modal window. You can use any other Bootstrap button instead.
class="btn" The Bootstrap CSS button class btn is used to create a small button in the footer of the modal window.
data-dismiss="modal" The custom HTML5 data attribute data-dismiss is used to close the modal window.
data-toggle="modal" The custom HTML5 data attribute data-toggle is used to open the modal window.
class="btn btn-primary btn-large" Sets the button style; clicking the button creates the modal window.