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
autofocusHTML 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-fluidwithin the.modal-bodyand 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.relatedTargetand 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-opento 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'staticfor 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
loadmethod and injected into the.modal-contentdiv. If you're using the data-api, you may alternatively use thehrefattribute 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.modalorhidden.bs.modalevent occurs)..modal('show')
Manually opens a modal. Returns to the caller before the modal has actually been shown (i.e. before the
shown.bs.modalevent 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">).showinstance method is called. If caused by a click, the clicked element is available as therelatedTargetproperty of the event.relatedTargetproperty of the event.hideinstance method has been called.remoteoption.