Vertically centering Bootstrap modal window

前端 未结 30 1772
被撕碎了的回忆
被撕碎了的回忆 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:25

    This does the job : http://jsfiddle.net/sRmLV/1140/

    It uses a helper-div and some custom css. No javascript or jQuery required.

    HTML (based on Bootstrap's demo-code)

    <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Launch demo modal</button>
    
    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="vertical-alignment-helper">
            <div class="modal-dialog vertical-align-center">
                <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>
                        <button type="button" class="btn btn-primary">Save changes</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    

    CSS

    .vertical-alignment-helper {
        display:table;
        height: 100%;
        width: 100%;
        pointer-events:none; /* This makes sure that we can still click outside of the modal to close it */
    }
    .vertical-align-center {
        /* To center vertically */
        display: table-cell;
        vertical-align: middle;
        pointer-events:none;
    }
    .modal-content {
        /* Bootstrap sets the size of the modal in the modal-dialog class, we need to inherit it */
        width:inherit;
        max-width:inherit; /* For Bootstrap 4 - to avoid the modal window stretching full width */
        height:inherit;
        /* To center horizontally */
        margin: 0 auto;
        pointer-events: all;
    }
    
    0 讨论(0)
  • 2020-12-07 07:26

    this Works for me perfectly:

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

    complete Demo URL : https://codepen.io/dimbslmh/full/mKfCc

    0 讨论(0)
  • you can use:

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

    to center it both vertically and horizontally.

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

    You can achieve vertical alignment center on Bootstrap 3 modal like that :

    .modal-vertical-centered {
      transform: translate(0, 50%) !important;
      -ms-transform: translate(0, 50%) !important; /* IE 9 */
      -webkit-transform: translate(0, 50%) !important; /* Safari and Chrome */
    }
    

    and add this css class to the "modal-dialog" container

    <div class="modal-dialog modal-vertical-centered">
    ...
    

    jsFiddle working example : http://jsfiddle.net/U4NRx/

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

    To make Modal verically Align Middle All dialog.

    /* Note:

    1.Even you don't need to assign selector , it will find all modal from the document and make it vertically middle

    2.To avoid middle some specific modal to be it center you can use :not selector in click event

    */

     $( "[data-toggle='modal']" ).click(function(){
         var d_tar = $(this).attr('data-target'); 
         $(d_tar).show();   
         var modal_he = $(d_tar).find('.modal-dialog .modal-content').height(); 
         var win_height = $(window).height();
         var marr = win_height - modal_he;
         $('.modal-dialog').css('margin-top',marr/2);
      });
    

    From Milan Pandya

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

    Based on Arany's answer, but also accounting for page scroll.

    (function($) {
        "use strict";
        function positionModals(e) {
            var $this = $(this).css('display', 'block'),
                $window = $(window),
                $dialog = $this.find('.modal-dialog'),
                offset = ($window.height() - $window.scrollTop() - $dialog.height()) / 2,
                marginBottom = parseInt($dialog.css('margin-bottom'), 10);
    
            $dialog.css('margin-top', offset < marginBottom ? marginBottom : offset);
        }
    
        $(document).on('show.bs.modal', '.modal', positionModals);
    
        $(window).on('resize', function(e) {
            $('.modal:visible').each(positionModals);
        });
    }(jQuery));
    
    0 讨论(0)
提交回复
热议问题