Bootstrap 3 modal vertical position center

后端 未结 30 1093
天涯浪人
天涯浪人 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 01:01

    The most universal solution I wrote. Dynamicaly computes with dialog height. (Next step could be recalculating of dialogs' height on window resize.)

    JSfiddle: http://jsfiddle.net/8Fvg9/3/

    // initialise on document ready
    jQuery(document).ready(function ($) {
        'use strict';
    
        // CENTERED MODALS
        // phase one - store every dialog's height
        $('.modal').each(function () {
            var t = $(this),
                d = t.find('.modal-dialog'),
                fadeClass = (t.is('.fade') ? 'fade' : '');
            // render dialog
            t.removeClass('fade')
                .addClass('invisible')
                .css('display', 'block');
            // read and store dialog height
            d.data('height', d.height());
            // hide dialog again
            t.css('display', '')
                .removeClass('invisible')
                .addClass(fadeClass);
        });
        // phase two - set margin-top on every dialog show
        $('.modal').on('show.bs.modal', function () {
            var t = $(this),
                d = t.find('.modal-dialog'),
                dh = d.data('height'),
                w = $(window).width(),
                h = $(window).height();
            // if it is desktop & dialog is lower than viewport
            // (set your own values)
            if (w > 380 && (dh + 60) < h) {
                d.css('margin-top', Math.round(0.96 * (h - dh) / 2));
            } else {
                d.css('margin-top', '');
            }
        });
    
    });
    
    0 讨论(0)
  • 2020-11-28 01:01

    This works for me:

    .modal {
      text-align: center;
      padding: 0!important;
    }
    
    .modal:before {
      content: '';
      display: inline-block;
      height: 100%;
      vertical-align: middle;
      margin-right: -4px;
    }
    
    .modal-dialog {
      display: inline-block;
      text-align: left;
      vertical-align: middle;
    }
    
    0 讨论(0)
  • 2020-11-28 01:01

    This is quite old and specifically asks for a solution using Bootstrap 3, but for anyone wondering: from Bootstrap 4 on there is a built-in solution called .modal-dialog-centered. Here's the issue: https://github.com/twbs/bootstrap/issues/23638

    So using v4 you just need to add .modal-dialog-centered to .modal-dialog to vertically center the modal:

    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
      Launch demo modal
    </button>
    
    <!-- Modal -->
    <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
      <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalCenterTitle">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            ...
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
    

    Demo

    0 讨论(0)
  • 2020-11-28 01:01

    I know it's a bit late, but I am adding a new answer so that it doesn't get lost in the crowd. It's a cross-desktop-mobile-browser solution that works everywhere properly as it should.

    It just needs the modal-dialog to be wrapped inside a modal-dialog-wrap class and need to have the following code additions:

    .modal-dialog-wrap {
      display: table;
      table-layout: fixed;
      width: 100%;
      height: 100%;
    }
    
    .modal-dialog {
      display: table-cell;
      vertical-align: middle;
      text-align: center;
    }
    
    .modal-content {
      display: inline-block;
      text-align: left;
    }
    

    The dialog starts centered and in cases of large content it simply grows vertically until a scrollbar appears.

    Here is a working fiddle for your pleasure!

    https://jsfiddle.net/v6u82mvu/1/

    0 讨论(0)
  • 2020-11-28 01:02

    i have downloaded bootstrap3-dialog from bellow link and modified the open function in bootstrap-dialog.js

    https://github.com/nakupanda/bootstrap3-dialog

    Code

    open: function () {
                !this.isRealized() && this.realize();
                this.updateClosable();
                //Custom To Vertically centering Bootstrap 
                var $mymodal = this.getModal();
                $mymodal = $mymodal.append('<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%"><tr><td align="center" valign="middle" class="centerModal"></td></tr></table>');
                $mymodal = $mymodal.find(".modal-dialog").appendTo($mymodal.find(".centerModal"));
                //END
                this.getModal().modal('show');
                return this;
            }
    

    Css

    .centerModal .modal-header{
        text-align:left;
    }
    .centerModal .modal-body{
        text-align:left;
    } 
    
    0 讨论(0)
  • 2020-11-28 01:04

    It can simply can be fixed with display: flex

    .modal-dialog {
      margin-top: 0;
      margin-bottom: 0;
      height: 100vh;
      display: flex;
      flex-direction: column;
      justify-content: center;
    }
    
    .modal.fade .modal-dialog {
      transform: translate(0, -100%);
    }
    
    .modal.in .modal-dialog {
      transform: translate(0, 0);
    }
    

    With prefix

    .modal-dialog {
      margin-top: 0;
      margin-bottom: 0;
      height: 100vh;
      display: -webkit-box;
      display: -webkit-flex;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
      -webkit-flex-direction: column;
          -ms-flex-direction: column;
              flex-direction: column;
      -webkit-box-pack: center;
      -webkit-justify-content: center;
          -ms-flex-pack: center;
              justify-content: center;
    }
    
    .modal.fade .modal-dialog {
      -webkit-transform: translate(0, -100%);
              transform: translate(0, -100%);
    }
    .modal.in .modal-dialog {
      -webkit-transform: translate(0, 0);
              transform: translate(0, 0);
    }
    
    0 讨论(0)
提交回复
热议问题