Vertically centering Bootstrap modal window

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

    
    
    
    
    

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

提交回复
热议问题