
What's the best way to center align a Bootstrap modal vertically on the page?
Asked on Sep 28, 2025
Answer
In Bootstrap 5, centering a modal vertically is straightforward using the built-in modal-dialog-centered class. This class ensures the modal is centered vertically within the viewport.
<!-- BEGIN COPY / PASTE -->
<div class="modal fade" id="myModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
This is a vertically centered modal.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<!-- END COPY / PASTE -->
Additional Comment:
✅ Answered with Bootstrap 5 best practices.- The modal-dialog-centered class is added to the modal-dialog div to center the modal vertically.
- Ensure that your modal is initialized correctly with the necessary Bootstrap JavaScript components.
- Use data-bs-dismiss and data-bs-toggle attributes for Bootstrap's modal functionality.
Recommended Links:
← Back to All Questions