📖 Scenario: You are building a simple webpage that shows different alert messages to users. Each alert has a different color and style to show its importance or type.
🎯 Goal: Create a webpage that displays five Bootstrap alert boxes with different variants and colors: primary, secondary, success, warning, and danger. Each alert should have the correct Bootstrap class and show a short message describing its type.
📋 What You'll Learn
Use Bootstrap 5 alert classes for styling
Create five alert boxes with classes: alert-primary, alert-secondary, alert-success, alert-warning, alert-danger
Each alert box should contain text describing its variant, e.g., 'This is a primary alert'
Use semantic HTML5 structure with a section
Include the Bootstrap CSS link in the for styling
💡 Why This Matters
🌍 Real World
Websites often need to show messages to users, like success confirmations or warnings. Using Bootstrap alerts helps create consistent, colorful messages that users can easily understand.
💼 Career
Knowing how to use Bootstrap alerts is useful for front-end developers to quickly build user-friendly interfaces with clear feedback messages.
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 tags. Include the Bootstrap 5 CSS CDN link inside the <head>. Add a <main> element with a comment inside it.
Bootsrap
Hint
Remember to include the Bootstrap CSS link inside the <head> section so the alerts get styled properly.
2
Add a primary alert box inside the main section
Inside the <main> element, add a <div> with the classes alert and alert-primary. Inside this div, write the text: This is a primary alert.
Bootsrap
Hint
Use a div with classes alert and alert-primary inside the main element.
3
Add four more alert boxes with secondary, success, warning, and danger variants
Below the primary alert div, add four more div elements with classes alert alert-secondary, alert alert-success, alert alert-warning, and alert alert-danger. Each should contain text describing its variant exactly as: This is a secondary alert, This is a success alert, This is a warning alert, and This is a danger alert respectively.
Bootsrap
Hint
Each alert uses the alert class plus its variant class like alert-success. Write them one after another inside main.
4
Add aria roles and container for accessibility and layout
Wrap all alert divs inside a <section> with role="alert" attribute for accessibility. Also, add a Bootstrap container class by wrapping the section inside a <div> with class container mt-4 inside the <main>. This will center the alerts and add margin on top.
Bootsrap
Hint
Use a div with class container mt-4 inside main. Inside that, add a section with role="alert" wrapping all alert divs.
Practice
(1/5)
1. Which Bootstrap class would you use to create a green alert box indicating success? <div class="alert ?" role="alert">Success message</div>
easy
A. alert-warning
B. alert-success
C. alert-danger
D. alert-info
Solution
Step 1: Understand Bootstrap alert color meanings
Bootstrap uses alert-success for green alerts indicating success.
Step 2: Match the class to the color green
The alert-success class applies green background and border to the alert box.
Final Answer:
alert-success -> Option B
Quick Check:
Green alert = alert-success [OK]
Hint: Success alerts are always green with alert-success class [OK]
Common Mistakes:
Using alert-danger for success (red color)
Using alert-warning which is yellow
Confusing alert-info with success
2. Which of the following is the correct syntax to create a Bootstrap alert with a blue background for informational messages? <div class="alert ?" role="alert">Info message</div>
easy
A. alert-light
B. alert-primary
C. alert-info
D. alert-dark
Solution
Step 1: Identify the class for informational alerts
Bootstrap uses alert-info for blue background alerts that show info messages.
Step 2: Confirm syntax correctness
The class alert-info is the correct class to apply blue styling for info alerts.
Final Answer:
alert-info -> Option C
Quick Check:
Info alert = alert-info [OK]
Hint: Info alerts use alert-info class for blue color [OK]
Common Mistakes:
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
3. What color will the alert box have when using this code?
A. The alert-danger class does not exist in Bootstrap
B. Class name should be danger instead of alert-danger
C. The alert class is missing, only alert-danger is present
D. Missing role="alert" attribute for accessibility
Solution
Step 1: Check the alert classes used
The code uses both alert and alert-danger classes correctly, so the styling should apply.
Step 2: Identify missing accessibility attribute
Bootstrap recommends adding role="alert" for screen readers to recognize the alert properly.
Final Answer:
Missing role="alert" attribute for accessibility -> Option D
Quick Check:
Alerts need role="alert" for proper behavior [OK]
Hint: Always add role="alert" for Bootstrap alerts [OK]
Common Mistakes:
Thinking alert-danger class is wrong
Forgetting to add alert base class
Assuming alert-danger does not exist
5. You want to create a Bootstrap alert that changes color based on a variable status in JavaScript. Which approach correctly applies the alert variant class dynamically?