
What's the best way to center a Bootstrap modal vertically on the screen?
Asked on Oct 16, 2025
Answer
Bootstrap 5 provides a straightforward way to center a modal vertically on the screen using the `modal-dialog-centered` class. This class ensures that the modal is vertically aligned within the viewport.
<!-- BEGIN COPY / PASTE -->
<div class="modal fade" id="exampleModal" 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">
Modal body content goes here.
</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.- Use the `modal-dialog-centered` class on the `modal-dialog` element to achieve vertical centering.
- Ensure your modal is triggered correctly using data attributes or JavaScript.
- The modal will automatically adjust its position based on the viewport size.
Recommended Links:
← Back to All Questions