Reputation: 515
I want to vertically center a modal. I searched a lot on Google but found nothing helpful. Here is my code:
function inactive(id, ths) {
$("#confirmation").modal("show");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal fade bs-example-modal-sm" id="confirmation" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Are you sure you want to inactivate it?</h4>
</div>
<div class="modal-body">
<div class="form-group">
<input type="text" class="form-control" name="captcha" id="cval" placeholder="enter captcha">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<button type="button" class="btn btn-primary" id="yes">Yes</button>
</div>
</div>
</div>
</div>
How can I center it vertically?
Upvotes: 20
Views: 50613
Reputation: 1
if you are using bootstrap 4 or above you can simply use bootstrap classes modal-dialog modal-dialog-centered
function inactive(id, ths) {
$("#confirmation").modal("show");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal-dialog modal-dialog-centered fade bs-example-modal-sm" id="confirmation" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Are you sure you want to inactivate it?</h4>
</div>
<div class="modal-body">
<div class="form-group">
<input type="text" class="form-control" name="captcha" id="cval" placeholder="enter captcha">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<button type="button" class="btn btn-primary" id="yes">Yes</button>
</div>
</div>
</div>
</div>
Upvotes: 0
Reputation: 99
Centering the modal vertically can be done if you use CSS calc() function on the top CSS property. Let's say that the modal has a height of 600px. Using the vh unit with a value of 50 will get you the midpoint on the vertical height of the screen. Then you just need to subtract half of the height of the modal element.
#modal {
position: fixed;
height: 600px;
top: calc(50vh - 300px);
z-index: 100;
}
Upvotes: 1
Reputation: 302
By adding position and transform properties, you wouldn't get it to be centred across all widths and devices, plus calculating it all the time could be a tedious process. Here is what worked by styling just the '.modal-dialog' class
/* Center a bootstrap modal Vertically and horizontally */
.modal-dialog {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
/* and Viola! */
Upvotes: 9
Reputation: 111
There is a specific bootstrap class (v4.1) for this:
Add .modal-dialog-centered to .modal-dialog to vertically center the modal.
Source: https://getbootstrap.com/docs/4.1/components/modal/#vertically-centered
Upvotes: 11
Reputation: 681
Easiest (not tested cross-browser but should be ok):
HTML:
<div class="container">
<p>Container</p>
<div class="modal">
<p>Modal</p>
</div>
</div>
CSS:
.container {
position:relative;
}
.modal {
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
}
Here's the fiddle: http://jsfiddle.net/o9pbgnz5/
It's important that you set both top:0
and bottom:0
. Then, margin-top:auto
and margin-bottom:auto
should take care of the vertical centering. In this example, the shorthand 'margin:auto' centers it both horizontally and vertically.
P.S. Also works with position:fixed;
Upvotes: 1
Reputation: 9731
As it looks as a bootstrap modal, Checkout this Fiddle (jsFiddle).
Try
.modal-dialog {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) !important;
}
Hope this helps!
Upvotes: 7
Reputation: 2211
Try this:-
// Vertical centered modals
// you can give custom class like this // var modalVerticalCenterClass = ".modal.modal-vcenter";
var modalVerticalCenterClass = ".modal";
function centerModals($element) {
var $modals;
if ($element.length) {
$modals = $element;
} else {
$modals = $(modalVerticalCenterClass + ':visible');
}
$modals.each( function(i) {
var $clone = $(this).clone().css('display', 'block').appendTo('body');
var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2);
top = top > 0 ? top : 0;
$clone.remove();
$(this).find('.modal-content').css("margin-top", top);
});
}
$(modalVerticalCenterClass).on('show.bs.modal', function(e) {
centerModals($(this));
});
$(window).on('resize', centerModals);
/* scroll fixes */
.modal-open .modal {
padding-left: 0px !important;
padding-right: 0px !important;
overflow-y: scroll;
}
/* centering styles for jsbin */
html,
body {
width:100%;
height:100%;
}
html {
display:table;
}
body {
display:table-cell;
vertical-align:middle;
}
body > .btn {
display: block;
margin: 0 auto;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Bootstrap 3 vertically centered modal and scroll fixes</title>
<meta name="description" content="Bootstrap 3 vertically centered modal and scroll fixes">
<!-- include bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
</head>
<body>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- include jQuery -->
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<!-- include bootstrap -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>
Upvotes: 1
Reputation: 50291
Not tested but you can try this
.yourElement{
position: relative;
top: 50%;
transform: translateY(-50%);
}
Upvotes: 45