本文介绍了Bootstrap 4 模态中心内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下如何在 Bootstrap 4 模式中水平居中标题.

我试过 text-centermx-automl-0/mr-0 但它们似乎没有工作.

这是一个链接到小提琴.

下面的代码;

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">启动演示模式<!-- 模态--><div class="modalfade" 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">模态标题</h5><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>

<div class="modal-body">...

<div class="modal-footer"><button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button><button type="button" class="btn btn-primary">保存更改</button>

解决方案

modal-header 是 display:flex 所以它的内容居中(就像 modal-title) 的工作方式不同.您可以使用 mx-auto 但居中是相对于关闭按钮的,因此它不是完全居中.

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

https://jsfiddle.net/44v0b25k/

<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><h5 class="modal-title text-center" id="exampleModalLabel">模态标题</h5>

<div class="modal-body">...

<div class="modal-footer"><button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button><button type="button" class="btn btn-primary">保存更改</button>

另一种选择是在 modal-title 上使用 w-100 使其全宽,并且 text-center 也可以使用.

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

https://jsfiddle.net/306ob2e5/

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

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

Here's a link to 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>
解决方案

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

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

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-100 on the modal-title so that it's full width, and text-center will also work.

<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/

这篇关于Bootstrap 4 模态中心内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:06