Modals in Bootstrap
Learn how to use Bootstrap modals to create interactive and responsive dialog boxes.
1. What is a Modal?
A modal is a dialog box/popup window that is displayed on top of the current page. It’s used to grab user attention without navigating away from the page.
2. Basic Modal Example
Create a simple modal using Bootstrap:
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal"> Launch Modal </button> <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog"> <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 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>
3. Modal Components
A modal consists of the following components:
- Modal Header: Contains the title and close button.
- Modal Body: Holds the main content of the modal.
- Modal Footer: Includes action buttons like Save and Close.
4. Launching a Modal
Modals can be launched using buttons, links, or JavaScript:
- Use
data-bs-toggle="modal"
anddata-bs-target="#modalID"
attributes. - Trigger programmatically using JavaScript:
let myModal = new bootstrap.Modal(document.getElementById('exampleModal')); myModal.show();
5. Static Modals
Prevent a modal from closing when clicking outside it:
<div class="modal fade" data-bs-backdrop="static" data-bs-keyboard="false"> ... </div>
6. Centered Modals
Align modals vertically using .modal-dialog-centered
:
<div class="modal-dialog modal-dialog-centered"> ... </div>
7. Scrolling Modals
Enable scrolling for long modal content using .modal-dialog-scrollable
:
<div class="modal-dialog modal-dialog-scrollable"> ... </div>
8. Modal Sizes
Modals can have different sizes using size classes:
.modal-sm
for small modals.modal-lg
for large modals.modal-xl
for extra-large modals
9. Customizing Modals
Style modals using custom CSS or Bootstrap utility classes:
<div class="modal fade text-white bg-dark"> ... </div>
10. Accessibility in Modals
- Ensure proper use of
aria-labelledby
andaria-hidden
attributes. - Maintain focus inside the modal using Bootstrap's built-in focus trapping.
11. Animations in Modals
Bootstrap modals include fade-in animations by default. Disable with .modal
instead of .modal.fade
.
12. Nested Modals
Avoid using nested modals as they can cause usability issues, but if necessary, handle their stacking with z-index.
13. Modal Events
Listen to modal lifecycle events like show, shown, hide, and hidden:
document.getElementById('exampleModal').addEventListener('shown.bs.modal', function () { console.log('Modal is shown'); });
14. Loading Content Dynamically
Load content into the modal dynamically using AJAX or JavaScript to update the modal body.