Vertically centering Bootstrap modal window

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

    Yet another CSS solution. Does not Work for popups that are bigger than the view port.

    .modal-dialog {
        position: absolute;
        right: 0;
        left: 0;
        margin-top: 0;
        margin-bottom: 0; 
    }
    .modal.fade .modal-dialog {
        transition: top 0.4s ease-out;
        transform: translate(0, -50%);
        top: 0;
    }
    .modal.in .modal-dialog {
        transform: translate(0, -50%);
        top: 50%;
    }
    

    In .modal-dialog class overriding the position to absolute(from relative) and centering the content right:0, left: 0

    In .modal.fade .modal-dialog , .modal.in .modal-dialog setting the transition animation over top rather than on translate.

    margin-top moves the popup slightly below the center in case of small popup and in case of long popups, the modal is stuck with header. Hence margin-top:0, margin-bottom:0

    Need to further refine it.

提交回复
热议问题