Bootstrap Q&A Logo
Bootstrap Q&A Part of the Q&A Network

What is the best way to center a modal vertically in Bootstrap?

Asked on Oct 08, 2025

Answer

In Bootstrap 5, you can center a modal vertically by using the `modal-dialog-centered` class. This class ensures that the modal is centered vertically 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:
  • The `modal-dialog-centered` class is applied to the `modal-dialog` div to center the modal vertically.
  • Ensure your modal is triggered correctly with data attributes or JavaScript.
  • This approach is compatible with Bootstrap 5 and above.
✅ Answered with Bootstrap 5 best practices.
← Back to All Questions