Lesson 18 of 60 – Bootstrap Alerts
30%

Bootstrap Alerts

Bootstrap alerts are used to display important messages to users. They can be used for success messages, warnings, errors, information, notifications, and other feedback messages.

Note: Bootstrap alerts are created using the .alert class together with a contextual class such as .alert-primary, .alert-success, or .alert-danger.

1. What Are Bootstrap Alerts?

An alert is a message box that provides feedback or important information to the user.

<div class="alert alert-primary">
This is a primary alert.
</div>
This is a primary alert.

Alerts are commonly used after form submissions, login attempts, payments, registrations, and other actions.

2. The .alert Class

The .alert class provides the basic Bootstrap alert styling. It is normally combined with a contextual alert class.

<div class="alert alert-success">
Operation completed successfully.
</div>
Operation completed successfully.

The second class determines the color and meaning of the alert.

3. Primary Alert

The .alert-primary class creates a primary-colored alert.

<div class="alert alert-primary">
This is a primary message.
</div>
This is a primary message.

Primary alerts can be used for general information.

4. Success Alert

The .alert-success class is used for successful operations.

<div class="alert alert-success">
Your registration was successful!
</div>
Your registration was successful!

Success alerts are useful after operations such as saving, registering, updating, or completing a task.

5. Danger Alert

The .alert-danger class is used for errors or dangerous situations.

<div class="alert alert-danger">
Something went wrong!
</div>
Something went wrong!

It can be used for failed operations, invalid data, or important errors.

6. Warning Alert

The .alert-warning class displays a warning message.

<div class="alert alert-warning">
Please check your information.
</div>
Please check your information.

Warning alerts are useful when users need to pay attention before continuing.

7. Info Alert

The .alert-info class displays an informational message.

<div class="alert alert-info">
Your class will start at 9:00 AM.
</div>
Your class will start at 9:00 AM.

8. Light Alert

The .alert-light class creates a light alert.

<div class="alert alert-light">
This is a light alert.
</div>
This is a light alert.

A border can make a light alert easier to distinguish from a light page background.

9. Dark Alert

The .alert-dark class creates a dark alert.

<div class="alert alert-dark">
This is a dark alert.
</div>
This is a dark alert.

10. Different Alert Colors

Bootstrap provides several contextual alert classes.

<div class="alert alert-primary">Primary</div>
<div class="alert alert-secondary">Secondary</div>
<div class="alert alert-success">Success</div>
<div class="alert alert-danger">Danger</div>
<div class="alert alert-warning">Warning</div>
<div class="alert alert-info">Info</div>
<div class="alert alert-light">Light</div>
<div class="alert alert-dark">Dark</div>
Primary Alert
Secondary Alert
Success Alert
Danger Alert
Warning Alert
Info Alert
Light Alert
Dark Alert

11. Alert with a Heading

You can add a heading inside an alert using a heading element and the .alert-heading class.

<div class="alert alert-success">

    <h4 class="alert-heading">
    Well done!
    </h4>

    Your account has been created successfully.

</div>

Well done!

Your account has been created successfully.

12. Alert with Paragraphs

An alert can contain multiple paragraphs of information.

<div class="alert alert-info">

    <h4 class="alert-heading">
    Important Information
    </h4>

    <p>
    Your course will start next Monday.
    </p>

    <p class="mb-0">
    Please arrive ten minutes early.
    </p>

</div>

Important Information

Your course will start next Monday.

Please arrive ten minutes early.

13. Alert with Links

Links can be placed inside alerts. Bootstrap provides the .alert-link class for styling links to match the alert.

<div class="alert alert-primary">

Please read our
<a href="#" class="alert-link">
documentation
</a>.

</div>
Please read our documentation.

14. Success Message Example

Success alerts are commonly displayed after a successful operation.

<div class="alert alert-success">
Student added successfully!
</div>
Student added successfully!

This is especially useful in admin panels and management systems.

15. Error Message Example

Danger alerts can be used when an operation fails.

<div class="alert alert-danger">
Unable to save the student record.
</div>
Unable to save the student record.

16. Warning Message Example

Warning alerts tell users that they should pay attention.

<div class="alert alert-warning">
Your fee payment is due soon.
</div>
Your fee payment is due soon.

17. Information Message Example

Information alerts can provide general instructions or updates.

<div class="alert alert-info">
The website will be updated tonight.
</div>
The website will be updated tonight.

18. Dismissible Alerts

Bootstrap allows users to close an alert by using the .alert-dismissible class together with a close button.

<div class="alert alert-warning alert-dismissible fade show"
role="alert">

    Please update your profile.

    <button type="button"
    class="btn-close"
    data-bs-dismiss="alert"
    aria-label="Close">
    </button>

</div>

19. The .alert-dismissible Class

The .alert-dismissible class adds extra padding to an alert to make space for the close button.

<div class="alert alert-danger alert-dismissible">

    Error occurred!

    <button type="button"
    class="btn-close"
    data-bs-dismiss="alert"
    aria-label="Close">
    </button>

