Vertically centering Bootstrap modal window

前端 未结 30 1902
被撕碎了的回忆
被撕碎了的回忆 2020-12-07 07:00

I would like to center my modal on the viewport (middle) I tried to add some css properties

 .modal { position: fixed; top:50%; left:50%; }   
相关标签:
30条回答
  • 2020-12-07 07:47

    .modal.in .modal-dialog {
        position: relative;
        top: 50%;
        transform: translateY(-50%);
    }

    0 讨论(0)
  • 2020-12-07 07:48

    No need to us javascript. Boostrap modal adds .in class when it appears. Just modify this class combination with modalclassName.fade.in with flex css and you are done.

    add this css to center your modal vertically and horizontally.

    .modal.fade.in {
        display: flex !important;
        justify-content: center;
        align-items: center;
    }
    .modal.fade.in .modal-dialog {
        width: 100%;
    }
    
    0 讨论(0)
  • 2020-12-07 07:49

    This works in BS3, not tested in v2. It centers the modal vertically. Note that it will transition there - if you want it to just appear in position edit CSS transition property for .modal-dialog

    centerModal = function() {
        var $dialog = $(this).find(".modal-dialog"),
            offset = ($(window).height() - $dialog.height()) / 2;
    
        // Center modal vertically in window
        $dialog.css({
            'transform': 'translateY(' + offset + 'px) !important',
        });
    };
    
    $('.modal').on('shown.bs.modal', centerModal);
    $(window).on("resize", function() {
        $('.modal').each(centerModal);
    });
    
    0 讨论(0)
  • 2020-12-07 07:49

    Use this simple script that centers the modals.

    If you want you can set a custom class (ex: .modal.modal-vcenter instead of .modal) to limit the functionality only to some modals.

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

    Also add this CSS fix for the modal's horizontal spacing; we show the scroll on the modals, the body scrolls is hidden automatically by Bootstrap:

    /* scroll fixes */
    .modal-open .modal {
      padding-left: 0px !important;
      padding-right: 0px !important;
      overflow-y: scroll;
    }
    
    0 讨论(0)
  • 2020-12-07 07:50

    Because gpcola's answer didn't work for me, I edited a bit so its works now. I used "margin-top" instead of transform. Also, i use the "show" instead of "shown" event because after it gave me a very bad jump of positioning (visible when you have bootstrap animations on). Be sure to set the display to "block" before positioning, otherwise $dialog.height() will be 0 and the modal will not be centered completely.

    (function ($) {
        "use strict";
        function centerModal() {
            $(this).css('display', 'block');
            var $dialog  = $(this).find(".modal-dialog"),
            offset       = ($(window).height() - $dialog.height()) / 2,
            bottomMargin = parseInt($dialog.css('marginBottom'), 10);
    
            // Make sure you don't hide the top part of the modal w/ a negative margin if it's longer than the screen height, and keep the margin equal to the bottom margin of the modal
            if(offset < bottomMargin) offset = bottomMargin;
            $dialog.css("margin-top", offset);
        }
    
        $(document).on('show.bs.modal', '.modal', centerModal);
        $(window).on("resize", function () {
            $('.modal:visible').each(centerModal);
        });
    }(jQuery));
    
    0 讨论(0)
  • 2020-12-07 07:50

    This takes Arany's answer and makes it work if the modal is taller than the height of the screen:

    function centerModal() {
        $(this).css('display', 'block');
        var $dialog = $(this).find(".modal-dialog");
        var offset = ($(window).height() - $dialog.height()) / 2;
        //Make sure you don't hide the top part of the modal w/ a negative margin if it's longer than the screen height, and keep the margin equal to the bottom margin of the modal
        var bottomMargin = $dialog.css('marginBottom');
        bottomMargin = parseInt(bottomMargin);
        if(offset < bottomMargin) offset = bottomMargin;
        $dialog.css("margin-top", offset);
    }
    
    $('.modal').on('show.bs.modal', centerModal);
    $(window).on("resize", function () {
        $('.modal:visible').each(centerModal);
    });
    
    0 讨论(0)
提交回复
热议问题