Vertically centering Bootstrap modal window

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

    Because most of the answer here didn't work, or only partially worked:

    body.modal-open .modal[style]:not([style='display: none;']) {
        display: flex !important;
        height: 100%;
    } 
    
    body.modal-open .modal[style]:not([style='display: none;']) .modal-dialog {
        margin: auto;
    }
    

    You have to use the [style] selector to only apply the style on the modal that is currently active instead of all the modals. .in would have been great, but it appears to be added only after the transition is complete which is too late and makes for some really bad transitions. Fortunately it appears bootstrap always applies a style attribute on the modal just as it is starting to show so this is a bit hacky, but it works.

    The :not([style='display: none;']) portion is a workaround to bootstrap not correctly removing the style attribute and instead setting the style display to none when you close the dialog.

提交回复
热议问题