Dismissible alerts let users close messages they don't want to see anymore. This keeps the page clean and easy to use.
Dismissible alerts in Bootsrap
<div class="alert alert-warning alert-dismissible fade show" role="alert"> Your message here. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div>
The alert-dismissible class makes the alert closable.
The btn-close button triggers the closing action with data-bs-dismiss="alert".
<div class="alert alert-success alert-dismissible fade show" role="alert"> Success! Your action worked. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div>
<div class="alert alert-danger alert-dismissible fade show" role="alert"> Error! Something went wrong. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div>
<div class="alert alert-info alert-dismissible fade show" role="alert"> Info: Remember to save your work. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div>
This page shows a yellow warning alert with a close button. When you click the close button, the alert disappears smoothly.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Dismissible Alert 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 mt-5"> <h1>Dismissible Alert Demo</h1> <div class="alert alert-warning alert-dismissible fade show" role="alert"> <strong>Warning!</strong> Please check your input. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div> </main> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> </body> </html>
Make sure to include Bootstrap's JavaScript bundle for the dismiss feature to work.
The fade show classes add a smooth fade-out effect when closing.
Use aria-label="Close" on the button for screen reader accessibility.
Dismissible alerts let users close messages to keep the page tidy.
Use alert-dismissible and a btn-close button with data-bs-dismiss="alert".
Always include Bootstrap's JS for the close button to work properly.
Practice
dismissible alert in Bootstrap?Solution
Step 1: Understand the role of dismissible alerts
Dismissible alerts let users close alert messages so the page looks tidy and less cluttered.Step 2: Compare options with this purpose
Only To allow users to close alert messages and keep the page clean describes this purpose correctly; others describe unrelated features.Final Answer:
To allow users to close alert messages and keep the page clean -> Option BQuick Check:
Dismissible alerts = user can close alerts [OK]
- Thinking dismissible changes alert color
- Confusing dismissible with animation only
- Believing dismissible disables alerts
Solution
Step 1: Recall Bootstrap's dismissible alert class
The official class to make an alert dismissible isalert-dismissiblecombined withalert.Step 2: Check each option
Only alert alert-dismissible uses the correct class names exactly as Bootstrap requires.Final Answer:
alert alert-dismissible -> Option AQuick Check:
Dismissible alert class = alert-dismissible [OK]
- Using incorrect class names like dismissible-alert
- Missing the alert class
- Using non-Bootstrap class names
<div class="alert alert-warning alert-dismissible fade show" role="alert"> Warning! Check your input. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div>
Solution
Step 1: Understand the alert structure and classes
The alert usesalert-dismissible,fade, andshowclasses with a close button havingdata-bs-dismiss="alert".Step 2: Know Bootstrap's behavior on close button click
Clicking the button triggers Bootstrap's JavaScript to fade out and remove the alert from the page.Final Answer:
The alert will fade out and disappear from the page -> Option DQuick Check:
Close button with data-bs-dismiss = alert hides alert [OK]
- Thinking alert text changes on close
- Assuming no effect without extra JS code
- Believing page reloads on close
<div class="alert alert-success alert-dismissible" role="alert"> Success! Your form was submitted. <button type="button" class="btn-close" aria-label="Close"></button> </div>
Solution
Step 1: Check the close button attributes
The close button must havedata-bs-dismiss="alert"to trigger alert dismissal.Step 2: Verify other classes and attributes
The alert-dismissible class is correct, fade is optional, and button type should be button, not submit.Final Answer:
Missing data-bs-dismiss="alert" on the close button -> Option CQuick Check:
Close button needs data-bs-dismiss attribute [OK]
- Forgetting data-bs-dismiss attribute
- Thinking fade class is mandatory
- Using wrong button type
Solution
Step 1: Understand Bootstrap's dismissible alert features
Bootstrap does not provide automatic dismiss with a data attribute; manual close requiresbtn-closeanddata-bs-dismiss="alert".Step 2: Implement auto-dismiss with JavaScript
To auto-remove after 5 seconds, add JavaScript code that removes the alert element after a timeout, while keeping manual close button.Final Answer:
Use a dismissible alert with btn-close and add JavaScript to remove it after 5 seconds -> Option AQuick Check:
Auto-dismiss needs JS plus dismissible alert [OK]
- Assuming data-bs-auto-dismiss exists
- Omitting the close button for manual close
- Using non-existent alert-auto-close class
Ready to prove it?
Pass the test and this pattern moves unknown
