A modal is a popup box that appears on top of the page to show important information or ask for user action without leaving the current page.
Modal structure and triggers in Bootsrap
<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>
The data-bs-toggle="modal" attribute on a button tells Bootstrap to open a modal when clicked.
The data-bs-target="#exampleModal" attribute links the button to the modal with the matching id.
<button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#myModal"> Open Modal </button> <div class="modal fade" id="myModal" tabindex="-1" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="myModalLabel">Hello Modal</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> This is a simple modal example. </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> </div> </div> </div> </div>
<button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#confirmModal"> Delete Item </button> <div class="modal fade" id="confirmModal" tabindex="-1" aria-labelledby="confirmLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="confirmLabel">Confirm Delete</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> Are you sure you want to delete this item? </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button> <button type="button" class="btn btn-danger">Delete</button> </div> </div> </div> </div>
This complete example shows a blue button labeled "Open Modal". When clicked, a modal appears with a title, some text, and two buttons: Close and Save changes.
<!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> <main class="container py-5"> <h1>Modal Structure and Triggers</h1> <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 modal popup using Bootstrap 5. </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> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Always include aria-labelledby and aria-hidden for accessibility.
The modal automatically traps keyboard focus when open, helping users who navigate by keyboard.
Use Bootstrap's JavaScript bundle to enable modal functionality.
Modals show content on top of the page without leaving it.
Use data-bs-toggle="modal" and data-bs-target="#id" on buttons to open modals.
Modal structure includes header, body, and footer sections for content and controls.
Practice
Solution
Step 1: Understand Bootstrap modal trigger attributes
Bootstrap requiresdata-bs-toggle="modal"on the trigger element to activate modal functionality.Step 2: Compare options with Bootstrap docs
Only data-bs-toggle="modal" uses the correct Bootstrap 5+ attribute syntax for toggling modals.Final Answer:
data-bs-toggle="modal" -> Option CQuick Check:
Modal trigger attribute = data-bs-toggle="modal" [OK]
- Using old Bootstrap 4 attribute data-toggle instead of data-bs-toggle
- Using class names instead of data attributes
- Forgetting the data-bs-toggle attribute
Solution
Step 1: Identify the correct attribute for modal target
Bootstrap usesdata-bs-targetwith a CSS selector to specify which modal to open.Step 2: Confirm the selector format
The value must be an ID selector starting with#, so#modalExampleis correct.Final Answer:
data-bs-target="#modalExample" -> Option AQuick Check:
Modal target attribute = data-bs-target="#id" [OK]
- Omitting the # in the target selector
- Using data-target instead of data-bs-target
- Using just the modal ID without #
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">Open</button>
<div class="modal fade" id="myModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">Content here</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>Solution
Step 1: Check button attributes for modal trigger
The button hasdata-bs-toggle="modal"anddata-bs-target="#myModal", so it triggers the modal with ID myModal.Step 2: Understand modal structure and behavior
The modal has correct structure and Bootstrap JS will add the show class dynamically to display it. aria-hidden="true" is default and toggled automatically.Final Answer:
The modal with title and content appears on top of the page -> Option AQuick Check:
Modal triggers show modal content on click [OK]
- Thinking modal needs show class initially
- Confusing aria-hidden attribute as an error
- Assuming button reloads page without type="button"
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="myModal">Open</button>
Solution
Step 1: Check data-bs-target attribute format
The value must be a CSS selector with # for ID. Here it is missing the # before myModal.Step 2: Verify other attributes
Button type="button" is recommended but not an error. data-bs-toggle is correct for Bootstrap 5+. Button can be outside modal.Final Answer:
Missing # in data-bs-target value -> Option BQuick Check:
Modal target needs #id selector [OK]
- Omitting # in data-bs-target
- Confusing Bootstrap 4 and 5 attribute names
- Thinking button must be inside modal
Solution
Step 1: Confirm trigger element setup
Using an anchor withhref="#",data-bs-toggle="modal", anddata-bs-target="#myModal"correctly triggers the modal.Step 2: Check modal attributes for accessibility and behavior
Modal needstabindex="-1"for focus management anddata-bs-backdrop="true"(default) to close on outside click and Escape key.Final Answer:
Anchor with data-bs-toggle and data-bs-target plus modal with tabindex="-1" and data-bs-backdrop="true" -> Option DQuick Check:
Modal triggers + tabindex + backdrop enable open/close [OK]
- Using data-toggle instead of data-bs-toggle
- Missing tabindex="-1" on modal
- Disabling keyboard with data-bs-keyboard="false" prevents Escape close
Ready to prove it?
Pass the test and this pattern moves unknown
