0
0
Tailwindmarkup~5 mins

Alert and notification patterns in Tailwind

Choose your learning style9 modes available
Introduction

Alerts and notifications help users know important information quickly. They catch attention and guide users on what to do next.

To show success messages after a user completes a task, like submitting a form.
To warn users about potential problems, like unsaved changes.
To show error messages when something goes wrong, like a failed login.
To give general information, like updates or tips.
To confirm actions, like deleting an item.
Syntax
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).

Examples
This is a success alert with green background and text.
Tailwind
<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>
This is an error alert with red background and text.
Tailwind
<div class="bg-red-100 text-red-800 p-4 rounded-md" role="alert" aria-live="assertive">
  <p>Error! Something went wrong.</p>
</div>
This is a warning alert with yellow background and text.
Tailwind
<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>
This is an informational alert with blue background and text.
Tailwind
<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>
Sample Program

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.

Tailwind
<!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>
OutputSuccess
Important Notes

Always use semantic roles like role="alert" for accessibility.

Use color combinations with good contrast for readability.

Keep alert messages short and clear.

Summary

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.