如何垂直居中模态?

时间:2016-11-05 06:03:56

标签: javascript jquery html css twitter-bootstrap

我想垂直居中一个模态。我在Google上搜索了很多但没有发现任何有用的信息。这是我的代码:



 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">&times;</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>
&#13;
&#13;
&#13;

如何将其垂直居中?

7 个答案:

答案 0 :(得分:17)

未经测试,但您可以试试这个

var array = [
  [1,2,3],
  [4,5,6],
  [7,8,9]
]

答案 1 :(得分:6)

因为它看起来像一个引导模式,所以 Fiddle (jsFiddle)结帐。

尝试

.modal-dialog {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) !important;
}

希望这有帮助!

答案 2 :(得分:4)

为此有一个特定的引导程序类(v4.1):

.modal-dialog 中添加 .modal-dialog-居中,以使模态垂直居中。

来源: https://getbootstrap.com/docs/4.1/components/modal/#vertically-centered

答案 3 :(得分:2)

通过添加位置和变换属性,您将无法使它在所有宽度和设备上居中,而且始终进行计算可能是一个乏味的过程。这是通过仅对'.modal-dialog'类进行样式设置而起作用的

Window1 childWin= new Window1();
childWin.ShowDialog();
OR
childWin.Show();

答案 4 :(得分:1)

试试这个: -

&#13;
&#13;
// 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);
&#13;
/* 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;
}
&#13;
<!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">&times;</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>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

最简单(未经测试的跨浏览器,但应该可以):

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; 
}

这是小提琴: http://jsfiddle.net/o9pbgnz5/

同时设置top:0 bottom:0非常重要。然后,margin-top:automargin-bottom:auto应该注意垂直居中。在此示例中,速记“ margin:auto”将其水平和垂直居中。

P.S。也适用于position:fixed;

答案 6 :(得分:0)

如果您在顶部 CSS 属性上使用 CSS calc() 函数,则可以垂直居中模式。假设模态的高度为 600px。使用值为 50 的 vh 单位将使您获得屏幕垂直高度的中点。然后你只需要减去模态元素高度的一半。

#modal {
   position:  fixed;
   height: 600px;
   top: calc(50vh - 300px);
   z-index: 100;
}