Bootstrap 5 Alerts: Styling and Scenarios for Notification Messages

Bootstrap 5 provides a concise yet powerful Alert component for displaying prompt messages, status feedback, or operation results on a page. Whether it’s a success prompt after form submission, an error warning, or a system notification requiring user attention, the Alert component can present these messages in a unified and aesthetically pleasing manner. Compared to native HTML dialog boxes, Bootstrap’s Alert supports multiple styles, animation effects, interactive features, and is fully responsive, quickly adapting to different device screens.

Quick Start: Creating Your First Alert

To use Bootstrap 5 Alerts, first include the Bootstrap 5 CSS and JS files in your project. The simplest way is to use a CDN:

<!-- Include Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

<!-- Include Bootstrap 5 JS (includes Popper for all interactive features) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

To create a basic Alert, use the alert class along with a color theme class:

<div class="alert alert-primary" role="alert">
  This is a basic alert message using the alert-primary style.
</div>

You’ll see a blue rectangular box with the text, which is the most basic Alert.

Core Styles: Rich Color Themes

Bootstrap 5 predefines multiple color themes for Alerts, allowing quick switching via class names. Each color corresponds to a specific scenario:

Class Name Color Use Case
alert-primary Blue General information prompt
alert-success Green Successful operation (e.g., form submission)
alert-danger Red Error message (e.g., connection failure)
alert-warning Yellow Warning message (e.g., password expiration)
alert-info Light blue Supplementary note (e.g., system notification)
alert-secondary Gray Secondary information

Example: Alerts with different colors

<!-- Success Alert -->
<div class="alert alert-success alert-dismissible fade show" role="alert">
  <strong>Success!</strong> Your order has been submitted successfully.
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

<!-- Error Alert -->
<div class="alert alert-danger alert-dismissible fade show" role="alert">
  <strong>Error!</strong> Network connection failed. Please check and try again.
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

<!-- Warning Alert -->
<div class="alert alert-warning alert-dismissible fade show" role="alert">
  <strong>Warning!</strong> Your storage space is nearly full. Please clean up in time.
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
  • alert-dismissible: Adds a close button (× icon) that automatically hides the alert when clicked.
  • fade show: Adds fade-in/out animation effects (smooth disappearance when closed).

When using links inside an alert, the alert-link class ensures the link color matches the alert’s theme, preventing visual jarring:

<div class="alert alert-info alert-dismissible fade show" role="alert">
  Click <a href="#" class="alert-link">here</a> to view the detailed help documentation.
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

2. Auto-Hide: Controlled by JavaScript

To make an alert disappear automatically (e.g., after 5 seconds), use JavaScript:

<div id="autoHideAlert" class="alert alert-success alert-dismissible fade show" role="alert">
  This alert will hide automatically in 5 seconds...
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

<script>
  setTimeout(() => {
    const alert = new bootstrap.Alert(document.getElementById('autoHideAlert'));
    alert.close(); // Call close() to hide the alert
  }, 5000); // Execute after 5000 milliseconds
</script>

Typical Scenario Applications

Scenario 1: Form Submission Success Prompt

Display a success message at the top of the page after a user submits a form:

<div class="alert alert-success alert-dismissible fade show" role="alert" style="margin-bottom: 0;">
  <i class="bi bi-check-circle"></i> <strong>Submission Successful!</strong> Your feedback has been received, and we will process it shortly.
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

Scenario 2: Delete Operation Confirmation Prompt

Show a confirmation alert when a user clicks “Delete” to prevent accidental actions:

<button class="btn btn-danger" onclick="showDeleteAlert()">Delete</button>

<div id="deleteAlert" class="alert alert-danger alert-dismissible fade show" role="alert" style="display: none;">
  <strong>Confirm Deletion?</strong> This action cannot be undone. Are you sure you want to delete it?
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
  <button class="btn btn-sm btn-success" onclick="confirmDelete()">Confirm</button>
</div>

<script>
  function showDeleteAlert() {
    const alert = document.getElementById('deleteAlert');
    alert.style.display = 'block';
    new bootstrap.Alert(alert).open(); // Show the alert
  }
  function confirmDelete() {
    // Execute deletion logic (e.g., call an API)
    console.log('Performing deletion...');
  }
</script>

Scenario 3: System Notification Floating Prompt

Display system notifications in the top-right corner with fixed positioning to avoid blocking content:

<div class="alert alert-info alert-dismissible fade show" role="alert" style="position: fixed; top: 20px; right: 20px; z-index: 999;">
  <i class="bi bi-bell"></i> You have an unread message!
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

Customizing Alert Styles

To adjust the alert’s appearance (e.g., border, background color), override default styles with custom CSS:

/* Custom green alert */
.custom-success-alert {
  background-color: #f0fff4 !important;
  border-left: 4px solid #4CAF50;
  color: #155724;
}

Apply the custom class when using the alert:

<div class="alert alert-success custom-success-alert alert-dismissible fade show" role="alert">
  Custom success message
</div>

Summary

Bootstrap 5 Alerts enable quick implementation of prompt messages and interactions through simple class combinations. Key takeaways:

  • Use alert-* color classes to define themes (e.g., alert-success).
  • Add a close button with alert-dismissible and fade animations with fade show.
  • Use alert-link to unify link styles and avoid visual conflicts.
  • Combine with JavaScript for auto-hide or complex interactions (e.g., delete confirmation).

By applying these methods, you can flexibly use Alerts in projects to enhance user experience and clarify page prompts.

Xiaoye