Bootstrap 3 modal vertical position center

后端 未结 30 1176
天涯浪人
天涯浪人 2020-11-28 00:15

This is a two part question:

  1. How can you position the modal vertically in the center when you don\'t know the exact height of the modal?

  2. Is

30条回答
  •  情深已故
    2020-11-28 00:43

    I came up with a pure css solution! It's css3 though, which means ie8 or lower is not supported, but other than this it's tested and working on ios, android, ie9+, chrome, firefox, desktop safari..

    I am using the following css:

    .modal-dialog {
      position:absolute;
      top:50% !important;
      transform: translate(0, -50%) !important;
      -ms-transform: translate(0, -50%) !important;
      -webkit-transform: translate(0, -50%) !important;
      margin:auto 5%;
      width:90%;
      height:80%;
    }
    .modal-content {
      min-height:100%;
      position:absolute;
      top:0;
      bottom:0;
      left:0;
      right:0; 
    }
    .modal-body {
      position:absolute;
      top:45px; /** height of header **/
      bottom:45px;  /** height of footer **/
      left:0;
      right:0;
      overflow-y:auto;
    }
    .modal-footer {
      position:absolute;
      bottom:0;
      left:0;
      right:0;
    }
    

    Here is a fiddle. http://codepen.io/anon/pen/Hiskj

    ..selecting this as the correct answer since there's no extra heavy javascript that brings the browser to its knees in case of more than one modals.

提交回复
热议问题