AA popup window appears with the title 'Modal title', body text 'This is the modal body content.', and two buttons labeled 'Close' and 'Save changes'.
BNothing happens because the modal is missing the 'show' class.
CThe page navigates to a new URL because the button is a link.
DAn error message appears because the modal ID does not match the button's data-bs-target.
Attempts:
2 left
💡 Hint
Look at the button's data attributes and the modal's ID to understand the trigger.
✗ Incorrect
The button uses data-bs-toggle="modal" and data-bs-target="#exampleModal" which triggers the modal with ID 'exampleModal' to appear. The modal content includes a header, body, and footer with buttons. The modal is hidden initially but appears on button click.
🧠 Conceptual
intermediate
1:30remaining
Which attribute correctly links a button to open a Bootstrap modal?
You want a button to open a modal with ID 'myModal'. Which attribute and value should you add to the button?
Adata-toggle="modal" href="#myModal"
Bdata-bs-toggle="modal" data-bs-target="#myModal"
Conclick="openModal('myModal')"
Daria-controls="myModal"
Attempts:
2 left
💡 Hint
Bootstrap 5 uses 'data-bs-' prefix for JavaScript behavior.
✗ Incorrect
In Bootstrap 5, to trigger a modal, the button needs data-bs-toggle="modal" and data-bs-target="#modalID". The other options either use old syntax or are unrelated.
📝 Syntax
advanced
2:00remaining
What error occurs if the modal's HTML is missing the 'modal-dialog' div?
Consider this modal HTML missing the
wrapper. What happens when the modal is triggered?
BBootstrap throws a JavaScript error and modal does not open.
CThe modal opens normally with no visible difference.
DThe modal appears but content is not centered or styled properly.
Attempts:
2 left
💡 Hint
The 'modal-dialog' class controls modal positioning and styling.
✗ Incorrect
The 'modal-dialog' div is required to center and style the modal content. Without it, the modal content appears but is not positioned or styled correctly, causing layout issues but no JS errors.
❓ accessibility
advanced
1:30remaining
Which attribute improves accessibility by linking modal title to the modal dialog?
In Bootstrap modals, which attribute on the modal container connects the modal title for screen readers?
Aaria-labelledby="modalTitleId"
Baria-describedby="modalTitleId"
Caria-hidden="false"
Drole="dialog"
Attempts:
2 left
💡 Hint
This attribute points to the element that labels the modal.
✗ Incorrect
The 'aria-labelledby' attribute on the modal container references the ID of the modal's title element, helping screen readers announce the modal's title. 'aria-describedby' is for description, 'role' defines the element type but does not link the title.
❓ selector
expert
2:30remaining
Which CSS selector targets only the visible Bootstrap modal dialog?
You want to style only the modal dialog that is currently visible (shown) in Bootstrap 5. Which CSS selector correctly selects it?
A.modal-dialog.active
B.modal-dialog:visible
C.modal.show .modal-dialog
D.modal[aria-hidden="false"] .modal-dialog
Attempts:
2 left
💡 Hint
Bootstrap adds the 'show' class to visible modals.
✗ Incorrect
Bootstrap adds the 'show' class to the modal container when visible. The selector '.modal.show .modal-dialog' targets the dialog inside a visible modal. ':visible' is not a valid CSS selector, '.active' is not used by Bootstrap for modals, and 'aria-hidden' is a string attribute but not reliable for CSS selection.
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