Understanding Bootstrap Modals

原文出处 Understanding Bootstrap Modals

Bootstrap CSS framework dialog component

Interested in mastering Bootstrap 3? You might like Syed Fazle Rahman’s book Jump Start Bootstrap. Buy the ebook for $19 or sign up as a Premium member and get it as part of your subscription. If you’d like to have a head start on the upcoming Bootstrap 4 CSS framework, a SitePoint Premium membership will give you access to Bootstrap 4 Features, a video course by Sandy Ludosky.

Update of 2014 article to make it compatible with latest version of the Bootstrap CSS framework.

In my previous tutorial, I explained how powerful Bootstrap 3 CSS framework is for a novice designers. It ships with some of the best ready-to-use JavaScript and jQuery components and plugins. The framework has reached version 3.3.7 and it’s getting ready for the release of v.4 beta, which will bring about several important and necessary changes.

In this tutorial we will be talking about one of the most useful jQuery Bootstrap plugins, The Modal.

The Bootstrap Modal is a lightweight multi-purpose JavaScript popup that is customizable and responsive. It can be used to display alert popups, videos, and images in a website. Websites built with Bootstrap can use the modal to showcase (for example) terms and conditions (as part of a signup process), videos (similar to a standard light box), or even social media widgets.

Now let’s examine the different parts of Bootstrap’s modal, so we can understand it better.

The Bootstrap Modal is divided into three primary sections: the header, body, and footer. Each has its own significance and hence should be used properly. We’ll discuss these shortly. The most exciting thing about Bootstrap’s modal? You don’t have to write a single line of JavaScript to use it! All the code and styles are predefined by Bootstrap. All that’s required is that you use the proper markup and the attributes to trigger it.

### More from this author

The Default Modal

The default Bootstrap Modal looks like this:

Default Bootstrap modal

To trigger the modal, you’ll need to include a link or a button. The markup for the trigger element might look like this:

Click to open Modal

Notice the link element has two custom data attributes: data-toggle and data-target. The toggle tells Bootstrap what to do and the target tells Bootstrap which element is going to open. So whenever a link like that is clicked, a modal with an id of “basicModal” will appear.

Now let’s see the code required to define the modal itself. Here is the markup:

            Modal title

                Modal Body

                Save changes

The parent div of the modal should have the same id as used in the trigger element above. In our case it would be id="basicModal".

Note: Custom attributes like aria-labelledby and aria-hidden in the parent modal element are used for accessibility. It is a good practice to make your website accessible to all, so you should include these attributes since they won’t negatively affect the standard functionality of the modal.

In the modal’s HTML, we can see a wrapper div nested inside the parent modal div. This div has a class of modal-content that tells bootstrap.js where to look for the contents of the modal. Inside this div, we need to place the three sections I mentioned earlier: the header, body, and footer.

The modal header, as the name implies, is used to give the modal a title and some other elements like the “x” close button. This should have a data-dismiss attribute that tells Bootstrap which element to hide.

Then we have the modal body, a sibling div of the modal header. Consider the body an open canvas to play with. You can add any kind of data inside the body, including a YouTube video embed, an image, or just about anything else.

Lastly, we have the modal footer. This area is by default right aligned. In this area you could place action buttons like “Save”, “Close”, “Accept”, etc., that are associated with the action the modal is displaying.

Now we are done with our first modal! You can check it out on our demo page.

Changing the Modal’s Size

Earlier I mentioned that the Bootstrap modal is responsive and flexible. We will see how to change its size in this section.

The modal comes in two new flavors in Bootstrap 3.3.7: Large and Small. Add a modifier class modal-lg to the modal-dialog div for a larger modal or modal-sm for a smaller modal.

Activating the Modal with jQuery

The modal is a jQuery plugin, so if you want to control the modal using jQuery, then you need to call the .modal() function on the modal’s selector. For Example:


The “options” here would be a JavaScript object that can be passed to customize the behaviour. For example:

var options = {
    "backdrop" : "static"

Available options include:

  • backdrop: This can be either true or static. This defines whether or not you want the user to be able to close the modal by clicking the background.

  • keyboard: if set to true then the modal will close via the ESC key.

  • show: Used for opening and closing the modal. It can be either true or false.

  • remote: This is one of the coolest options. It can be used to load remote content using jQuery’s load() method. You need to specify an external page in this option. It is set to false by default.

Bootstrap Modal’s Events

You can further customize the normal behavior of the Bootstrap modal by using various events that are triggered while opening and closing the modal. These events have to be bound using jQuery’s .on() method.

Various events available are:

  • show.bs.modal: fired just before the modal is open.

  • shown.bs.modal: fired after the modal is shown.

  • hide.bs.modal: fired just before the modal is hidden.

  • hidden.bs.modal: fired after the modal is closed.

  • loaded.bs.modal: fired when remote content is successfully loaded in the modal’s content area using the remote option mentioned above.

You can use one of the above events like this:

$('#basicModal').on('shown.bs.modal', function (e) {
    alert('Modal is successfully shown!');

Loading Remote Content in the Modal

There are three different ways to load remote content inside a Bootstrap modal.

The first way is by using the remote option inside the options object, as mentioned above. The other two ways are done without JavaScript, as shown below.

You can provide a value to the href attribute inside the modal’s trigger element. In our case, the trigger is a link. For example, instead of the # value we included earlier, we can include a URL to a specific page:

Click to open Modal

You can also provide a custom data attribute of data-remote to the trigger element instead of using the href attribute. For example:

Click to open Modal


The modal is one of the best plugins offered by Bootstrap 3. For a novice designer, it is one of the best ways to load content inside a popup screen without writing any JavaScript.

Read Bootstrap 3 – JavaScript Components for more JavaScript plugins available from Bootstrap and if you’re new to Bootstrap, you can check out my Bootstrap 3 Tutorial.

Below is a CodePen demo with 3 example modals.

See the Pen Bootstrap 3.1.0 Modal Demos by SitePoint (@SitePoint) on CodePen.

Download Bootstrap modal demo