Modal Bootstrap v50
Modal Bootstrap v50
View on GitHub
Modal
Use Bootstrapʼs JavaScript modal plugin to add dialogs to your site
for lightboxes, user notifications, or completely custom content.
On this page
How it works
Examples
Modal components
Live demo
Static backdrop
Scrolling long content
Vertically centered
Tooltips and popovers
Using the grid
Varying modal content
Toggle between modals
Change animation
Remove animation
Dynamic heights
Accessibility
Embedding YouTube videos
Optional sizes
Fullscreen Modal
Sass
Variables
How it works
Before getting started with Bootstrapʼs modal component, be sure to read the
following as our menu options have recently changed.
Modals are built with HTML, CSS, and JavaScript. Theyʼre positioned over
everything else in the document and remove scroll from the <body> so that modal
content scrolls instead.
Clicking on the modal “backdrop” will automatically close the modal.
Bootstrap only supports one modal window at a time. Nested modals arenʼt
supported as we believe them to be poor user experiences.
Modals use position: fixed, which can sometimes be a bit particular about its
rendering. Whenever possible, place your modal HTML in a top-level position to
avoid potential interference from other elements. Youʼll likely run into issues when
nesting a .modal within another fixed element.
Once again, due to position: fixed, there are some caveats with using modals
on mobile devices. See our browser support docs for details.
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
myModal.addEventListener('shown.bs.modal', function () {
myInput.focus()
})
Examples
Modal components
Below is a static modal example (meaning its position and display have been
overridden). Included are the modal header, modal body (required for padding), and
modal footer (optional). We ask that you include modal headers with dismiss actions
whenever possible, or provide another explicit dismiss action.
Live demo
Toggle a working modal demo by clicking the button below. It will slide down and fade
in from the top of the page.
Static backdrop
When backdrop is set to static, the modal will not close when clicking outside it. Click
the button below to try it.
You can also create a scrollable modal that allows scroll the modal body by adding
.modal-dialog-scrollable to .modal-dialog.
Vertically centered
Add .modal-dialog-centered to .modal-dialog to vertically center the modal.
<div class="modal-body">
<h5>Popover in a modal</h5>
<p>This <a href="#" role="button" class="btn btn-secondary popover-test" title
<hr>
<h5>Tooltips in a modal</h5>
<p><a href="#" class="tooltip-test" title="Tooltip">This link</a> and <a href
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-md-4">.col-md-4</div>
<div class="col-md-4 ms-auto">.col-md-4 .ms-auto</div>
</div>
<div class="row">
<div class="col-md-3 ms-auto">.col-md-3 .ms-auto</div>
<div class="col-md-2 ms-auto">.col-md-2 .ms-auto</div>
</div>
<div class="row">
<div class="col-md-6 ms-auto">.col-md-6 .ms-auto</div>
</div>
<div class="row">
<div class="col-sm-9">
Level 1: .col-sm-9
<div class="row">
<div class="col-8 col-sm-6">
Level 2: .col-8 .col-sm-6
</div>
<div class="col-4 col-sm-6">
Level 2: .col-4 .col-sm-6
</div>
</div>
</div>
</div>
</div>
</div>
Below is a live demo followed by example HTML and JavaScript. For more information,
read the modal events docs for details on relatedTarget.
Change animation
The $modal-fade-transform variable determines the transform state of .modal-
dialog before the modal fade-in animation, the $modal-show-transform variable
determines the transform of .modal-dialog at the end of the modal fade-in animation.
If you want for example a zoom-in animation, you can set $modal-fade-transform:
scale(.8).
Dynamic heights
If the height of a modal changes while it is open, you should call
myModal.handleUpdate() to readjust the modalʼs position in case a scrollbar appears.
Accessibility
Be sure to add aria-labelledby="...", referencing the modal title, to .modal.
Additionally, you may give a description of your modal dialog with aria-describedby
on .modal. Note that you donʼt need to add role="dialog" since we already add it via
JavaScript.
Optional sizes
Modals have three optional sizes, available via modifier classes to be placed on a
.modal-dialog. These sizes kick in at certain breakpoints to avoid horizontal scrollbars
on narrower viewports.
Our default modal without modifier class constitutes the “medium” size modal.
Fullscreen Modal
Another override is the option to pop up a modal that covers the user viewport,
available via modifier classes that are placed on a .modal-dialog.
Class Availability
.modal-fullscreen Always
Sass
Variables
$modal-inner-padding: $spacer;
$modal-footer-margin-between: .5rem;
$modal-dialog-margin: .5rem;
$modal-dialog-margin-y-sm-up: 1.75rem;
$modal-title-line-height: $line-height-base;
$modal-content-color: null;
$modal-content-bg: $white;
$modal-content-border-color: rgba($black, .2);
$modal-content-border-width: $border-width;
$modal-content-border-radius: $border-radius-lg;
$modal-content-inner-border-radius: subtract($modal-content-border-radius, $modal-
$modal-content-box-shadow-xs: $box-shadow-sm;
$modal-content-box-shadow-sm-up: $box-shadow;
$modal-backdrop-bg: $black;
$modal-backdrop-opacity: .5;
$modal-header-border-color: $border-color;
$modal-footer-border-color: $modal-header-border-color;
$modal-header-border-width: $modal-content-border-width;
$modal-footer-border-width: $modal-header-border-width;
$modal-sm: 300px;
$modal-md: 500px;
$modal-lg: 800px;
$modal-xl: 1140px;
Loop
Responsive fullscreen modals are generated via the $breakpoints map and a loop in
scss/_modal.scss.
@include media-breakpoint-down($breakpoint) {
.modal-fullscreen#{$postfix} {
width: 100vw;
max-width: none;
height: 100%;
margin: 0;
.modal-content {
height: 100%;
border: 0;
@include border-radius(0);
}
.modal-header {
@include border-radius(0);
}
.modal-footer {
@include border-radius(0);
}
}
}
}
Usage
The modal plugin toggles your hidden content on demand, via data attributes or
JavaScript. It also overrides default scrolling behavior and generates a .modal-
backdrop to provide a click area for dismissing shown modals when clicking outside
the modal.
Via JavaScript
Create a modal with a single line of JavaScript:
keyboard boolean true Closes the modal when escape key is pressed
focus boolean true Puts the focus on the modal when initialized.
Methods
Passing options
Activates your content as a modal. Accepts an optional options object.
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 or hidden.bs.modal event occurs).
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).
myModal.show()
Also, you can pass a DOM element as an argument that can be received in the modal
events (as the relatedTarget property).
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).
myModal.hide()
handleUpdate
Manually readjust the modalʼs position if the height of a modal changes while it is open
(i.e. in case a scrollbar appears).
myModal.handleUpdate()
dispose
Destroys an elementʼs modal. (Removes stored data on the DOM element)
getInstance
Static method which allows you to get the modal instance associated with a DOM
element
getOrCreateInstance
Static method which allows you to get the modal instance associated with a DOM
element, or create a new one in case it wasnʼt initialised
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.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
hidden.bs.modal This event is fired when the modal has finished being hidden from
the user (will wait for CSS transitions to complete).
hidePrevented.bs.modal This event is fired when the modal is shown, its backdrop is static
and a click outside the modal or an escape key press is performed
with the keyboard option or data-bs-keyboard set to false.
Currently v5.0.2.
Analytics by Fathom.
Links Guides
Home Getting started
Themes Parcel
Blog
Swag Store
Projects Community
Bootstrap 5 Issues
Bootstrap 4 Discussions