Bootstrap 3 modal vertical position center

后端 未结 30 1090
天涯浪人
天涯浪人 2020-11-28 00:15

This is a two part question:

  1. How can you position the modal vertically in the center when you don\'t know the exact height of the modal?

  2. Is

相关标签:
30条回答
  • 2020-11-28 00:47

    In mobile plantform it might look a little different, here's my code.

    <div class="modal-container">
      <style>
      .modal-dialog{
        margin-top: 60%;
        width:80%;
        margin-left: 10%;
        margin-right: 10%;
        margin-bottom: 100%
      }
      @media screen and (orientation:landscape){
        .modal-dialog{
          margin-top: 70;
          width:80%;
          margin-left: 10%;
          margin-right: 10%;
          margin-bottom: 100%
        }
      }
      .modal-body{
        text-align: center;
      }
      .modal-body p{
        margin:14px 0px;
        font-size: 110%;
      }
      .modal-content{
        border-radius: 10px;
      }
      .modal-footer{
        padding:0px;
      }
      .modal-footer a{
        padding: 15px;
      }
      .modal-footer a:nth-child(1){
        border-radius: 0px 0px 0px 10px;
      }
      .modal-footer a:nth-child(2){
        border-radius: 0px 0px 10px 0px;
      }
      </style>
      <h2>Basic Modal Example</h2>
      <div data-toggle="modal" data-target="#myModal">Div for modal</div>
        <div class="modal fade" id="myModal" role="dialog">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-body">
                <p>确定要取消本次订单嘛?</p>
              </div>
              <div class="modal-footer">
                <div class="btn-group btn-group-justified">
                  <a href="#" class="btn btn-default" data-dismiss="modal">取消</a>
                  <a href="#" class="btn btn-default" data-dismiss="modal">确定</a>
                </div>
              </div>
            </div>
          </div>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-11-28 00:48

    All what I did in my case is to set the Top in my css knowing the height of the modal

    <div id="myModal" class="modal fade"> ... </div>

    in my css i set

    #myModal{
        height: 400px;
        top: calc(50% - 200px) !important;
    }
    
    0 讨论(0)
  • 2020-11-28 00:48
    $('#myModal').on('shown.bs.modal', function() {
        var initModalHeight = $('#modal-dialog').outerHeight(); //give an id to .mobile-dialog
        var userScreenHeight = $(document).outerHeight();
        if (initModalHeight > userScreenHeight) {
            $('#modal-dialog').css('overflow', 'auto'); //set to overflow if no fit
        } else {
            $('#modal-dialog').css('margin-top', 
            (userScreenHeight / 2) - (initModalHeight/2)); //center it if it does fit
        }
    });
    
    0 讨论(0)
  • 2020-11-28 00:49

    Try something like this:

    .popup__overlay {
        position: fixed;
        left:  0;
        top:  0;
        width: 100%;
        height: 100%;
        z-index: 999;
        text-align: center
        }
    .popup {
        display: inline-block;
        vertical-align: middle
        } 
    
    0 讨论(0)
  • 2020-11-28 00:52
    .modal {
      text-align: center;
    }
    
    @media screen and (min-width: 768px) { 
      .modal:before {
        display: inline-block;
        vertical-align: middle;
        content: " ";
        height: 100%;
      }
    }
    
    .modal-dialog {
      display: inline-block;
      text-align: left;
      vertical-align: middle;
    }
    

    And adjust a little bit .fade class to make sure it appears out of the top border of window, instead of center

    0 讨论(0)
  • 2020-11-28 00:52

    it's not that complicated.

    please try this:

    $(document).ready(function(){
        var modalId = "#myModal";
        resize: function(){
                var new_margin = Math.ceil(($(window).height() - $(modalId).find('.modal-dialog').height()) / 2);
                $(modalId).find('.modal-dialog').css('margin-top', new_margin + 'px');
        }
        $(window).resize(function(){
            resize();
        });
        $(modalId).on('shown.bs.modal', function(){
            resize();
        });
    });
    
    0 讨论(0)
提交回复
热议问题