Modals
javascript-modals
Modals are streamlined, but flexible, dialog prompts with the minimum required functionality and smart defaults.
Modals are streamlined, but flexible, dialog prompts with the minimum required functionality and smart defaults.
Due to how HTML5 defines its semantics, the
autofocus
HTML attribute has no effect in Bootstrap modals. To achieve the same effect, use some custom JavaScript:Examples
Static example
A rendered modal with header, body, and set of actions in the footer.
Live demo
Toggle a modal via JavaScript by clicking the button below.
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.
Using the grid system
To take advantage of the Bootstrap grid system within a modal, just nest
.container-fluid
within the.modal-body
and then use the normal grid system classes within this container.Varying modal content based on trigger button
Have a bunch of buttons that all trigger the same modal, just with slightly different contents? Use
event.relatedTarget
and HTMLdata-*
attributes (possibly via jQuery) to vary the contents of the modal depending on which button was clicked. See the Modal Events docs for details onrelatedTarget
.Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to
data-
, as indata-offset=""
.Usage
The modal plugin toggles your hidden content on demand, via data attributes or JavaScript. It also adds
.modal-open
to the<body>
to override default scrolling behavior and generates a .modal-backdrop to provide a click area for dismissing shown modals when clicking outside the modal.Via data attributes
Activate a modal without writing JavaScript. Set
data-toggle="modal"
on a controller element, like a button, along with adata-target="#foo"
orhref="#foo"
to target a specific modal to toggle.Via JavaScript
Call a modal with id myModal with a single line of JavaScript:
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to
data-
, as indata-backdrop=""
.'static'
static
for a backdrop which doesn't close the modal on click.This option is deprecated since v3.3.0 and will be removed in v4. We recommend instead using client-side templating or a data binding framework, or calling jQuery.load yourself.
If a remote URL is provided, content will be loaded one time via jQuery's
load
method and injected into the.modal-content
div. If you're using the data-api, you may alternatively use thehref
attribute to specify the remote source. An example of this is shown below:Methods
.modal(options)
Activates your content as a modal. Accepts an optional options
object
..modal('toggle')
Manually toggles a modal. Returns to the caller before the modal has actually been shown or hidden (i.e. before the shown
.bs.modal
orhidden.bs.modal
event occurs)..modal('show')
Manually opens a modal. Returns to the caller before the modal has actually been shown (i.e. before the
shown.bs.modal
event occurs)..modal('hide')
Manually hides a modal. Returns to the caller before the modal has actually been hidden (i.e. before the hidden.bs.modal event occurs).
.modal('handleUpdate')
Readjusts the modal's positioning to counter a scrollbar in case one should appear, which would make the modal jump to the left.
Only needed when the height of the modal changes while it is open.
Events
Bootstrap's modal class exposes a few events for hooking into modal functionality.
All modal events are fired at the modal itself (i.e. at the
<div class="modal">
).show
instance method is called. If caused by a click, the clicked element is available as therelatedTarget
property of the event.relatedTarget
property of the event.hide
instance method has been called.remote
option.