twitter-bootstrap Bootstrap 4 模态中心内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50041065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:20:52  来源:igfitidea点击:

Bootstrap 4 Modal Center Content

htmlcsstwitter-bootstrapbootstrap-4

提问by TheOrdinaryGeek

Can someone please explain how to horizontally center the title in a Bootstrap 4 modal.

有人可以解释一下如何在 Bootstrap 4 模式中水平居中标题。

I've tried text-center, mx-autoand ml-0 / mr-0but they don't appear to work.

我试过text-centermx-automl-0 / mr-0,但他们似乎没有工作。

Here's a linkto a fiddle.

这是一个小提琴的链接

Code below;

下面的代码;

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">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">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

回答by Zim

The modal-headeris display:flex so centering its' content (like the modal-title) works differently. You can use mx-autobut then centering is relative to the close button so it's not exactly centered.

modal-header是显示:弯曲,从而围绕其内容(如modal-title)的工作方式不同。您可以使用mx-auto但是居中是相对于关闭按钮的,因此它不完全居中。

One option is to make the header display:block (d-block) and use text-center.

一种选择是使标题 display:block ( d-block) 并使用text-center.

https://jsfiddle.net/44v0b25k/

https://jsfiddle.net/44v0b25k/

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header d-block">
        <button type="button" class="close float-right" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h5 class="modal-title text-center" id="exampleModalLabel">Modal title</h5>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

Another option is to use w-100on the modal-titleso that it's full width, and text-centerwill also work.

另一种选择是使用w-100modal-title,这样它的全宽,并且text-center还将工作。

<div class="modal-header">
     <h5 class="modal-title w-100 text-center" id="exampleModalLabel">Modal title</h5>
     <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
     </button>
</div>

https://jsfiddle.net/306ob2e5/

https://jsfiddle.net/306ob2e5/

回答by Pete

Below I have added text-center d-blockto the header and d-inline-blockto the title.

下面我添加text-center d-block到标题和d-inline-block标题。

The problem was that the header was flex with justify-content space-between which pushed your title to the left.

问题是标题是弹性的,中间有 justify-content 空间,这将你​​的标题推到了左边。

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header text-center d-block">
        <h5 class="modal-title d-inline-block" id="exampleModalLabel">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">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

Example Bootply

示例 Bootply

回答by DavidG

Just apply CSS to the modal-titleclass, for example:

只需将 CSS 应用于modal-title类,例如:

.modal-title {
    text-align: center;
    width: 100%;
}

Example: https://jsfiddle.net/vxsw0xsy/

示例:https: //jsfiddle.net/vxsw0xsy/