2014-07-12 11:20:15 +02:00
---
2015-08-15 07:45:55 +02:00
layout: docs
2014-07-12 11:20:15 +02:00
title: Modal
2017-05-28 08:01:14 +02:00
description: Use Bootstrap's JavaScript modal plugin to add dialogs to your site for lightboxes, user notifications, or completely custom content.
2015-08-06 02:47:45 +02:00
group: components
2017-05-28 08:01:14 +02:00
toc: true
2014-07-12 11:20:15 +02:00
---
2016-12-24 23:04:44 +01:00
## How it works
2014-12-01 05:17:45 +01:00
2016-12-24 23:04:44 +01:00
Before getting started with Bootstrap's modal component, be sure to read the following as our menu options have recently changed.
2014-10-27 06:31:59 +01:00
2016-12-24 23:04:44 +01:00
- 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.
2017-01-05 23:24:40 +01:00
- 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.
2019-02-04 11:22:02 +01:00
- Once again, due to `position: fixed` , there are some caveats with using modals on mobile devices. [See our browser support docs ]({{< docsref "/getting-started/browsers-devices#modals-and-dropdowns-on-mobile" >}} ) for details.
2016-12-24 23:04:44 +01:00
- Due to how HTML5 defines its semantics, [the `autofocus` HTML attribute ](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-autofocus ) has no effect in Bootstrap modals. To achieve the same effect, use some custom JavaScript:
2015-04-17 01:56:40 +02:00
2020-10-19 11:56:49 +02:00
```js
2022-04-26 18:38:41 +02:00
const myModal = document.getElementById('myModal')
const myInput = document.getElementById('myInput')
2019-05-14 13:36:10 +02:00
2022-04-26 18:38:41 +02:00
myModal.addEventListener('shown.bs.modal', () => {
2019-05-14 13:36:10 +02:00
myInput.focus()
2016-12-24 23:04:44 +01:00
})
2020-10-19 11:56:49 +02:00
```
2015-04-17 01:56:40 +02:00
2020-10-13 15:37:21 +02:00
{{< callout info > }}
2022-10-16 18:09:33 +02:00
{{< partial " callouts / info-prefersreducedmotion . md " > }}
2020-10-13 15:37:21 +02:00
{{< / callout > }}
2018-11-03 19:23:26 +01:00
2017-06-26 18:14:52 +02:00
Keep reading for demos and usage guidelines.
2016-12-24 23:04:44 +01:00
## Examples
2014-03-17 03:03:53 +01:00
2016-12-24 23:04:44 +01:00
### Modal components
2014-07-13 09:54:29 +02:00
2016-12-24 23:04:44 +01:00
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.
2014-07-13 09:54:29 +02:00
Add dark mode support (#35857)
* Add dark mode to docs
* Minor fix: missing space indentation
* Minor fix: revert utilities/z-index added-in modification
* Remove prev: and next: from doc because extracted to another PR
* Use .bg-body-tertiary in all Utilities > Overflow examples
* fix example
* Fix up spacing examples
* Update box-shadow Sass variables and utilities to auto-adjust to color modes
* Remove unused docs class
* Refactor form styles to use CSS variable for background images on .form-check and .form-switch
* Fix docs selector
* Rename shortcut for clarity
* Heading consistency
* Reintroduce missing 4th grid item in Utilities > Spacing example
* Fix bundlewatch
* .bd-callout* rendering is OK so removing comments in the code
* Update scss/_utilities.scss
Co-authored-by: Julien Déramond <julien.deramond@orange.com>
* Fix gutters example styling
* Fix text colors on background utils docs
* redesign and fix up position marker example, which doesn't show nicely in darkmode but at least isn't broken
* fix some color utils examples
* Deprecate mixin notice
* Deprecate notice for list-group-item-variant() mixin
* Revamp new link CSS vars
* Use map-keys in some each Sass files
* Remove list-group-item-variant mixin ref in sass loop desc
* Display CSS vars scoped to our built-in dark mode
* Revert previous commit
* Fix list group variant link
* Fix typo
* Remove imports of alert/list-group mixins in scss/_mixins.scss
* Small formatting + comments removal in scss/_content.scss
* Fix alert links colors
* fix dropdown border-radius mixin
* fix link color and underline again, this time using CSS var override for color var and fallback value for the underline
* fix colors on docs navbar for dark mode
* remove two changes
* missing ref
* another link underline fix, just use sass vars for link decoration for now
* missing color bg docs, plus move dropdown override to scss
* more changes from review
* fix some examples, drop unused docs navbar styles, update docs navbar color mode to use mixin
* Few fixes around type
- Restored CSS variable for color on headings, this time with a fallback value
- In conjunction, restored and wrapped the default CSS var with a null value check
- Split headings and paragraphs docs in Reboot, elaborated on them
* Restyle custom details > summary element in docs
* Rewrite some migration docs
* fix form checks
* Fix up some navbar styling, tweak docs callout
* Fix select images, mostly for validation styling
* Clean up some migration notes, document some new form control CSS vars, mention new variables-dark in sass docs
* Update site/content/docs/5.2/components/scrollspy.md
Co-authored-by: Julien Déramond <julien.deramond@orange.com>
* Apply suggestions from code review
Co-authored-by: Julien Déramond <julien.deramond@orange.com>
* mention form control css vars in migration guide
* Tweak grid and flex docs background examples
* clarify some docs
* fix some more things
Co-authored-by: Julien Déramond <juderamond@gmail.com>
Co-authored-by: Julien Déramond <julien.deramond@orange.com>
2022-11-29 07:30:26 +01:00
< div class = "bd-example bg-body-tertiary" >
2022-04-19 20:21:43 +02:00
< div class = "modal position-static d-block" tabindex = "-1" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog" >
2014-07-13 09:54:29 +02:00
< div class = "modal-content" >
< div class = "modal-header" >
2016-12-25 02:26:19 +01:00
< h5 class = "modal-title" > Modal title< / h5 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2014-07-13 09:54:29 +02:00
< / div >
< div class = "modal-body" >
2016-12-24 23:04:44 +01:00
< p > Modal body text goes here.< / p >
2014-07-13 09:54:29 +02:00
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2014-07-13 09:54:29 +02:00
< button type = "button" class = "btn btn-primary" > Save changes< / button >
< / div >
2016-12-24 23:04:44 +01:00
< / div >
< / div >
< / div >
2014-07-13 09:54:29 +02:00
< / div >
2014-03-17 03:03:53 +01:00
2020-10-19 11:56:49 +02:00
```html
2020-06-04 09:35:09 +02:00
< div class = "modal" tabindex = "-1" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog" >
2014-03-17 03:03:53 +01:00
< div class = "modal-content" >
< div class = "modal-header" >
2016-12-25 02:26:19 +01:00
< h5 class = "modal-title" > Modal title< / h5 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2014-03-17 03:03:53 +01:00
< / div >
< div class = "modal-body" >
2016-12-24 23:04:44 +01:00
< p > Modal body text goes here.< / p >
2014-03-17 03:03:53 +01:00
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2018-03-15 23:18:30 +01:00
< button type = "button" class = "btn btn-primary" > Save changes< / button >
2014-03-17 03:03:53 +01:00
< / div >
2016-12-24 23:04:44 +01:00
< / div >
< / div >
< / div >
2020-10-19 11:56:49 +02:00
```
2014-03-17 03:03:53 +01:00
2022-10-02 12:02:36 +02:00
{{< callout info > }}
In the above static example, we use `<h5>` , to avoid issues with the heading hierarchy in the documentation page. Structurally, however, a modal dialog represents its own separate document/context, so the `.modal-title` should ideally be an `<h1>` . If necessary, you can use the [font size utilities ]({{< docsref "/utilities/text#font-size" >}} ) to control the heading's appearance. All the following live examples use this approach.
{{< / callout > }}
2014-07-13 09:54:29 +02:00
### Live demo
2014-03-17 03:03:53 +01:00
2016-12-24 23:04:44 +01:00
Toggle a working modal demo by clicking the button below. It will slide down and fade in from the top of the page.
2014-03-17 03:03:53 +01:00
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalLive" tabindex = "-1" aria-labelledby = "exampleModalLiveLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog" >
2014-07-13 09:54:29 +02:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "exampleModalLiveLabel" > Modal title< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2014-07-13 09:54:29 +02:00
< / div >
< div class = "modal-body" >
2022-01-11 10:17:12 +01:00
< p > Woo-hoo, you're reading this text in a modal!< / p >
2014-07-13 09:54:29 +02:00
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2014-07-13 09:54:29 +02:00
< button type = "button" class = "btn btn-primary" > Save changes< / button >
< / div >
2016-12-24 23:04:44 +01:00
< / div >
< / div >
2014-07-13 09:54:29 +02:00
< / div >
2016-12-24 23:04:44 +01:00
< div class = "bd-example" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalLive" >
2014-07-13 09:54:29 +02:00
Launch demo modal
< / button >
< / div >
2014-03-17 03:03:53 +01:00
2020-10-19 11:56:49 +02:00
```html
2014-03-17 03:03:53 +01:00
<!-- Button trigger modal -->
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModal" >
2014-03-17 03:03:53 +01:00
Launch demo modal
< / button >
<!-- Modal -->
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModal" tabindex = "-1" aria-labelledby = "exampleModalLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog" >
2014-03-17 03:03:53 +01:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "exampleModalLabel" > Modal title< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2014-03-17 03:03:53 +01:00
< / div >
< div class = "modal-body" >
...
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2014-03-17 03:03:53 +01:00
< button type = "button" class = "btn btn-primary" > Save changes< / button >
< / div >
< / div >
< / div >
< / div >
2020-10-19 11:56:49 +02:00
```
2014-03-17 03:03:53 +01:00
2019-10-25 20:12:09 +02:00
### Static backdrop
2022-02-21 17:13:42 +01:00
When backdrop is set to static, the modal will not close when clicking outside of it. Click the button below to try it.
2019-10-25 20:12:09 +02:00
2020-07-22 21:33:11 +02:00
< div class = "modal fade" id = "staticBackdropLive" data-bs-backdrop = "static" data-bs-keyboard = "false" tabindex = "-1" aria-labelledby = "staticBackdropLiveLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog" >
2019-10-25 20:12:09 +02:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "staticBackdropLiveLabel" > Modal title< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2019-10-25 20:12:09 +02:00
< / div >
< div class = "modal-body" >
2022-02-21 17:13:42 +01:00
< p > I will not close if you click outside of me. Don't even try to press escape key.< / p >
2019-10-25 20:12:09 +02:00
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2019-10-25 20:12:09 +02:00
< button type = "button" class = "btn btn-primary" > Understood< / button >
< / div >
< / div >
< / div >
< / div >
< div class = "bd-example" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#staticBackdropLive" >
2019-10-25 20:12:09 +02:00
Launch static backdrop modal
< / button >
< / div >
2020-10-19 11:56:49 +02:00
```html
2019-10-25 20:12:09 +02:00
<!-- Button trigger modal -->
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#staticBackdrop" >
2019-10-25 20:12:09 +02:00
Launch static backdrop modal
< / button >
<!-- Modal -->
2020-07-22 21:33:11 +02:00
< div class = "modal fade" id = "staticBackdrop" data-bs-backdrop = "static" data-bs-keyboard = "false" tabindex = "-1" aria-labelledby = "staticBackdropLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog" >
2019-10-25 20:12:09 +02:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "staticBackdropLabel" > Modal title< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2019-10-25 20:12:09 +02:00
< / div >
< div class = "modal-body" >
...
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2019-10-25 20:12:09 +02:00
< button type = "button" class = "btn btn-primary" > Understood< / button >
< / div >
< / div >
< / div >
< / div >
2020-10-19 11:56:49 +02:00
```
2019-10-25 20:12:09 +02:00
2016-12-24 23:04:44 +01:00
### Scrolling long content
2014-03-17 03:03:53 +01:00
2016-12-24 23:04:44 +01:00
When modals become too long for the user's viewport or device, they scroll independent of the page itself. Try the demo below to see what we mean.
2014-03-17 03:03:53 +01:00
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalLong" tabindex = "-1" aria-labelledby = "exampleModalLongTitle" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog" >
2014-03-17 03:03:53 +01:00
< div class = "modal-content" >
2016-12-24 23:04:44 +01:00
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "exampleModalLongTitle" > Modal title< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2016-12-24 23:04:44 +01:00
< / div >
2021-04-23 13:58:32 +02:00
< div class = "modal-body" style = "min-height: 1500px" >
< p > This is some placeholder content to show the scrolling behavior for modals. Instead of repeating the text the modal, we use an inline style set a minimum height, thereby extending the length of the overall modal and demonstrating the overflow scrolling. When content becomes longer than the height of the viewport, scrolling will move the modal as needed.< / p >
2016-12-24 23:04:44 +01:00
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2016-12-24 23:04:44 +01:00
< button type = "button" class = "btn btn-primary" > Save changes< / button >
< / div >
2014-03-17 03:03:53 +01:00
< / div >
< / div >
< / div >
2016-12-24 23:04:44 +01:00
< div class = "bd-example" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalLong" >
2016-12-24 23:04:44 +01:00
Launch demo modal
< / button >
2014-03-17 03:03:53 +01:00
< / div >
2019-01-20 22:28:16 +01:00
You can also create a scrollable modal that allows scroll the modal body by adding `.modal-dialog-scrollable` to `.modal-dialog` .
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalScrollable" tabindex = "-1" aria-labelledby = "exampleModalScrollableTitle" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-dialog-scrollable" >
2019-01-20 22:28:16 +01:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "exampleModalScrollableTitle" > Modal title< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2019-01-20 22:28:16 +01:00
< / div >
< div class = "modal-body" >
2021-09-08 07:59:27 +02:00
< p > This is some placeholder content to show the scrolling behavior for modals. We use repeated line breaks to demonstrate how content can exceed minimum inner height, thereby showing inner scrolling. When content becomes longer than the predefined max-height of modal, content will be cropped and scrollable within the modal.< / p >
2021-04-23 13:58:32 +02:00
< br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br > < br >
< p > This content should appear at the bottom after you scroll.< / p >
2019-01-20 22:28:16 +01:00
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2019-01-20 22:28:16 +01:00
< button type = "button" class = "btn btn-primary" > Save changes< / button >
< / div >
< / div >
< / div >
< / div >
< div class = "bd-example" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalScrollable" >
2019-01-20 22:28:16 +01:00
Launch demo modal
< / button >
< / div >
2020-10-19 11:56:49 +02:00
```html
2020-03-23 18:15:30 +01:00
<!-- Scrollable modal -->
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-dialog-scrollable" >
2020-03-23 18:15:30 +01:00
...
2019-01-20 22:28:16 +01:00
< / div >
2020-10-19 11:56:49 +02:00
```
2019-01-20 22:28:16 +01:00
2017-10-29 23:14:10 +01:00
### Vertically centered
2017-11-24 23:28:58 +01:00
Add `.modal-dialog-centered` to `.modal-dialog` to vertically center the modal.
2017-10-29 23:14:10 +01:00
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalCenter" tabindex = "-1" aria-labelledby = "exampleModalCenterTitle" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-dialog-centered" >
2017-10-29 23:14:10 +01:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "exampleModalCenterTitle" > Modal title< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2017-10-29 23:14:10 +01:00
< / div >
< div class = "modal-body" >
2021-04-23 13:58:32 +02:00
< p > This is a vertically centered modal.< / p >
2017-10-29 23:14:10 +01:00
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2017-10-29 23:14:10 +01:00
< button type = "button" class = "btn btn-primary" > Save changes< / button >
< / div >
< / div >
< / div >
< / div >
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalCenteredScrollable" tabindex = "-1" aria-labelledby = "exampleModalCenteredScrollableTitle" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-dialog-centered modal-dialog-scrollable" >
2019-01-20 22:28:16 +01:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "exampleModalCenteredScrollableTitle" > Modal title< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2019-01-20 22:28:16 +01:00
< / div >
< div class = "modal-body" >
2021-09-08 07:59:27 +02:00
< p > This is some placeholder content to show a vertically centered modal. We've added some extra copy here to show how vertically centering the modal works when combined with scrollable modals. We also use some repeated line breaks to quickly extend the height of the content, thereby triggering the scrolling. When content becomes longer than the predefined max-height of modal, content will be cropped and scrollable within the modal.< / p >
2021-04-23 13:58:32 +02:00
< br > < br > < br > < br > < br > < br > < br > < br > < br > < br >
< p > Just like that.< / p >
2019-01-20 22:28:16 +01:00
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2019-01-20 22:28:16 +01:00
< button type = "button" class = "btn btn-primary" > Save changes< / button >
< / div >
< / div >
< / div >
< / div >
2017-10-29 23:14:10 +01:00
< div class = "bd-example" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalCenter" >
2019-01-20 22:28:16 +01:00
Vertically centered modal
< / button >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalCenteredScrollable" >
2019-01-20 22:28:16 +01:00
Vertically centered scrollable modal
2017-10-29 23:14:10 +01:00
< / button >
< / div >
2020-10-19 11:56:49 +02:00
```html
2020-03-23 18:15:30 +01:00
<!-- Vertically centered modal -->
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-dialog-centered" >
2020-03-23 18:15:30 +01:00
...
< / div >
2017-10-29 23:14:10 +01:00
2020-03-23 18:15:30 +01:00
<!-- Vertically centered scrollable modal -->
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-dialog-centered modal-dialog-scrollable" >
2020-03-23 18:15:30 +01:00
...
2014-07-13 09:54:29 +02:00
< / div >
2020-10-19 11:56:49 +02:00
```
2014-03-17 03:03:53 +01:00
2016-12-24 23:04:44 +01:00
### Tooltips and popovers
2014-07-13 09:54:29 +02:00
2019-02-04 11:22:02 +01:00
[Tooltips ]({{< docsref "/components/tooltips" >}} ) and [popovers ]({{< docsref "/components/popovers" >}} ) can be placed within modals as needed. When modals are closed, any tooltips and popovers within are also automatically dismissed.
2016-12-24 23:04:44 +01:00
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalPopovers" tabindex = "-1" aria-labelledby = "exampleModalPopoversLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog" >
2016-12-24 23:04:44 +01:00
< div class = "modal-content" >
2014-07-13 09:54:29 +02:00
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "exampleModalPopoversLabel" > Modal title< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2014-07-13 09:54:29 +02:00
< / div >
< div class = "modal-body" >
2022-10-02 12:02:36 +02:00
< h2 class = "fs-5" > Popover in a modal< / h2 >
2022-11-12 18:21:33 +01:00
< p > This < button class = "btn btn-secondary" data-bs-toggle = "popover" title = "Popover title" data-bs-content = "Popover body content is set in this attribute." data-bs-container = "#exampleModalPopovers" > button< / button > triggers a popover on click.< / p >
2016-12-24 23:04:44 +01:00
< hr >
2022-10-02 12:02:36 +02:00
< h2 class = "fs-5" > Tooltips in a modal< / h2 >
2022-05-04 10:05:15 +02:00
< p > < a href = "#" data-bs-toggle = "tooltip" title = "Tooltip" data-bs-container = "#exampleModalPopovers" > This link< / a > and < a href = "#" data-bs-toggle = "tooltip" title = "Tooltip" data-bs-container = "#exampleModalPopovers" > that link< / a > have tooltips on hover.< / p >
2016-12-24 23:04:44 +01:00
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2016-12-24 23:04:44 +01:00
< button type = "button" class = "btn btn-primary" > Save changes< / button >
2014-07-13 09:54:29 +02:00
< / div >
< / div >
< / div >
< / div >
2016-12-24 23:04:44 +01:00
< div class = "bd-example" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalPopovers" >
2016-12-24 23:04:44 +01:00
Launch demo modal
< / button >
< / div >
2014-03-17 03:03:53 +01:00
2020-10-19 11:56:49 +02:00
```html
2016-12-24 23:04:44 +01:00
< div class = "modal-body" >
2022-10-02 12:02:36 +02:00
< h2 class = "fs-5" > Popover in a modal< / h2 >
2022-11-12 18:21:33 +01:00
< p > This < button class = "btn btn-secondary" data-bs-toggle = "popover" title = "Popover title" data-bs-content = "Popover body content is set in this attribute." > button< / button > triggers a popover on click.< / p >
2016-12-24 23:04:44 +01:00
< hr >
2022-10-02 12:02:36 +02:00
< h2 class = "fs-5" > Tooltips in a modal< / h2 >
2022-05-04 10:05:15 +02:00
< p > < a href = "#" data-bs-toggle = "tooltip" title = "Tooltip" > This link< / a > and < a href = "#" data-bs-toggle = "tooltip" title = "Tooltip" > that link< / a > have tooltips on hover.< / p >
2014-03-17 03:03:53 +01:00
< / div >
2020-10-19 11:56:49 +02:00
```
2014-03-17 03:03:53 +01:00
2016-12-24 23:04:44 +01:00
### Using the grid
2015-03-01 22:44:10 +01:00
2016-12-24 23:04:44 +01:00
Utilize the Bootstrap grid system within a modal by nesting `.container-fluid` within the `.modal-body` . Then, use the normal grid system classes as you would anywhere else.
2015-03-01 22:44:10 +01:00
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "gridSystemModal" tabindex = "-1" aria-labelledby = "gridModalLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog" >
2015-03-01 22:44:10 +01:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "gridModalLabel" > Grids in modals< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2015-03-01 22:44:10 +01:00
< / div >
< div class = "modal-body" >
2015-08-21 11:23:41 +02:00
< div class = "container-fluid bd-example-row" >
< div class = "row" >
< div class = "col-md-4" > .col-md-4< / div >
2020-06-09 15:44:20 +02:00
< div class = "col-md-4 ms-auto" > .col-md-4 .ms-auto< / div >
2015-08-21 11:23:41 +02:00
< / div >
< div class = "row" >
2020-06-09 15:44:20 +02:00
< 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 >
2015-08-21 11:23:41 +02:00
< / div >
< div class = "row" >
2020-06-09 15:44:20 +02:00
< div class = "col-md-6 ms-auto" > .col-md-6 .ms-auto< / div >
2015-08-21 11:23:41 +02:00
< / div >
< div class = "row" >
< div class = "col-sm-9" >
Level 1: .col-sm-9
< div class = "row" >
2016-11-04 17:54:34 +01:00
< div class = "col-8 col-sm-6" >
Level 2: .col-8 .col-sm-6
2015-08-21 11:23:41 +02:00
< / div >
2016-11-04 17:54:34 +01:00
< div class = "col-4 col-sm-6" >
Level 2: .col-4 .col-sm-6
2015-08-21 11:23:41 +02:00
< / div >
2015-03-01 22:44:10 +01:00
< / div >
< / div >
< / div >
< / div >
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2015-03-01 22:44:10 +01:00
< button type = "button" class = "btn btn-primary" > Save changes< / button >
< / div >
< / div >
< / div >
< / div >
2016-12-24 23:04:44 +01:00
< div class = "bd-example" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#gridSystemModal" >
2016-12-24 23:04:44 +01:00
Launch demo modal
< / button >
2015-03-01 22:44:10 +01:00
< / div >
2020-10-19 11:56:49 +02:00
```html
2016-12-24 23:04:44 +01:00
< div class = "modal-body" >
< div class = "container-fluid" >
< div class = "row" >
< div class = "col-md-4" > .col-md-4< / div >
2020-06-09 15:44:20 +02:00
< div class = "col-md-4 ms-auto" > .col-md-4 .ms-auto< / div >
2016-12-24 23:04:44 +01:00
< / div >
< div class = "row" >
2020-06-09 15:44:20 +02:00
< 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 >
2016-12-24 23:04:44 +01:00
< / div >
< div class = "row" >
2020-06-09 15:44:20 +02:00
< div class = "col-md-6 ms-auto" > .col-md-6 .ms-auto< / div >
2016-12-24 23:04:44 +01:00
< / 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 >
2020-10-19 11:56:49 +02:00
```
2016-12-24 23:04:44 +01:00
### Varying modal content
2020-07-22 21:33:11 +02:00
Have a bunch of buttons that all trigger the same modal with slightly different contents? Use `event.relatedTarget` and [HTML `data-bs-*` attributes ](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes ) to vary the contents of the modal depending on which button was clicked.
2014-11-10 07:02:53 +01:00
2016-12-24 23:04:44 +01:00
Below is a live demo followed by example HTML and JavaScript. For more information, [read the modal events docs ](#events ) for details on `relatedTarget` .
2014-11-10 07:02:53 +01:00
2022-06-20 16:19:47 +02:00
{{< example stackblitz_add_js = "true" > }}
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModal" data-bs-whatever = "@mdo" > Open modal for @mdo </ button >
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModal" data-bs-whatever = "@fat" > Open modal for @fat </ button >
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModal" data-bs-whatever = "@getbootstrap" > Open modal for @getbootstrap </ button >
2016-12-24 23:04:44 +01:00
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModal" tabindex = "-1" aria-labelledby = "exampleModalLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog" >
2016-12-24 23:04:44 +01:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "exampleModalLabel" > New message< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2016-12-24 23:04:44 +01:00
< / div >
< div class = "modal-body" >
< form >
2019-08-29 22:07:55 +02:00
< div class = "mb-3" >
2017-10-19 19:31:49 +02:00
< label for = "recipient-name" class = "col-form-label" > Recipient:< / label >
2016-12-24 23:04:44 +01:00
< input type = "text" class = "form-control" id = "recipient-name" >
< / div >
2019-08-29 22:07:55 +02:00
< div class = "mb-3" >
2017-10-19 19:31:49 +02:00
< label for = "message-text" class = "col-form-label" > Message:< / label >
2016-12-24 23:04:44 +01:00
< textarea class = "form-control" id = "message-text" > < / textarea >
< / div >
< / form >
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2016-12-24 23:04:44 +01:00
< button type = "button" class = "btn btn-primary" > Send message< / button >
2014-11-10 07:02:53 +01:00
< / div >
< / div >
< / div >
< / div >
2019-01-08 17:33:28 +01:00
{{< / example > }}
2014-11-10 07:02:53 +01:00
2020-10-19 11:56:49 +02:00
```js
2022-04-26 18:38:41 +02:00
const exampleModal = document.getElementById('exampleModal')
exampleModal.addEventListener('show.bs.modal', event => {
2019-05-14 13:36:10 +02:00
// Button that triggered the modal
2022-04-26 18:38:41 +02:00
const button = event.relatedTarget
2020-07-22 21:33:11 +02:00
// Extract info from data-bs-* attributes
2022-04-26 18:38:41 +02:00
const recipient = button.getAttribute('data-bs-whatever')
2023-01-06 04:59:59 +01:00
// If necessary, you could initiate an Ajax request here
2019-02-19 15:19:02 +01:00
// and then do the updating in a callback.
2019-05-14 13:36:10 +02:00
//
2019-02-19 15:19:02 +01:00
// Update the modal's content.
2022-04-26 18:38:41 +02:00
const modalTitle = exampleModal.querySelector('.modal-title')
const modalBodyInput = exampleModal.querySelector('.modal-body input')
2020-05-19 07:33:12 +02:00
2022-04-26 18:38:41 +02:00
modalTitle.textContent = `New message to ${recipient}`
2020-05-19 07:33:12 +02:00
modalBodyInput.value = recipient
2014-11-10 07:02:53 +01:00
})
2020-10-19 11:56:49 +02:00
```
2014-11-10 07:02:53 +01:00
2021-03-17 06:15:06 +01:00
### Toggle between modals
Toggle between multiple modals with some clever placement of the `data-bs-target` and `data-bs-toggle` attributes. For example, you could toggle a password reset modal from within an already open sign in modal. **Please note multiple modals cannot be open at the same time** —this method simply toggles between two separate modals.
2021-04-15 08:21:36 +02:00
{{< example > }}
< div class = "modal fade" id = "exampleModalToggle" aria-hidden = "true" aria-labelledby = "exampleModalToggleLabel" tabindex = "-1" >
2021-03-17 06:15:06 +01:00
< div class = "modal-dialog modal-dialog-centered" >
< div class = "modal-content" >
2021-04-15 08:21:36 +02:00
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "exampleModalToggleLabel" > Modal 1< / h1 >
2021-04-15 08:21:36 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
< / div >
< div class = "modal-body" >
Show a second modal and hide this one with the button below.
< / div >
2021-03-17 06:15:06 +01:00
< div class = "modal-footer" >
2021-09-28 17:46:09 +02:00
< button class = "btn btn-primary" data-bs-target = "#exampleModalToggle2" data-bs-toggle = "modal" > Open second modal< / button >
2021-03-17 06:15:06 +01:00
< / div >
< / div >
< / div >
< / div >
2021-04-15 08:21:36 +02:00
< div class = "modal fade" id = "exampleModalToggle2" aria-hidden = "true" aria-labelledby = "exampleModalToggleLabel2" tabindex = "-1" >
2021-03-17 06:15:06 +01:00
< div class = "modal-dialog modal-dialog-centered" >
< div class = "modal-content" >
2021-04-15 08:21:36 +02:00
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-5" id = "exampleModalToggleLabel2" > Modal 2< / h1 >
2021-04-15 08:21:36 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
< / div >
< div class = "modal-body" >
Hide this modal and show the first with the button below.
< / div >
2021-03-17 06:15:06 +01:00
< div class = "modal-footer" >
2021-08-10 16:55:34 +02:00
< button class = "btn btn-primary" data-bs-target = "#exampleModalToggle" data-bs-toggle = "modal" > Back to first< / button >
2021-03-17 06:15:06 +01:00
< / div >
< / div >
< / div >
< / div >
2022-11-12 18:21:33 +01:00
< button class = "btn btn-primary" data-bs-target = "#exampleModalToggle" data-bs-toggle = "modal" > Open first modal< / button >
2021-04-15 08:21:36 +02:00
{{< / example > }}
2021-03-17 06:15:06 +01:00
2018-08-13 09:00:53 +02:00
### 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)` .
2016-12-24 23:04:44 +01:00
### Remove animation
For modals that simply appear rather than fade in to view, remove the `.fade` class from your modal markup.
2020-10-19 11:56:49 +02:00
```html
2020-06-04 09:35:09 +02:00
< div class = "modal" tabindex = "-1" aria-labelledby = "..." aria-hidden = "true" >
2016-12-24 23:04:44 +01:00
...
< / div >
2020-10-19 11:56:49 +02:00
```
2016-12-24 23:04:44 +01:00
### Dynamic heights
2015-03-01 22:44:10 +01:00
2019-05-14 13:36:10 +02:00
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.
2015-03-01 22:44:10 +01:00
2016-12-24 23:04:44 +01:00
### Accessibility
2020-06-04 09:35:09 +02:00
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.
2016-12-24 23:04:44 +01:00
### Embedding YouTube videos
Embedding YouTube videos in modals requires additional JavaScript not in Bootstrap to automatically stop playback and more. [See this helpful Stack Overflow post ](https://stackoverflow.com/questions/18622508/bootstrap-3-and-youtube-in-modal ) for more information.
## Optional sizes
2016-05-17 15:14:07 +02:00
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.
2016-12-24 23:04:44 +01:00
2022-03-14 08:38:04 +01:00
{{< bs-table " table " > }}
| Size | Class | Modal max-width
| --- | --- | --- |
| Small | `.modal-sm` | `300px` |
2023-01-17 00:55:28 +01:00
| Default | < span class = "text-body-secondary" > None</ span > | `500px` |
2022-03-14 08:38:04 +01:00
| Large | `.modal-lg` | `800px` |
| Extra large | `.modal-xl` | `1140px` |
{{< / bs-table > }}
2018-07-08 05:15:48 +02:00
Our default modal without modifier class constitutes the "medium" size modal.
2016-12-24 23:04:44 +01:00
< div class = "bd-example" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalXl" > Extra large modal< / button >
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalLg" > Large modal< / button >
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalSm" > Small modal< / button >
2016-12-24 23:04:44 +01:00
< / div >
2020-10-19 11:56:49 +02:00
```html
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-xl" > ...< / div >
< div class = "modal-dialog modal-lg" > ...< / div >
< div class = "modal-dialog modal-sm" > ...< / div >
2020-10-19 11:56:49 +02:00
```
2016-12-24 23:04:44 +01:00
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalXl" tabindex = "-1" aria-labelledby = "exampleModalXlLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-xl" >
2016-05-17 15:14:07 +02:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-4" id = "exampleModalXlLabel" > Extra large modal< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2016-05-17 15:14:07 +02:00
< / div >
< div class = "modal-body" >
...
< / div >
< / div >
< / div >
< / div >
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalLg" tabindex = "-1" aria-labelledby = "exampleModalLgLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-lg" >
2016-12-24 23:04:44 +01:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-4" id = "exampleModalLgLabel" > Large modal< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2016-12-24 23:04:44 +01:00
< / div >
< div class = "modal-body" >
...
< / div >
< / div >
< / div >
< / div >
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalSm" tabindex = "-1" aria-labelledby = "exampleModalSmLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-sm" >
2016-12-24 23:04:44 +01:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-4" id = "exampleModalSmLabel" > Small modal< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2016-12-24 23:04:44 +01:00
< / div >
< div class = "modal-body" >
...
< / div >
< / div >
< / div >
< / div >
2019-04-23 16:03:59 +02:00
## 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` .
2022-03-14 08:38:04 +01:00
{{< bs-table > }}
| Class | Availability |
2023-03-27 10:48:42 +02:00
| --- | --- |
2022-03-14 08:38:04 +01:00
| `.modal-fullscreen` | Always |
| `.modal-fullscreen-sm-down` | `576px` |
| `.modal-fullscreen-md-down` | `768px` |
| `.modal-fullscreen-lg-down` | `992px` |
| `.modal-fullscreen-xl-down` | `1200px` |
| `.modal-fullscreen-xxl-down` | `1400px` |
{{< / bs-table > }}
2019-04-23 16:03:59 +02:00
< div class = "bd-example" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalFullscreen" > Full screen< / button >
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalFullscreenSm" > Full screen below sm< / button >
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalFullscreenMd" > Full screen below md< / button >
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalFullscreenLg" > Full screen below lg< / button >
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalFullscreenXl" > Full screen below xl< / button >
< button type = "button" class = "btn btn-primary" data-bs-toggle = "modal" data-bs-target = "#exampleModalFullscreenXxl" > Full screen below xxl< / button >
2019-04-23 16:03:59 +02:00
< / div >
2020-10-19 11:56:49 +02:00
```html
2019-04-23 16:03:59 +02:00
<!-- Full screen modal -->
2020-02-27 16:30:06 +01:00
< div class = "modal-dialog modal-fullscreen-sm-down" >
...
2019-04-23 16:03:59 +02:00
< / div >
2020-10-19 11:56:49 +02:00
```
2019-04-23 16:03:59 +02:00
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalFullscreen" tabindex = "-1" aria-labelledby = "exampleModalFullscreenLabel" aria-hidden = "true" >
2019-04-23 16:03:59 +02:00
< div class = "modal-dialog modal-fullscreen" >
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-4" id = "exampleModalFullscreenLabel" > Full screen modal< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2019-04-23 16:03:59 +02:00
< / div >
< div class = "modal-body" >
2021-05-05 04:31:37 +02:00
...
2019-04-23 16:03:59 +02:00
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2019-04-23 16:03:59 +02:00
< / div >
< / div >
< / div >
< / div >
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalFullscreenSm" tabindex = "-1" aria-labelledby = "exampleModalFullscreenSmLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-fullscreen-sm-down" >
2019-04-23 16:03:59 +02:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-4" id = "exampleModalFullscreenSmLabel" > Full screen below sm< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2019-04-23 16:03:59 +02:00
< / div >
< div class = "modal-body" >
...
< / div >
2020-02-27 16:30:06 +01:00
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2020-02-27 16:30:06 +01:00
< / div >
2019-04-23 16:03:59 +02:00
< / div >
< / div >
< / div >
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalFullscreenMd" tabindex = "-1" aria-labelledby = "exampleModalFullscreenMdLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-fullscreen-md-down" >
2019-04-23 16:03:59 +02:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-4" id = "exampleModalFullscreenMdLabel" > Full screen below md< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2019-04-23 16:03:59 +02:00
< / div >
< div class = "modal-body" >
...
< / div >
2020-02-27 16:30:06 +01:00
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2020-02-27 16:30:06 +01:00
< / div >
2019-04-23 16:03:59 +02:00
< / div >
< / div >
< / div >
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalFullscreenLg" tabindex = "-1" aria-labelledby = "exampleModalFullscreenLgLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-fullscreen-lg-down" >
2019-04-23 16:03:59 +02:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-4" id = "exampleModalFullscreenLgLabel" > Full screen below lg< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2019-04-23 16:03:59 +02:00
< / div >
< div class = "modal-body" >
...
< / div >
2020-02-27 16:30:06 +01:00
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2020-02-27 16:30:06 +01:00
< / div >
2019-04-23 16:03:59 +02:00
< / div >
< / div >
< / div >
2020-06-04 09:35:09 +02:00
< div class = "modal fade" id = "exampleModalFullscreenXl" tabindex = "-1" aria-labelledby = "exampleModalFullscreenXlLabel" aria-hidden = "true" >
2020-05-11 11:45:42 +02:00
< div class = "modal-dialog modal-fullscreen-xl-down" >
2019-04-23 16:03:59 +02:00
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-4" id = "exampleModalFullscreenXlLabel" > Full screen below xl< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2019-04-23 16:03:59 +02:00
< / div >
< div class = "modal-body" >
...
< / div >
2020-02-27 16:30:06 +01:00
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2020-02-27 16:30:06 +01:00
< / div >
2019-04-23 16:03:59 +02:00
< / div >
< / div >
< / div >
2020-09-29 20:56:01 +02:00
< div class = "modal fade" id = "exampleModalFullscreenXxl" tabindex = "-1" aria-labelledby = "exampleModalFullscreenXxlLabel" aria-hidden = "true" >
< div class = "modal-dialog modal-fullscreen-xxl-down" >
< div class = "modal-content" >
< div class = "modal-header" >
2022-10-02 12:02:36 +02:00
< h1 class = "modal-title fs-4" id = "exampleModalFullscreenXxlLabel" > Full screen below xxl< / h1 >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn-close" data-bs-dismiss = "modal" aria-label = "Close" > < / button >
2020-09-29 20:56:01 +02:00
< / div >
< div class = "modal-body" >
...
< / div >
< div class = "modal-footer" >
2020-07-22 21:33:11 +02:00
< button type = "button" class = "btn btn-secondary" data-bs-dismiss = "modal" > Close< / button >
2020-09-29 20:56:01 +02:00
< / div >
< / div >
< / div >
< / div >
2022-03-29 00:05:37 +02:00
## CSS
2021-02-11 04:29:59 +01:00
### Variables
2022-03-29 00:05:37 +02:00
{{< added-in " 5 . 2 . 0 " > }}
2022-04-11 09:42:43 +02:00
As part of Bootstrap's evolving CSS variables approach, modals now use local CSS variables on `.modal` and `.modal-backdrop` for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.
2022-03-29 00:05:37 +02:00
{{< scss-docs name = "modal-css-vars" file = "scss/_modal.scss" > }}
{{< scss-docs name = "modal-backdrop-css-vars" file = "scss/_modal.scss" > }}
### Sass variables
2021-02-11 04:29:59 +01:00
{{< scss-docs name = "modal-variables" file = "scss/_variables.scss" > }}
2023-03-13 11:41:27 +01:00
### Sass loop
2021-02-11 04:29:59 +01:00
[Responsive fullscreen modals ](#fullscreen-modal ) are generated via the `$breakpoints` map and a loop in `scss/_modal.scss` .
{{< scss-docs name = "modal-fullscreen-loop" file = "scss/_modal.scss" > }}
2014-07-13 09:54:29 +02:00
## Usage
2021-06-14 07:43:35 +02:00
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.
2014-07-13 09:54:29 +02:00
### Via data attributes
2021-07-19 15:56:05 +02:00
#### Toggle
2020-07-22 21:33:11 +02:00
Activate a modal without writing JavaScript. Set `data-bs-toggle="modal"` on a controller element, like a button, along with a `data-bs-target="#foo"` or `href="#foo"` to target a specific modal to toggle.
2014-03-17 03:03:53 +01:00
2020-10-19 11:56:49 +02:00
```html
2020-07-22 21:33:11 +02:00
< button type = "button" data-bs-toggle = "modal" data-bs-target = "#myModal" > Launch modal< / button >
2020-10-19 11:56:49 +02:00
```
2021-07-30 07:56:36 +02:00
2021-07-19 15:56:05 +02:00
#### Dismiss
2021-07-28 16:39:32 +02:00
{{% js-dismiss "modal" %}}
2021-07-19 15:56:05 +02:00
{{< callout warning > }}
2022-06-02 23:10:22 +02:00
While both ways to dismiss a modal are supported, keep in mind that dismissing from outside a modal does not match the [ARIA Authoring Practices Guide dialog (modal) pattern ](https://www.w3.org/WAI/ARIA/apg/patterns/dialogmodal/ ). Do this at your own risk.
2021-07-19 15:56:05 +02:00
{{< / callout > }}
2014-03-17 03:03:53 +01:00
2014-07-13 09:54:29 +02:00
### Via JavaScript
2019-05-14 13:36:10 +02:00
Create a modal with a single line of JavaScript:
2014-07-13 09:54:29 +02:00
2020-10-19 11:56:49 +02:00
```js
2022-04-26 18:38:41 +02:00
const myModal = new bootstrap.Modal(document.getElementById('myModal'), options)
// or
const myModalAlternative = new bootstrap.Modal('#myModal', options)
2020-10-19 11:56:49 +02:00
```
2014-07-13 09:54:29 +02:00
### Options
2022-04-21 20:41:43 +02:00
{{< markdown > }}
{{< partial " js-data-attributes . md " > }}
{{< / markdown > }}
2014-07-13 09:54:29 +02:00
2022-03-14 08:38:04 +01:00
{{< bs-table " table " > }}
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `backdrop` | boolean, `'static'` | `true` | Includes a modal-backdrop element. Alternatively, specify `static` for a backdrop which doesn't close the modal when clicked. |
| `focus` | boolean | `true` | Puts the focus on the modal when initialized. |
2022-05-20 19:21:48 +02:00
| `keyboard` | boolean | `true` | Closes the modal when escape key is pressed. |
2022-03-14 08:38:04 +01:00
{{< / bs-table > }}
2014-07-13 09:54:29 +02:00
### Methods
2020-10-13 15:37:21 +02:00
{{< callout danger > }}
2022-10-16 18:09:33 +02:00
{{< partial " callouts / danger-async-methods . md " > }}
2020-10-13 15:37:21 +02:00
{{< / callout > }}
2017-03-28 23:43:16 +02:00
2019-05-14 13:36:10 +02:00
#### Passing options
2014-07-13 09:54:29 +02:00
Activates your content as a modal. Accepts an optional options `object` .
2020-10-19 11:56:49 +02:00
```js
2022-04-26 18:38:41 +02:00
const myModal = new bootstrap.Modal('#myModal', {
2014-03-17 03:03:53 +01:00
keyboard: false
})
2020-10-19 11:56:49 +02:00
```
2014-03-17 03:03:53 +01:00
2022-03-14 08:38:04 +01:00
{{< bs-table " table " > }}
| Method | Description |
| --- | --- |
| `dispose` | Destroys an element's modal. (Removes stored data on the DOM element) |
2022-10-25 20:37:36 +02:00
| `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 initialized. |
2022-05-20 19:21:48 +02:00
| `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). |
| `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). |
2022-08-29 14:11:13 +02:00
| `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). Also, you can pass a DOM element as an argument that can be received in the modal events (as the `relatedTarget` property). (i.e. `const modalToggle = document.getElementById('toggleMyModal'); myModal.show(modalToggle)` . |
2022-05-20 19:21:48 +02:00
| `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). |
2022-03-14 08:38:04 +01:00
{{< / bs-table > }}
2021-06-03 17:53:27 +02:00
2014-07-13 09:54:29 +02:00
### Events
2015-03-01 22:44:10 +01:00
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">` ).
2014-07-13 09:54:29 +02:00
2022-03-14 08:38:04 +01:00
{{< bs-table > }}
| Event | Description |
| --- | --- |
| `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). |
| `hidePrevented.bs.modal` | This event is fired when the modal is shown, its backdrop is `static` and a click outside of the modal is performed. The event is also fired when the escape key is pressed and the `keyboard` option is set to `false` . |
2022-05-20 19:21:48 +02:00
| `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. |
2022-03-14 08:38:04 +01:00
{{< / bs-table > }}
2014-07-13 09:54:29 +02:00
2020-10-19 11:56:49 +02:00
```js
2022-04-26 18:38:41 +02:00
const myModalEl = document.getElementById('myModal')
myModalEl.addEventListener('hidden.bs.modal', event => {
2014-03-17 03:03:53 +01:00
// do something...
})
2020-10-19 11:56:49 +02:00
```