Bootstrap Modals

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Bootstrap Modals

In this tutorial you will learn how to create modals with Bootstrap.

Creating Modals with Bootstrap

Modal is basically a dialog box or popup window that is used to provide important information to the user or prompt user to take necessary actions before moving on. Modals are widely used to warn users for situations like session time out or to receive their final confirmation before going to perform any critical actions such as saving or deleting important data.

You can easily create very smart and flexible dialog boxes with the Bootstrap modal plugin. The following example will show you how to create a simple modal with a header, message body and the footer containing action buttons for the user.

Example

  •  id="myModal" class="modal fade">
  •      class="modal-dialog">
  •          class="modal-content">
  •              class="modal-header">
  •                  type="button" class="close" data-dismiss="modal" aria-hidden="true">×
  •                  class="modal-title">Confirmation
  • 
     
  •              class="modal-body">
  • 
     

    Do you want to save changes you made to document before closing?

  •                  class="text-warning">If you don't save, your changes will be lost.

     

  • 
     
  •              class="modal-footer">
  •                  type="button" class="btn btn-default" data-dismiss="modal">Close
  •                  type="button" class="btn btn-primary">Save changes
  • 
     
  • 
     
  • 
     
  • 
     

— The above example launches the modal window when the DOM is fully loaded via JavaScript. The output will look something like this:

Bootstrap Modal Popup

 

Tip:Always try to place your modal HTML in a top-level position in your document, preferably before closing of the

tag (i.e. ) to avoid interference from other elements, otherwise it may affect modal's appearance or functionality.


Activate Modals via Data Attributes

You can activate a Bootstrap modal by clicking on the button or link via data attributes without writing any JavaScript code. See the following example:

Example

  •  href="#myModal" role="button" class="btn btn-large btn-primary" data-toggle="modal">Launch Demo Modal
  • 
     
  •  id="myModal" class="modal fade">
  •      class="modal-dialog">
  •          class="modal-content">
  •              class="modal-header">
  •                  type="button" class="close" data-dismiss="modal" aria-hidden="true">×
  •                  class="modal-title">Confirmation
  • 
     
  •              class="modal-body">
  • 
     

    Do you want to save changes you made to document before closing?

  •                  class="text-warning">If you don't save, your changes will be lost.

     

  • 
     
  •              class="modal-footer">
  •                  type="button" class="btn btn-default" data-dismiss="modal">Close
  •                  type="button" class="btn btn-primary">Save changes
  • 
     
  • 
     
  • 
     
  • 
     

The above example launches the modal window on click of the "Launch Demo Modal" button. Let's go through each part of this modal code one by one for a better understanding.

Explanation of Code

To activate a Bootstrap modal via data attributes we basically need two components — the controller element like a button or link, and the modal element itself.

  • The outermost container of every modal in a document must have a unique id (in this case id="myModal", line no-5), so that it can be targeted via data-target (for buttons) or href (for hyperlinks) attribute of the controller element (line no-2).

  • The attribute data-toggle="modal" is required to add on the controller element (line no-2), like a button or an anchor, along with a attribute data-target="#myModal" or href="#myModal" to target a specific modal to toggle.

  • The .modal-dialog class (line no-6) sets the width as well as horizontal and vertical alignment of the modal box. Whereas the class .modal-content sets the styles like text and background color, borders, rounded corners etc.

Rest of the thing is self explanatory, such as the .modal-header element defines a header for the modal that usually contains a modal title and a close button, whereas the .modal-body element contains the actual content like text, images, forms etc. and the .modal-footer element defines the footer that typically contains action buttons for the user.

 

Note:The .fade class on the .modal element adds a fading and sliding animation effect while showing and hiding the modal window. If you want the modal that simply appear without any effect you can just remove this class.


Activate Modals via JavaScript

You may also activate a Bootstrap modal window via JavaScript — just call the modal() Bootstrap method with the modal id or class selector in your JavaScript code.

Example

  •  

Changing the Size of Modals

Bootstrap gives you option further to scaling a modal up or down. You can make modals larger or smaller by adding an extra class .modal-lg or .modal-sm on .modal-dialog.

