Modals help show important messages or content on top of the page. Changing their size and position makes them fit better for different needs.
Modal sizes and positioning in Bootsrap
<div class="modal-dialog modal-sm"> <div class="modal-content"> <!-- Modal content here --> </div> </div>
Use modal-sm, modal-lg, or modal-xl classes on modal-dialog to change size.
By default, modals are centered horizontally. You can add modal-dialog-centered to center vertically.
<div class="modal-dialog modal-sm"> <div class="modal-content"> <!-- Small modal content --> </div> </div>
<div class="modal-dialog modal-lg modal-dialog-centered"> <div class="modal-content"> <!-- Large centered modal content --> </div> </div>
<div class="modal-dialog modal-xl"> <div class="modal-content"> <!-- Extra large modal content --> </div> </div>
This example shows three buttons that open modals of different sizes and positions. The small modal is compact, the large modal is centered vertically, and the extra large modal is very wide. All modals have accessible labels and close buttons.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Modal Sizes and Positioning</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" /> </head> <body> <div class="container py-4"> <h1>Modal Sizes and Positioning Demo</h1> <button type="button" class="btn btn-primary me-2" data-bs-toggle="modal" data-bs-target="#smallModal"> Small Modal </button> <button type="button" class="btn btn-success me-2" data-bs-toggle="modal" data-bs-target="#largeCenteredModal"> Large Centered Modal </button> <button type="button" class="btn btn-warning" data-bs-toggle="modal" data-bs-target="#extraLargeModal"> Extra Large Modal </button> </div> <!-- Small Modal --> <div class="modal fade" id="smallModal" tabindex="-1" aria-labelledby="smallModalLabel" aria-hidden="true"> <div class="modal-dialog modal-sm"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="smallModalLabel">Small Modal</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> This is a small modal for quick messages. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> </div> </div> </div> </div> <!-- Large Centered Modal --> <div class="modal fade" id="largeCenteredModal" tabindex="-1" aria-labelledby="largeCenteredModalLabel" aria-hidden="true"> <div class="modal-dialog modal-lg modal-dialog-centered"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="largeCenteredModalLabel">Large Centered Modal</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> This modal is large and vertically centered on the screen. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> </div> </div> </div> </div> <!-- Extra Large Modal --> <div class="modal fade" id="extraLargeModal" tabindex="-1" aria-labelledby="extraLargeModalLabel" aria-hidden="true"> <div class="modal-dialog modal-xl"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="extraLargeModalLabel">Extra Large Modal</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> This modal is extra large, perfect for showing a lot of content. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> </div> </div> </div> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Always add aria-labelledby and aria-hidden for accessibility.
Use modal-dialog-centered to center modals vertically.
Test modals on different screen sizes to ensure they look good everywhere.
Use modal-sm, modal-lg, and modal-xl classes to control modal size.
Add modal-dialog-centered to center modals vertically on the screen.
Always include accessible labels and test responsiveness for best user experience.
Practice
Solution
Step 1: Understand modal size classes
Bootstrap provides specific classes to control modal sizes:modal-smfor small,modal-lgfor large, andmodal-xlfor extra large modals.Step 2: Identify the class for smaller modal
The classmodal-smis used to make the modal smaller than the default size.Final Answer:
<code>modal-sm</code> -> Option DQuick Check:
Small modal = modal-sm [OK]
- Using modal-lg or modal-xl for small size
- Confusing modal-centered with size classes
- Not adding any size class and expecting smaller modal
Solution
Step 1: Identify the correct class for vertical centering
Bootstrap uses the classmodal-dialog-centeredon themodal-dialogelement to vertically center the modal.Step 2: Verify the element to apply the class
The class must be added to themodal-dialogdiv, notmodalormodal-content.Final Answer:
Add modal-dialog-centered to the modal-dialog div -> Option AQuick Check:
Vertical center = modal-dialog-centered on modal-dialog [OK]
- Adding centering class to wrong modal element
- Using non-existent classes like modal-center
- Confusing modal-dialog-centered with modal size classes
<div class="modal-dialog modal-xl">
<div class="modal-content">
<!-- Modal body -->
</div>
</div>Solution
Step 1: Identify the size class used
The classmodal-xlis applied to themodal-dialogdiv, which sets the modal to extra large size.Step 2: Understand the effect of modal-xl
This class makes the modal wider than default and large sizes, so the modal will appear extra large on the screen.Final Answer:
Extra large modal dialog -> Option CQuick Check:
modal-xl = extra large modal [OK]
- Confusing modal-xl with modal-lg or modal-sm
- Thinking modal-xl centers the modal
- Ignoring the class and assuming default size
A) <div class="modal-dialog modal-lg modal-dialog-centered"> B) <div class="modal-dialog modal-centered modal-lg"> C) <div class="modal-dialog modal-lg-centered"> D) <div class="modal-dialog modal-lg center-dialog">
Solution
Step 1: Check correct Bootstrap classes for size and centering
The correct classes aremodal-lgfor large size andmodal-dialog-centeredfor vertical centering.Step 2: Verify the class names and order
A uses both classes correctly on themodal-dialogdiv. Other options use incorrect or non-existent classes.Final Answer:
Option A -> Option AQuick Check:
modal-lg + modal-dialog-centered = A [OK]
- Using wrong class names like modal-centered or modal-lg-centered
- Placing classes on wrong elements
- Missing one of the required classes
modal-dialog to achieve this?Solution
Step 1: Understand Bootstrap modal size classes
Bootstrap modal size classes likemodal-sm,modal-lg, andmodal-xlare not responsive by themselves and cannot be combined to change size at different screen widths.Step 2: Identify how to make modal responsive
To have different modal sizes on different devices, you must usemodal-dialog-centeredfor vertical centering and write custom CSS media queries to adjust modal width responsively.Final Answer:
modal-dialog modal-dialog-centered plus custom CSS media queries for sizes -> Option BQuick Check:
Responsive modal sizes require custom CSS + modal-dialog-centered [OK]
- Trying to combine multiple size classes on modal-dialog
- Assuming Bootstrap size classes are responsive by default
- Not adding vertical centering class
Ready to prove it?
Pass the test and this pattern moves unknown
