How do I center a Bootstrap 5 modal vertically on the screen?

Asked on Sep 28, 2025

Answer

In Bootstrap 5, you can center a modal vertically by adding the `modal-dialog-centered` class to the modal dialog. This class ensures the modal is displayed in the center of the screen.
<!-- 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:
  • Use the `modal-dialog-centered` class inside the `modal-dialog` to center the modal vertically.
  • Ensure that the modal is triggered using JavaScript or data attributes for it to display correctly.
  • This approach works for both small and large screens.
✅ Answered with Bootstrap 5 best practices.