Bootstrap 4 Modal Center Content
Below I have added text-center d-block
to the header and d-inline-block
to the title.
The problem was that the header was flex with justify-content space-between which pushed your title to the left.
<!-- 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">×</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
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">×</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">×</span>
</button>
</div>
https://jsfiddle.net/306ob2e5/