Bootstrap Modal Plugin

Bootstrap Modal Plugin is just like an overlay div or you can say it a child window. The purpose of model is without leaving the current or parent window to display the content from other source. That information, interaction and more things can provide this child window.

If you want to implement functionality of this plugin individually, simple include the modal.js file along the other JS files. Else as we have discussed in the tutorial of Bootstrap Plugin Overview, you can include bootstrap.js or the minified bootstrap.min.js.


You can toggle the modal plugin’s hidden content via data attributes and via javascript

Via data attributes

Set attribute data-toggle=”modal” on a controller element, like a button or link, along with a data-target=”#identifier” or href=”#identifier” to target a specific modal (with the id=”identifier”) to toggle.

Via JavaScript

You can call a modal with id="myModal" with a single line of JavaScript using this technique.

Details of the preceding code:

  • To invoke the modal window, you need to have some kind of a trigger. You can use a button or a link. Here we have used button.

  • If you look in the code above, you will see that in the <button> tag, the data-target="#myModal" is the target of the modal that you want to load on the page. This code allows you to create multiple modals on the page and then have different triggers for
    each of them. Now, to be clear, you don’t load multiple modals at the same time, but you can create many on the page to be loaded at different times.

  • There are two classes to take note of in the modal:

    • The first is .modal, which is simply identifying the content of the <div> as a modal.

    • And second is the .fade class. When the modal is toggled, it will cause the content to fade in and out.

  • aria-labelledby="myModalLabel", attribute reference the modal title.

  • The attribute aria-hidden="true" is used to keep the Modal Window invisible till a trigger comes (like a click on the associated button).

  • <div class="modal-header">, modal-header is the class to define style for the header of the modal window.

  • class="close", is a CSS class close that sets style for the Close button of the modal window.

  • data-dismiss="modal", is a custom HTML5 data attribute. Here it is used to close the modal window.

  • class="modal-body", is a CSS class of Bootstrap CSS to set style for body of the modal window.

  • class="modal-footer", is a CSS class of Bootstrap CSS to set style for footer of the modal window.

  • data-toggle="modal", HTML5 custom data attribute data-toggle is used to open the modal window.

Static Example

A rendered modal with header, body, and set of actions in the footer.

Live example with transition

A static modal window example with transition is as shown in the following example:

Optional sizes

Modals have two optional sizes, available via modifier classes to be placed on a .modal-dialog.

Remove animation

For modals that simply appear rather than fade in to view, remove the .fade class from your modal markup.


Modal plugin has some options which can be passed via data attributes or JavaScript to modify the face of Modal Window. Below we are listing options in table:

Option Name Type/Default Value Default Description
backdrop boolean or the string ‘static’ true

If you do not requirement to close the modal when click outside the modal, specify static for backdrop.

keyboard boolean true Closes the modal when escape key is pressed
show boolean true Shows the modal when initialized.
remote path true

Insert content into the model body .modal-content div using .load method of jQuery. If a remote and valid url add in href attribute, content will be loaded there. An example of this is shown below:


Options: .modal(options)

Trigger you content as a modal with some useful methods which can be used with modal().

Toggle: .modal(‘toggle’)

Manually toggles a modal

Show: .modal(‘show’)

Manually opens a modal

Hide: .modal(‘hide’)

Manually hides a modal


The following example demonstrates the usage of methods:

Just click the Close button to exit the modal window.


Below the description of events is listed which hooking into modal functionality.

Triggered after the show method is called.

Triggered when the modal has been made visible to the user (will wait for CSS transitions to complete).

Triggered when the hide instance method has been called.

Triggered when the modal has finished being hidden from the user.


The following example shows the usage of events:

Like seen in the above demo, after click on the Close button i.e. hide event, an alert message is displayed.

Leave a Reply

Your email address will not be published. Required fields are marked *