Alerts help show important messages to users in a clear and colorful way.
Alert variants and colors in Bootsrap
<div class="alert alert-variant" role="alert">Your message here</div>
Replace alert-variant with the color style you want, like alert-success or alert-danger.
The role="alert" helps screen readers know this is an important message.
<div class="alert alert-primary" role="alert">This is a primary alert.</div>
<div class="alert alert-success" role="alert">Success! Your action worked.</div>
<div class="alert alert-warning" role="alert">Warning! Check this carefully.</div>
<div class="alert alert-danger" role="alert">Error! Something went wrong.</div>
This page shows different alert boxes with colors from Bootstrap. Each alert uses a different variant class to show how messages can look.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Bootstrap Alert Variants</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-4"> <h1>Bootstrap Alert Variants and Colors</h1> <div class="alert alert-primary" role="alert">This is a primary alert.</div> <div class="alert alert-success" role="alert">Success! Your action worked.</div> <div class="alert alert-warning" role="alert">Warning! Check this carefully.</div> <div class="alert alert-danger" role="alert">Error! Something went wrong.</div> <div class="alert alert-info" role="alert">Info: Here is some information.</div> <div class="alert alert-light" role="alert">Light alert with subtle background.</div> <div class="alert alert-dark" role="alert">Dark alert with strong background.</div> </main> </body> </html>
Use semantic colors to match the message type for better user understanding.
Alerts are responsive and will adjust on different screen sizes.
Always include role="alert" for accessibility.
Alerts show messages with different colors using alert-variant classes.
Common variants include primary, success, warning, danger, info, light, and dark.
Use alerts to clearly communicate status or important info to users.
Practice
<div class="alert ?" role="alert">Success message</div>Solution
Step 1: Understand Bootstrap alert color meanings
Bootstrap usesalert-successfor green alerts indicating success.Step 2: Match the class to the color green
Thealert-successclass applies green background and border to the alert box.Final Answer:
alert-success -> Option BQuick Check:
Green alert = alert-success [OK]
- Using alert-danger for success (red color)
- Using alert-warning which is yellow
- Confusing alert-info with success
<div class="alert ?" role="alert">Info message</div>Solution
Step 1: Identify the class for informational alerts
Bootstrap usesalert-infofor blue background alerts that show info messages.Step 2: Confirm syntax correctness
The classalert-infois the correct class to apply blue styling for info alerts.Final Answer:
alert-info -> Option CQuick Check:
Info alert = alert-info [OK]
- Using alert-primary which is darker blue but not info
- Using alert-light which is very pale
- Using alert-dark which is black/dark gray
<div class="alert alert-warning" role="alert">Warning!</div>
Solution
Step 1: Identify the alert variant class
The classalert-warningis used for warning alerts in Bootstrap.Step 2: Recall the color for warning alerts
Bootstrap stylesalert-warningwith a yellow background to indicate caution or warning.Final Answer:
Yellow background -> Option AQuick Check:
Warning alert = yellow background [OK]
- Confusing warning with danger (red)
- Thinking warning is green
- Mixing up warning with info (blue)
<div class="alert alert-danger">Error occurred</div>
What is missing or incorrect?
Solution
Step 1: Check the alert classes used
The code uses bothalertandalert-dangerclasses correctly, so the styling should apply.Step 2: Identify missing accessibility attribute
Bootstrap recommends addingrole="alert"for screen readers to recognize the alert properly.Final Answer:
Missing role="alert" attribute for accessibility -> Option DQuick Check:
Alerts need role="alert" for proper behavior [OK]
- Thinking alert-danger class is wrong
- Forgetting to add alert base class
- Assuming alert-danger does not exist
status in JavaScript. Which approach correctly applies the alert variant class dynamically?const status = 'danger';
const alertDiv = document.createElement('div');
alertDiv.className = 'alert alert-' + status;
alertDiv.setAttribute('role', 'alert');
alertDiv.textContent = 'Status message';
document.body.appendChild(alertDiv);Solution
Step 1: Understand dynamic class assignment
The code concatenates 'alert-' with the variablestatusto form 'alert-danger', which is valid.Step 2: Verify role attribute and appending
The role is correctly set to 'alert' for accessibility, and the element is appended to the document body.Final Answer:
This code correctly applies the alert-danger class dynamically -> Option AQuick Check:
Dynamic class with 'alert-' + status works [OK]
- Hardcoding class instead of using variable
- Setting wrong role attribute
- Not appending alert to the DOM
Ready to prove it?
Pass the test and this pattern moves unknown
