Bootstrap 3 modal vertical position center

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

    You might want to check out this collection of methods for absolute centering a div: http://codepen.io/shshaw/full/gEiDt

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

    I came up with a pure css solution! It's css3 though, which means ie8 or lower is not supported, but other than this it's tested and working on ios, android, ie9+, chrome, firefox, desktop safari..

    I am using the following css:

    .modal-dialog {
      position:absolute;
      top:50% !important;
      transform: translate(0, -50%) !important;
      -ms-transform: translate(0, -50%) !important;
      -webkit-transform: translate(0, -50%) !important;
      margin:auto 5%;
      width:90%;
      height:80%;
    }
    .modal-content {
      min-height:100%;
      position:absolute;
      top:0;
      bottom:0;
      left:0;
      right:0; 
    }
    .modal-body {
      position:absolute;
      top:45px; /** height of header **/
      bottom:45px;  /** height of footer **/
      left:0;
      right:0;
      overflow-y:auto;
    }
    .modal-footer {
      position:absolute;
      bottom:0;
      left:0;
      right:0;
    }
    

    Here is a fiddle. http://codepen.io/anon/pen/Hiskj

    ..selecting this as the correct answer since there's no extra heavy javascript that brings the browser to its knees in case of more than one modals.

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

    There is an easiest way to do this using css:

    .modal-dialog {
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        width:500px;
        height:300px;
    }
    

    That's it. Notice that it is only needed to be applied to the .modal-dialog container div.

    Demo: https://jsfiddle.net/darioferrer/0ueu4dmy/

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

    My solution:

    .modal.in .modal-dialog 
    {
        -webkit-transform: translate(0, calc(50vh - 50%));
        -ms-transform: translate(0, 50vh) translate(0, -50%);
        -o-transform: translate(0, calc(50vh - 50%));
        transform: translate(0, 50vh) translate(0, -50%);
    }
    
    0 讨论(0)
  • 2020-11-28 00:46
    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);
    
    0 讨论(0)
  • 2020-11-28 00:47

    For the centering, I don't get what's with the overly complicated solutions. bootstrap already centers it horizontally for you, so you don't need to mess with this. My solution is just set a top margin only using jQuery.

    $('#myModal').on('loaded.bs.modal', function() {
        $(this).find('.modal-dialog').css({
            'margin-top': function () {
                return (($(window).outerHeight() / 2) - ($(this).outerHeight() / 2));
            }
        });
    });
    

    I've used the loaded.bs.modal event as I am remotely loading content, and using the shown.ba.modal event causes the height calculation to be incorrect. You can of course add in an event for the window being resized if you need it to be that responsive.

    0 讨论(0)
提交回复
热议问题