Vertically center modal dialogues in Bootstrap 4.
Note: The requirements below have been added to make it clear I am looking for a proper way to vertically center a Bootstrap modal, covering all possible cases, on all possible devices, in all browsers. In my case, I wanted it for a large SPA reusing the same modal throughout the app so I needed it to work in each case.
It should:
- keep modal contents accessible, on all devices, even when taller than device height
- work on any device+browser combination with a market share larger than
1%
- not use
display:table-cell
or similar hacks (any layout-ing technique not meant or designed for layout-ing) - close on
click
ortap
anywhere outside of.modal-content
(including above and below). - limit usage of jQuery/JavaScript as much as possible
- (optional) work on default Bootstrap examples without need of markup modifications
There is a much easier way of achieving this without having to write a bunch of CSS overrides or other custom CSS, basically using just the stock bootstrap classes and adding one extra HTML element to control the height.
CSS (Not Needed see below)
HTML (Updated see below)
Then to use:
JS
OR HTML
There is probably a way to achieve the desired result using only the bootstrap
.row
,.col
andflex-XXX
classes but I was unable to get that to work.One last note, you might have to add:
<body class="d-flex">
to get everything to work depending on the rest of your CSS.UPDATE
There is a way to achieve this using only bootstrap classes,
h-100
andw-100
:Another simple way to make your modal vertical align is to adjust
top: 50%;
,transform: translateY(-50%);
andmargin: 0 auto
to the modal dialog class.Edit: The downside is that you also have to set
max-height: 100vh;
to.modal-content
. Otherwise the top of modal is not accessible anymore when your modal is getting heigher than the viewport.Demo:
Update, as of Beta 3, [docs]:
Original answer:
SCSS
:or unprefixed
CSS
:Note 1: Please note fully prefixed CSS gradually becomes obsolete as browser support for certain properties changes. The right way of getting the updated prefixed CSS is to:
> 0%
).Note 2: This answer was added in early stages of v4 (alpha 3 or 4), which is now currently in beta. You can safely replace the CSS part of this answer by adding the following classes to
.modal-dialog
:..., as pointed out by @Androbaut in the comment below. You will still need the JavaScript (see below) to close the modal window on
click tap
below/above the modal window.jQuery
(needed to close modal on click/tap above/below):That's it.
Working snippet, fully-prefixed CSS and markup using different modal sizes:
If you find any bugs or shortcomings please let me know. I will take the time to improve the answer and keep it useful. Help with this task is welcome.
Add .modal-dialog-centered to .modal-dialog to vertically center the modal.
Here's a simple Flexbox Approach.
SCSS
Working Demo
This solution works for small and long modals thats needs scroll.
Add these custom css rules:
And add these classes:
modal-dialog
:h-100 my-0 mx-auto d-flex flex-column justify-content-center
modal-content
:m-2
Like this: