Vertically centering Bootstrap modal window

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

    I think this is a bit cleaner pure CSS solution than Rens de Nobel's solution. Also this does not prevent from closing the dialog by clicking outside of it.

    http://plnkr.co/edit/JCGVZQ?p=preview

    Just add some CSS class to DIV container with .modal-dialog class to gain higher specificity than the bootstraps CSS, e.g. .centered.

    HTML

    
    

    And make this .modal-dialog.centered container fixed and properly positioned.

    CSS

    .modal .modal-dialog.centered {
        position: fixed;
        bottom: 50%;
        right: 50%;
        transform: translate(50%, 50%);
    }
    

    Or even simpler using flexbox.

    CSS

    .modal {
        display: flex;
        align-items: center;
        justify-content: center;
    }
    

提交回复
热议问题