Example

  •  class="btn btn-primary" data-toggle="modal" data-target="#largeModal">Large modal
  • 
     
  •  id="largeModal" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog">
  •      class="modal-dialog modal-lg">
  •          class="modal-content">
  •              class="modal-header">
  •                  type="button" class="close" data-dismiss="modal" aria-hidden="true">×
  •                  class="modal-title">Large Modal
  • 
     
  •              class="modal-body">
  • 
     

    Add the .modal-lg class on .modal-dialog to create this large modal.

  • 
     
  •              class="modal-footer">
  •                  type="button" class="btn btn-default" data-dismiss="modal">Cancel
  •                  type="button" class="btn btn-primary">OK
  • 
     
  • 
     
  • 
     
  • 
     
  • 
     
  •  class="btn btn-primary" data-toggle="modal" data-target="#smallModal">Small modal
  • 
     
  •  id="smallModal" class="modal fade" tabindex="-1" role="dialog">
  •      class="modal-dialog modal-sm">
  •          class="modal-content">
  •              class="modal-header">
  •                  type="button" class="close" data-dismiss="modal" aria-hidden="true">×
  •                  class="modal-title">Small Modal
  • 
     
  •              class="modal-body">
  • 
     

    Add the .modal-sm class on .modal-dialog to create this small modal.

  • 
     
  •              class="modal-footer">
  •                  type="button" class="btn btn-default" data-dismiss="modal">Cancel
  •                  type="button" class="btn btn-primary">OK
  • 
     
  • 
     
  • 
     
  • 
     

Changing Modal Content Based on Trigger Button

Often several modal on a web page has almost same content with minor differences.

You can use the modal events to create slightly different modal windows based on the same modal HTML. The following example will change the title of the modal window according to the trigger button's data-title attribute value.

Example

  •  

Options

There are certain options which can be passed to modal() Bootstrap method to customize the functionality of a modal. Options can be passed via data attributes or JavaScript.

For setting the modals options via data attributes, just append the option name to data-, like data-backdrop="static", data-keyboard="false" etc.

Name

Type

Default Value

Description

backdrop

boolean
or the string static

true

Includes a modal-backdrop (black overlay area) element. Alternatively, you may specify static for a backdrop which doesn't close the modal on click.

keyboard

boolean

true

Closes the modal window on press of escape key.

show

boolean

true

Shows the modal when initialized or activate.

remote

URL

false

Deprecated If a remote url is provided, content will be loaded one time via jQuery's load() method and injected into the .modal-content div.

Data attributes provides an easy way for setting the modal options, however JavaScript is the more preferable way as it prevents you from repetitive work. See the .modal(options) method in the section below to know how to set the options for modals using JavaScript.

If you're using the data api for setting the options for modal window, you may alternatively use the href attribute to provide the URL of remote source, like this:

Example

  •  href="remote.html" role="button" class="btn btn-large btn-primary" data-toggle="modal" data-target="#myModal">Launch Demo Modal
  • 
     
  •  id="myModal" class="modal fade">
  •      class="modal-dialog">
  •          class="modal-content">
  •             
  • 
     
  • 
     
  • 
     

Note:The remote option for the Bootstrap modals is deprecated since v3.3.0 and will be removed in upcoming v4. Use the client-side templating or a data binding framework instead, or call the jQuery load() method yourself.

Methods

These are the standard bootstrap's modals methods:

.modal(options)

This method activates the content as a modal. It also allows you to set options for them.

The jQuery code in the following example will prevent the modal from closing when a user clicks on the backdrop i.e. black overlay area behind the modal.

Example

  •  

The following jQuery code will prevent the modal from closing on press of the escape key.

Example

  •   

The jQuery code in the following example will create a modal in which content of the modal will be inserted from a remote file upon activation.

Example

  •   

.modal('toggle')

This method toggles a modal window manually.

Example

  •  

.modal('show')

This method can be used to open a modal window manually.

Example

  •  

.modal('hide')

This method can be used to hide a modal window manually.

Example

  •  

.modal('handleUpdate')

This method readjusts the modal's position to counter the jerk that is occurring due to the appearance of the viewport scrollbar in case if the modal height changes in such a way that it becomes higher than the viewport height while it is open.

A common example of this scenario is showing the hidden elements inside the modal via JavaScript or loading content inside the modal using Ajax after activation.

Example

  •  

Events

Bootstrap's modal class includes few events for hooking into modal functionality.

Event

Description

show.bs.modal

This event fires immediately when the show instance method is called.

shown.bs.modal

This event is fired when the modal has been made visible to the user. It will wait until the CSS transition process has been fully completed before getting fired.

hide.bs.modal

This event is fired immediately when the hide instance method has been called.

hidden.bs.modal

This event is fired when the modal has finished being hidden from the user. It will wait until the CSS transition process has been fully completed before getting fired.

loaded.bs.modal

This event is fired when the modal has loaded content using the remote option.

The following example displays an alert message to the user when fade out transition of the modal window has been fully completed.

Example

  •  

Tip:See also the Bootstrap FAQ section for more examples on modals, like setting vertical alignment, changing default width, embedding video, etc.