Bootstrap Modals offer a lightweight, multi-purpose JavaScript popup to add dialogs to yours. Learn how to customize Bootstrap Modals easily. Multiple examples and tutorial.
Overview
Bootstrap modals are lightweight and multi-purpose popups. Modals are split into three primary sections: header, body, and footer. Each has its role and so should be used accordingly.
Examples
Modal components
Below is a basic modal example. Included are the header, body , and modal footer.
Modal title
Modal body text goes here.
Live preview
Toggle a working modal by clicking the following button. It will slide down and fade in from the top of the site.
Modal title
Woohoo, you're reading this text in a modal!
Scrolling long content
While modals become too long for the user’s viewport or device, they scroll separately of the page itself.
Modal title
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce consequat, purus at luctus pulvinar, sapien libero scelerisque neque, sed bibendum ex dui at felis. Integer sed imperdiet nibh, non eleifend lectus. Sed interdum, tortor et rutrum elementum, nisi risus venenatis risus, eget dictum ligula lacus et magna. Nullam pharetra enim quis velit pellentesque euismod. Integer nec neque massa. Maecenas sollicitudin id justo id varius. Maecenas vel nisl eget velit aliquam facilisis. Aenean luctus placerat ornare. Cras congue dolor et enim molestie rhoncus. Proin volutpat, augue eget luctus scelerisque, orci nisi mollis eros, ut volutpat ipsum justo ac tortor.
Curabitur eget arcu quam. Nullam blandit justo eu commodo mollis. Quisque hendrerit orci vitae ante faucibus molestie. Praesent maximus et dolor eu condimentum. Praesent egestas est eu sem scelerisque tempor. Curabitur posuere metus vitae risus ultricies fermentum. Nulla fermentum, elit ac maximus laoreet, arcu massa bibendum ipsum, ac feugiat tellus orci id erat. Pellentesque elit felis, vehicula et egestas nec, pulvinar quis magna. Morbi sit amet malesuada erat, a placerat ex.
Praesent imperdiet, nulla sit amet vehicula volutpat, dolor velit aliquet mauris, a placerat orci urna vitae tellus. In hac habitasse platea dictumst. Donec ut elit eu urna tristique pellentesque. Maecenas dignissim iaculis scelerisque. Aenean ligula neque, efficitur sed elementum id, posuere eget lectus. Sed tristique sollicitudin arcu at interdum. Donec eget gravida nisi, id mollis nibh. Aliquam id accumsan quam. Praesent at suscipit magna. Sed porttitor tincidunt congue. Fusce dignissim dapibus consectetur. Ut bibendum finibus sem eu sodales. Nullam porta nisi lacus, at mollis arcu malesuada at.
Vertically centered
Add .modal-dialog-centered to .modal-dialog to vertically center the modal.
Modal title
Praesent imperdiet, nulla sit amet vehicula volutpat, dolor velit aliquet mauris, a placerat orci urna vitae tellus. In hac habitasse platea dictumst. Donec ut elit eu urna tristique pellentesque.
Tooltips and popovers
Tooltips and popovers can be placed within modals as needed. When modals are closed, any tooltips and popovers within are also automatically dismissed.
Use the Bootstrap grid inside a modal by nesting .container-fluid inside the .modal-body. Next, use the standard grid classes.
Grids in modals
.col-md-4
.col-md-4 .ml-auto
.col-md-3 .ml-auto
.col-md-2 .ml-auto
.col-md-6 .ml-auto
Level 1: .col-sm-9
Level 2: .col-8 .col-sm-6
Level 2: .col-4 .col-sm-6
Modifying modal content
In situation that you have many buttons that trigger the same modal with slightly different contents? Use event.relatedTarget and HTML data-* attributes to change the contents of the modal depending on button was clicked.
New message
<buttontype="button"class="btn btn-primary"data-toggle="modal"data-target="#exampleModal"data-whatever="@mdo">Open modal for @mdo</button><buttontype="button"class="btn btn-primary"data-toggle="modal"data-target="#exampleModal"data-whatever="@fat">Open modal for @fat</button><buttontype="button"class="btn btn-primary"data-toggle="modal"data-target="#exampleModal"data-whatever="@getbootstrap">Open modal for @getbootstrap</button><divclass="modal fade"id="exampleModal"tabindex="-1"role="dialog"aria-labelledby="exampleModalLabel"aria-hidden="true"><divclass="modal-dialog"role="document"><divclass="modal-content"><divclass="modal-header"><h5class="modal-title"id="exampleModalLabel">New message</h5><buttontype="button"class="close"data-dismiss="modal"aria-label="Close"><spanaria-hidden="true">×</span></button></div><divclass="modal-body"><form><divclass="form-group"><labelfor="recipient-name"class="col-form-label">Recipient:</label><inputtype="text"class="form-control"id="recipient-name"></div><divclass="form-group"><labelfor="message-text"class="col-form-label">Message:</label><textareaclass="form-control"id="message-text"></textarea></div></form></div><divclass="modal-footer"><buttontype="button"class="btn btn-secondary"data-dismiss="modal">Close</button><buttontype="button"class="btn btn-primary">Send message</button></div></div></div></div>
Remove animation
For modals that merely appear rather than fade into view, remove the .fade class from your modal markup.
Dynamic heights
If the height of modal changes while it is open, you should call $('#myModal').modal('handleUpdate') to readjust the modal’s position in case a scrollbar appears.
Accessibility
Be sure to add role="dialog" and aria-labelledby="...", referencing the modal title, to .modal, and role="document" to the .modal-dialog itself.
Optional sizes
Bootstrap modals have two sizes, available via css classes to be placed on a .modal-dialog.
Large modal
...
Small modal
...
Usage
The bootstrap modal plugin toggles your hidden content on request, using data attributes or JavaScript. It additionally adds .modal-open to the <body> to override default scrolling behavior and creates a .modal-backdrop to implement a click area for dismissing shown modals when clicking outside the bootstrap modal.
Via data attributes
Show a modal without writing JavaScript. Set data-toggle="modal" on a controller component, like a button, along with a data-target="#foo" or href="#foo" to target a specific modal to toggle.
Via JavaScript
Call a modal with id myModal with a single line of JavaScript:
Properties
Properties can be given via data attributes or JavaScript. For data attributes, add the option name to data-, as in data-backdrop="".
Name
Type
Default
Description
backdrop
boolean or the string 'static'
true
Includes a modal-backdrop element. Alternatively, specify static for a backdrop which doesn't close the modal on click.
keyboard
boolean
true
Closes the modal when escape key is pressed
focus
boolean
true
Puts the focus on the modal when initialized.
show
boolean
true
Shows the modal when initialized.
Methods
Asynchronous methods and transitions
All API methods are asynchronous and start a transition. They return to the caller as soon as the transition is started but before it ends. In addition, a method call on a transitioning component will be ignored.
Activates your content as a modal. Accepts an optional options object.
.modal('toggle')
Manually toggle a modal.
.modal('show')
Manually opens a modal
.modal('hide')
Manually hide a modal.
.modal('handleUpdate')
Manually adjust the modal’s position if the height of modal changes while it is open.
.modal('dispose')
Destroys an element’s modal.
Events
Bootstrap modal class shows a few events for hooking into modal functionality.
Event Type
Description
show.bs.modal
This event fires immediately when the show instance method is called. If caused by a click, the clicked element is available as the relatedTarget property of the event.
shown.bs.modal
This event is fired when the modal has been made visible to the user (will wait for CSS transitions to complete). If caused by a click, the clicked element is available as the relatedTarget property of the event.
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 (will wait for CSS transitions to complete).