📖 Scenario: You are building a simple web page that shows different alert messages to users. These alerts can be success, warning, or error messages. You want to style them nicely using Tailwind CSS so they look clear and easy to read.
🎯 Goal: Create a web page with three alert boxes: one success alert, one warning alert, and one error alert. Each alert should have a colored background, an icon, and a message. Use Tailwind CSS classes to style them with good spacing, colors, and rounded corners.
📋 What You'll Learn
Use Tailwind CSS utility classes for styling
Create three alert boxes with distinct colors for success, warning, and error
Include an icon (using emoji) for each alert type
Add accessible roles and aria-labels for screen readers
Make sure the alerts have padding, margin, and rounded corners
💡 Why This Matters
🌍 Real World
Alerts and notifications are common in websites and apps to inform users about success, warnings, or errors. Styling them accessibly and clearly improves user experience.
💼 Career
Front-end developers often build notification components. Knowing how to use semantic HTML, ARIA roles, and utility-first CSS frameworks like Tailwind is valuable for creating professional UI components.
Progress0 / 4 steps
1
Create the HTML skeleton with a container
Create a basic HTML5 structure with lang="en", charset="UTF-8", and a viewport meta tag. Inside the <body>, add a <main> element with a class of max-w-md mx-auto mt-10 to center the content and limit its width.
Tailwind
Hint
Start by setting up a clean HTML page with Tailwind CSS included. Use a <main> container with classes to center and add margin.
2
Add a success alert box with Tailwind classes
Inside the <main>, add a <section> with role="alert" and aria-label="Success alert". Give it Tailwind classes bg-green-100 text-green-800 p-4 rounded mb-4 flex items-center. Inside it, add a <span> with the emoji ✔️ and a <p> with the text Success! Your operation completed..
Tailwind
Hint
Use a <section> with role="alert" and add Tailwind classes for green background and text. Add emoji and message inside.
3
Add warning and error alert boxes below the success alert
Below the success alert, add two more <section> elements with role="alert" and appropriate aria-label attributes: Warning alert and Error alert. Style the warning alert with bg-yellow-100 text-yellow-800 and the error alert with bg-red-100 text-red-800. Each alert should have padding, rounded corners, margin bottom, and use flex items-center. Include emoji ⚠️ for warning and ❌ for error, with messages: Warning! Check your input. and Error! Something went wrong. respectively.
Tailwind
Hint
Add two more <section> alerts with correct roles, aria-labels, colors, emoji, and messages below the success alert.
4
Make alerts responsive and improve spacing
Add Tailwind classes to the <main> element to add horizontal padding on small screens using px-4. Also, add space-y-4 to the <main> to create vertical spacing between the alert sections instead of using margin bottom on each alert. Remove the mb-4 classes from each alert <section>.
Tailwind
Hint
Add px-4 and space-y-4 to the <main> for padding and spacing. Remove mb-4 from alerts.
Practice
(1/5)
1. What is the main purpose of an alert in a web page using Tailwind CSS?
easy
A. To decorate the page with colorful boxes
B. To quickly inform users about important messages or statuses
C. To create navigation menus
D. To add animations to images
Solution
Step 1: Understand the role of alerts
Alerts are designed to catch user attention for important information like success, errors, or warnings.
Step 2: Differentiate alerts from other UI elements
Alerts are not for decoration or navigation but for communication of status or messages.
Final Answer:
To quickly inform users about important messages or statuses -> Option B
Quick Check:
Alerts = Important user messages [OK]
Hint: Alerts are for important messages, not decoration [OK]
Common Mistakes:
Thinking alerts are just decorative boxes
Confusing alerts with navigation or layout elements
Ignoring the purpose of user communication
2. Which Tailwind CSS class combination correctly styles a success alert with green background and white text?
easy
A. bg-green-500 text-white p-4 rounded
B. bg-red-500 text-black p-2 rounded
C. bg-yellow-300 text-gray-700 p-6 rounded
D. bg-blue-600 text-white p-1 rounded
Solution
Step 1: Identify success alert colors
Success alerts typically use green backgrounds with white text for contrast.
Step 2: Match Tailwind classes
bg-green-500 sets green background, text-white sets white text, p-4 adds padding, and rounded rounds corners.
Final Answer:
<code>bg-green-500 text-white p-4 rounded</code> -> Option A
Quick Check:
Green background + white text = success alert [OK]
Hint: Green background with white text means success alert [OK]
Common Mistakes:
Using red or yellow for success alerts
Forgetting padding or rounded corners
Mixing text colors that reduce readability
3. What will be the visual result of this Tailwind alert code?
A. Missing role="alert" attribute for accessibility
B. Wrong color classes for a warning alert
C. Padding class p-3 is invalid in Tailwind
D. The <strong> tag should not be used inside alerts
Solution
Step 1: Check accessibility attributes
Alerts should have role="alert" to notify assistive technologies about important messages.
Step 2: Verify color and padding classes
bg-yellow-200 and text-yellow-800 are correct for warning alerts; p-3 is a valid padding class.
Final Answer:
Missing role="alert" attribute for accessibility -> Option A
Quick Check:
Accessibility needs role="alert" [OK]
Hint: Always add role="alert" for alert accessibility [OK]
Common Mistakes:
Forgetting role attribute for screen readers
Thinking padding classes are invalid
Misunderstanding semantic tags usage
5. You want to create a dismissible info alert using Tailwind CSS that is accessible and responsive. Which combination of features should you include?
hard
A. Add role="dialog" and hide the alert on small screens
B. Use only bg-blue-500 and text-white classes without accessibility roles
C. Use role="alert", aria-live="polite", a close button with keyboard support, and responsive padding classes
D. Use aria-hidden="true" to hide the alert from screen readers
Solution
Step 1: Ensure accessibility with roles and live regions
Use role="alert" and aria-live="polite" so screen readers announce the alert politely.
Step 2: Add a close button with keyboard support
Make the alert dismissible by adding a button accessible via keyboard (e.g., tabindex and aria-label).
Step 3: Use responsive padding classes
Apply padding classes like p-4 sm:p-6 to ensure good spacing on all screen sizes.
Final Answer:
Use role="alert", aria-live="polite", a close button with keyboard support, and responsive padding classes -> Option C
Quick Check:
Accessibility + dismissible + responsive = Use role="alert", aria-live="polite", a close button with keyboard support, and responsive padding classes [OK]
Hint: Combine accessibility roles, keyboard support, and responsive design [OK]
Common Mistakes:
Ignoring accessibility roles
Not providing keyboard support for dismiss button
Hiding alerts from screen readers incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.