Alerts and notifications help users know important information quickly. They catch attention and guide users on what to do next.
Alert and notification patterns in Tailwind
<div class="bg-color text-color p-4 rounded-md" role="alert" aria-live="assertive"> <p>Message text here</p> </div>
Use role="alert" and aria-live="assertive" for screen readers to announce the alert immediately.
Change bg-color and text-color classes to match the alert type (success, error, warning, info).
<div class="bg-green-100 text-green-800 p-4 rounded-md" role="alert" aria-live="assertive"> <p>Success! Your form was submitted.</p> </div>
<div class="bg-red-100 text-red-800 p-4 rounded-md" role="alert" aria-live="assertive"> <p>Error! Something went wrong.</p> </div>
<div class="bg-yellow-100 text-yellow-800 p-4 rounded-md" role="alert" aria-live="assertive"> <p>Warning! Please check your input.</p> </div>
<div class="bg-blue-100 text-blue-800 p-4 rounded-md" role="alert" aria-live="assertive"> <p>Info: New updates are available.</p> </div>
This page shows four types of alerts using Tailwind CSS colors and spacing. Each alert uses semantic roles for accessibility. The alerts have different background and text colors to show success, error, warning, and info messages.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Alert and Notification Patterns</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="bg-gray-50 p-6"> <main class="max-w-md mx-auto space-y-6"> <section> <h2 class="text-lg font-semibold mb-2">Success Alert</h2> <div class="bg-green-100 text-green-800 p-4 rounded-md" role="alert" aria-live="assertive"> <p>Success! Your form was submitted.</p> </div> </section> <section> <h2 class="text-lg font-semibold mb-2">Error Alert</h2> <div class="bg-red-100 text-red-800 p-4 rounded-md" role="alert" aria-live="assertive"> <p>Error! Something went wrong.</p> </div> </section> <section> <h2 class="text-lg font-semibold mb-2">Warning Alert</h2> <div class="bg-yellow-100 text-yellow-800 p-4 rounded-md" role="alert" aria-live="assertive"> <p>Warning! Please check your input.</p> </div> </section> <section> <h2 class="text-lg font-semibold mb-2">Info Alert</h2> <div class="bg-blue-100 text-blue-800 p-4 rounded-md" role="alert" aria-live="assertive"> <p>Info: New updates are available.</p> </div> </section> </main> </body> </html>
Always use semantic roles like role="alert" for accessibility.
Use color combinations with good contrast for readability.
Keep alert messages short and clear.
Alerts quickly inform users about important things.
Use different colors to show success, error, warning, or info.
Make alerts accessible with roles and live regions.
Practice
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 BQuick Check:
Alerts = Important user messages [OK]
- Thinking alerts are just decorative boxes
- Confusing alerts with navigation or layout elements
- Ignoring the purpose of user communication
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-500sets green background,text-whitesets white text,p-4adds padding, androundedrounds corners.Final Answer:
<code>bg-green-500 text-white p-4 rounded</code> -> Option AQuick Check:
Green background + white text = success alert [OK]
- Using red or yellow for success alerts
- Forgetting padding or rounded corners
- Mixing text colors that reduce readability
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative" role="alert"> <strong class="font-bold">Error!</strong> <span class="block sm:inline"> Something went wrong.</span> </div>
Solution
Step 1: Analyze the color classes
bg-red-100,border-red-400, andtext-red-700create a red-themed alert indicating an error or danger.Step 2: Read the content inside the alert
The text includes a bold 'Error!' and a message 'Something went wrong.', matching the red error style.Final Answer:
A red alert box with bold 'Error!' and message 'Something went wrong.' -> Option DQuick Check:
Red colors + 'Error!' text = error alert [OK]
- Confusing red alert with success or info
- Ignoring the role attribute for accessibility
- Misreading the text content inside the alert
<div class="bg-yellow-200 text-yellow-800 p-3 rounded"> <strong>Warning!</strong> Please check your input. </div>
Solution
Step 1: Check accessibility attributes
Alerts should haverole="alert"to notify assistive technologies about important messages.Step 2: Verify color and padding classes
bg-yellow-200andtext-yellow-800are correct for warning alerts;p-3is a valid padding class.Final Answer:
Missingrole="alert"attribute for accessibility -> Option AQuick Check:
Accessibility needs role="alert" [OK]
- Forgetting role attribute for screen readers
- Thinking padding classes are invalid
- Misunderstanding semantic tags usage
Solution
Step 1: Ensure accessibility with roles and live regions
Userole="alert"andaria-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.,tabindexandaria-label).Step 3: Use responsive padding classes
Apply padding classes likep-4 sm:p-6to ensure good spacing on all screen sizes.Final Answer:
Userole="alert",aria-live="polite", a close button with keyboard support, and responsive padding classes -> Option CQuick Check:
Accessibility + dismissible + responsive = Userole="alert",aria-live="polite", a close button with keyboard support, and responsive padding classes [OK]
- Ignoring accessibility roles
- Not providing keyboard support for dismiss button
- Hiding alerts from screen readers incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
