📖 Scenario: You are creating a simple webpage that uses Bootstrap to show a modal popup. This modal will display a welcome message when the user clicks a button.
🎯 Goal: Build a Bootstrap modal with a button that opens it. The modal should have a header, body text, and a close button.
📋 What You'll Learn
Use Bootstrap 5 modal structure
Create a button that triggers the modal
Include modal header with a title
Include modal body with a welcome message
Include modal footer with a close button
💡 Why This Matters
🌍 Real World
Modals are common in websites for showing dialogs, alerts, forms, or extra information without leaving the current page.
💼 Career
Knowing how to implement modals with Bootstrap is useful for front-end developers to create interactive and user-friendly interfaces quickly.
Progress0 / 4 steps
1
Create the basic HTML structure with Bootstrap CSS link
Write the basic HTML5 skeleton with lang="en", charset="UTF-8", and viewport meta tag. Include the Bootstrap 5 CSS CDN link inside the <head>. Add an empty <body> section.
Bootsrap
Hint
Remember to include the Bootstrap CSS link inside the <head> section.
2
Add a button that triggers the modal
Inside the <body>, add a Bootstrap button with type="button", class btn btn-primary, and attributes data-bs-toggle="modal" and data-bs-target="#welcomeModal". The button text should be Open Modal.
Bootsrap
Hint
Use data-bs-toggle="modal" and data-bs-target="#welcomeModal" on the button to link it to the modal.
3
Add the modal HTML structure
Below the button, add a <div> with class="modal fade" and id="welcomeModal". Inside it, add div elements with classes modal-dialog and modal-content. Inside modal-content, add modal-header with a h5 title Welcome and a close button with data-bs-dismiss="modal". Add modal-body with the text Welcome to our website!. Add modal-footer with a close button that also dismisses the modal.
Bootsrap
Hint
Use the Bootstrap modal structure with correct classes and data-bs-dismiss="modal" on close buttons.
4
Add Bootstrap JavaScript bundle for modal functionality
Before the closing </body> tag, add the Bootstrap 5 JavaScript bundle CDN script tag with src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js". This enables the modal to open and close properly.
Bootsrap
Hint
Include the Bootstrap JavaScript bundle before the closing </body> tag to enable modal functionality.
Practice
(1/5)
1. What attribute do you add to a button to open a Bootstrap modal when clicked?
A. The modal with title and content appears on top of the page
B. Nothing happens because the modal is missing the show class
C. The page reloads because the button has no type
D. An error appears because aria-hidden is true
Solution
Step 1: Check button attributes for modal trigger
The button has data-bs-toggle="modal" and data-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 A
Quick Check:
Modal triggers show modal content on click [OK]
Hint: Modal shows on click if data-bs-toggle and data-bs-target are correct [OK]
Common Mistakes:
Thinking modal needs show class initially
Confusing aria-hidden attribute as an error
Assuming button reloads page without type="button"
4. Identify the error in this modal trigger button code:
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 B
Quick Check:
Modal target needs #id selector [OK]
Hint: Always prefix modal ID with # in data-bs-target [OK]
Common Mistakes:
Omitting # in data-bs-target
Confusing Bootstrap 4 and 5 attribute names
Thinking button must be inside modal
5. You want a modal that opens when a link is clicked and closes when the user presses Escape or clicks outside. Which is the correct minimal setup for the link and modal?
hard
A. <button data-bs-toggle="modal" data-bs-target="#myModal">Open</button> and modal with data-bs-keyboard="false"
B. <a href="#myModal" data-toggle="modal">Open</a> and modal with tabindex="0"
C. <a href="#" data-bs-toggle="modal" data-bs-target="myModal">Open</a> and modal with tabindex="-1" only
D. <a href="#" data-bs-toggle="modal" data-bs-target="#myModal">Open</a> and modal with tabindex="-1" and data-bs-backdrop="true"
Solution
Step 1: Confirm trigger element setup
Using an anchor with href="#", data-bs-toggle="modal", and data-bs-target="#myModal" correctly triggers the modal.
Step 2: Check modal attributes for accessibility and behavior
Modal needs tabindex="-1" for focus management and data-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 D