Bootstrap模态未正确显示居中

时间:2018-05-29 08:41:02

标签: html modal-dialog bootstrap-4 bootstrap-modal

screenshot of the page

嗨,我正试图弄清楚为什么这个模态会像这样显示出来。我跟随的代码是从bootstrap网站复制的。 这是

<button type="button" class="btn btn-info btn-lg" (click)="showPerformedActivityModal()">Show Performed Activities</button>

<!--Modal -->
<div class="container">
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true"
  [ngStyle]="{'display': myModalIsOpen ? 'block' : 'none', 'opacity': 5}">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <app-table [config]="config" [columns]="columns" [entity]="entity" [sort]="sort" [sortOrder]="sortOrder">
          </app-table>
        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-primary" (click)="refreshResources()">Select Activities</button>
          <button type="button" class="btn btn-default" (click)="closeModal()">Close</button>
        </div>
      </div>

    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

这可以完成工作:http://jsfiddle.net/sRmLV/1140/

您需要使用新的DIV和自定义CSS,更改类&#39;模态对话框为中心&#39;通过&#39; vertical-align-center&#39;

<强> HTML

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Launch demo modal</button>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="vertical-alignment-helper">
    <div class="modal-dialog vertical-align-center">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
          <h4 class="modal-title" id="myModalLabel">Modal title</h4>
        </div>
        <div class="modal-body">...</div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary">Save changes</button>
        </div>
      </div>
    </div>
  </div>
</div>

<强> CSS

.vertical-alignment-helper {
display:table;
height: 100%;
width: 100%;
pointer-events:none;}

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