How to Create Modal in Bootstrap: Simple Guide with Example
To create a modal in Bootstrap, use the
modal component by adding a div with class modal and required nested elements. Trigger it with a button using data-bs-toggle="modal" and data-bs-target="#modalId" attributes.Syntax
A Bootstrap modal requires a container div with class modal and an id to identify it. Inside, it has modal-dialog for layout, modal-content for the modal box, and sections like modal-header, modal-body, and modal-footer. A button or link with data-bs-toggle="modal" and data-bs-target="#modalId" opens the modal.
html
<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 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>
Example
This example shows a complete Bootstrap modal with a button that opens it. The modal has a header, body text, and footer with action buttons. It uses Bootstrap 5 classes and attributes for accessibility and animation.
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Modal Example</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-5"> <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal"> Open 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">Welcome</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> This is a simple Bootstrap modal example. </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> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Output
A webpage with a blue button labeled 'Open Modal'. Clicking it opens a centered modal with title 'Welcome', body text, and two buttons 'Close' and 'Save changes'. The modal can be closed by clicking 'Close', the X button, or outside the modal.
Common Pitfalls
- Forgetting to include Bootstrap's JavaScript bundle will prevent the modal from opening.
- Missing
data-bs-toggle="modal"or incorrectdata-bs-targetattribute on the trigger button stops the modal from showing. - Not setting the modal
idor mismatching it with the trigger'sdata-bs-targetcauses no action. - Using older Bootstrap 4 attributes like
data-toggleinstead ofdata-bs-togglein Bootstrap 5 will fail.
html
<!-- Wrong trigger button --> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"> Open Modal </button> <!-- Correct trigger button --> <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal"> Open Modal </button>
Quick Reference
Modal key parts:
data-bs-toggle="modal": tells the button to open a modaldata-bs-target="#modalId": points to the modal'sidmodalclass: main modal containermodal-dialog: modal box wrappermodal-content: modal content areabtn-close: button to close modal
Remember to include Bootstrap CSS and JS bundle for full functionality.
Key Takeaways
Use
data-bs-toggle="modal" and data-bs-target="#id" on a button to open a modal.Structure the modal with
modal, modal-dialog, and modal-content classes.Include Bootstrap's CSS and JavaScript bundle for the modal to work properly.
Ensure the modal's
id matches the trigger's data-bs-target exactly.Use Bootstrap 5 attributes like
data-bs-toggle, not legacy ones.