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

How do I center a Bootstrap modal both vertically and horizontally on the page?

Asked on Oct 15, 2025

Answer

To center a Bootstrap modal both vertically and horizontally, you can use the `modal-dialog-centered` class. This class ensures that the modal is perfectly centered on the page.
<!-- 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">Centered Modal</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        This is a vertically and horizontally 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:
  • The `modal-dialog-centered` class is used to vertically center the modal.
  • Ensure that the modal is triggered correctly using JavaScript or data attributes.
  • The modal will be horizontally centered by default in Bootstrap 5.
  • You can customize the modal further by adding additional classes or styles to the modal elements.
✅ Answered with Bootstrap 5 best practices.
← Back to All Questions