Bootstrap 4模态中心内容 [英] Bootstrap 4 Modal Center Content

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

问题描述

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

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

我尝试了text-centermx-automl-0 / mr-0,但是它们似乎没有用.

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

这是链接到小提琴.

下面的代码;

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

推荐答案

modal-header是display:flex,因此居中放置其内容(例如modal-title)的方式有所不同.您可以使用mx-auto,但是居中是相对于关闭按钮的,因此它并不是完全居中.

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.

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

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>

另一种选择是在modal-title上使用w-100使其全角,并且text-center也将起作用.

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模态中心内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