twitter-bootstrap 如何垂直居中模态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40435286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to vertically center modal?
提问by Rushabh Shah
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?
我怎样才能垂直居中?
回答by brk
Not tested but you can try this
未测试,但你可以试试这个
.yourElement{
position: relative;
top: 50%;
transform: translateY(-50%);
}
回答by Saurav Rastogi
回答by Diana Quinteros G.
There is a specific bootstrap class (v4.1) for this:
为此有一个特定的引导程序类 (v4.1):
Add .modal-dialog-centeredto .modal-dialogto vertically center the modal.
添加.modal-dialog- central到.modal-dialog以垂直居中模态。
Source: https://getbootstrap.com/docs/4.1/components/modal/#vertically-centered
来源:https: //getbootstrap.com/docs/4.1/components/modal/#vertically-central
回答by Kingsley Gyimah
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
通过添加位置和变换属性,您不会让它在所有宽度和设备上居中,而且一直计算它可能是一个乏味的过程。这是仅通过样式设置 '.modal-dialog' 类而起作用的方法
/* Center a bootstrap modal Vertically and horizontally */
.modal-dialog {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
/* and Viola! */
回答by Lucian Davidescu
Easiest (not tested cross-browser but should be ok):
最简单(未经跨浏览器测试,但应该没问题):
HTML:
HTML:
<div class="container">
<p>Container</p>
<div class="modal">
<p>Modal</p>
</div>
</div>
CSS:
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/
这是小提琴:http: //jsfiddle.net/o9pbgnz5/
It's important that you set both top:0andbottom:0. Then, margin-top:autoand margin-bottom:autoshould take care of the vertical centering. In this example, the shorthand 'margin:auto' centers it both horizontally and vertically.
设置top:0和很重要bottom:0。然后,margin-top:auto和margin-bottom:auto应采取垂直居中的照顾。在这个例子中,速记 'margin:auto' 将它水平和垂直居中。
P.S. Also works with position:fixed;
PS也适用于 position:fixed;
回答by Razia sultana
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>