</div>

For the close button to dismiss the alert using Bootstrap's JavaScript behavior, use data-bs-dismiss="alert".

20. Fade and Show Classes

The .fade and .show classes can be used with dismissible alerts to provide a transition effect.

<div class="alert alert-success alert-dismissible fade show"
role="alert">

    Saved successfully!

    <button type="button"
    class="btn-close"
    data-bs-dismiss="alert"
    aria-label="Close">
    </button>

</div>

21. Alert with Icons

You can add an icon or symbol to an alert to make its purpose easier to recognize.

<div class="alert alert-success">
✓ Payment completed successfully!
</div>

<div class="alert alert-danger">
✕ Payment failed.
</div>
✓ Payment completed successfully!
✕ Payment failed.

Icons should support the message rather than replacing meaningful text.

22. Alert for Form Validation

Alerts can display general form validation messages.

<div class="alert alert-danger">
Please fill in all required fields.
</div>
Please fill in all required fields.

For individual fields, Bootstrap also provides form validation styles that can be used alongside appropriate messages.

23. Alert for Login

Login systems commonly use alerts to display authentication feedback.

<div class="alert alert-success">
Login successful!
</div>

<div class="alert alert-danger">
Invalid username or password.
</div>
Login successful!
Invalid username or password.

24. Alert for Payment Status

Payment systems can use different alerts to communicate payment status.

<div class="alert alert-success">
Payment completed successfully.
</div>

<div class="alert alert-warning">
Payment is pending.
</div>

<div class="alert alert-danger">
Payment failed.
</div>
Payment completed successfully.
Payment is pending.
Payment failed.

25. Alerts in an Admin Dashboard

Alerts are very useful in admin dashboards for displaying system messages.

<div class="alert alert-success">
Student added successfully.
</div>

<div class="alert alert-warning">
5 students have pending fees.
</div>

<div class="alert alert-danger">
Database connection failed.
</div>
Student added successfully.
5 students have pending fees.
Database connection failed.

26. Alert Accessibility

Alerts should be designed so that their information is understandable and accessible.

  • Use clear and meaningful messages.
  • Do not rely only on color to communicate meaning.
  • Use sufficient color contrast.
  • Use role="alert" when appropriate for important dynamic messages.
  • Provide useful text for screen-reader users.
  • Do not make important information disappear too quickly.
Tip: A red alert should contain text such as "Payment failed" rather than communicating the failure only through the red color.

27. Common Alert Mistakes

Some common mistakes include:

  • Using alerts for every small piece of information.
  • Writing very long alert messages.
  • Using color without meaningful text.
  • Forgetting the .alert class.
  • Using the wrong alert type for the message.
  • Making important alerts too difficult to notice.

An alert should be short, clear, and relevant to the user's current action.

28. Alert Reference

Common Bootstrap alert classes:

.alert
.alert-primary
.alert-secondary
.alert-success
.alert-danger
.alert-warning
.alert-info
.alert-light
.alert-dark
.alert-heading
.alert-link
.alert-dismissible

These classes can be combined according to the type of message you want to display.

29. Practical Student Management Example

Here is a practical example of alerts in a student management system.

<div class="alert alert-success">
Student added successfully!
</div>

<div class="alert alert-warning">
Student has pending fees.
</div>

<div class="alert alert-danger">
Unable to delete student.
</div>

<div class="alert alert-info">
Student attendance was updated.
</div>
Student added successfully!
Student has pending fees.
Unable to delete student.
Student attendance was updated.

30. Complete Bootstrap Alert Example

The following example combines headings, links, messages, and a dismissible alert.

<div class="container mt-4">

    <div class="alert alert-success">

        <h4 class="alert-heading">
        Success!
        </h4>

        <p>
        Your registration has been completed successfully.
        </p>

        <hr>

        <p class="mb-0">
        You can now
        <a href="#" class="alert-link">
        login to your account
        </a>.
        </p>

    </div>


    <div class="alert alert-warning alert-dismissible fade show"
    role="alert">

        Your profile is incomplete.

        <button type="button"
        class="btn-close"
        data-bs-dismiss="alert"
        aria-label="Close">
        </button>

    </div>

</div>

Success!

Your registration has been completed successfully.


You can now login to your account .

📌 Key Points

  • Use .alert as the basic Bootstrap alert class.
  • Use contextual classes such as .alert-success and .alert-danger.
  • Use success alerts for successful operations.
  • Use danger alerts for errors or failed operations.
  • Use warning alerts when users need attention.
  • Use info alerts for general information.
  • Use .alert-heading for headings inside alerts.
  • Use .alert-link for links inside alerts.
  • Use .alert-dismissible for closable alerts.
  • Use .btn-close with data-bs-dismiss="alert" for the close button.
  • Use .fade and .show for dismissible alert transitions.
  • Do not rely only on color to communicate important information.

🧠 Quick Quiz

Question: Which Bootstrap class is used to create a success alert?