Bootstrap Alerts
Learn how to display success, error, warning, and information messages using Bootstrap 5.
What are Bootstrap Alerts?
Bootstrap alerts are message boxes used to communicate important information to users. They can display success messages, errors, warnings, and general information without navigating away from the page.Examples: Form submitted successfully, invalid login, missing information, and system maintenance notifications.
Learning Objectives
- Understand the purpose of Bootstrap alerts.
- Create alerts using contextual classes.
- Use alert links and headings.
- Create dismissible alerts.
- Display messages after user actions.
- Understand the difference between alerts and validation messages.
1. How Do Bootstrap Alerts Work?
An alert can be displayed when an application needs to communicate a result or provide feedback. The application decides which message to display, and Bootstrap styles it using alert classes.
Working Diagram
User Action
The user submits a form or performs an operation.
Application Response
The application determines the result and message.
Alert Displayed
The user sees a success, error, warning, or information message.
Next Action
The user understands the result and continues with the task. A dismissible alert can also be closed.
2. Creating Basic Alerts
Bootstrap uses the alert class with contextual classes
such as alert-success, alert-danger,
alert-warning, and alert-info.
Program Code
<div class="alert alert-success">
Record saved successfully!
</div>
<div class="alert alert-danger">
Unable to save the record.
</div>
<div class="alert alert-warning">
Please verify the information.
</div>
<div class="alert alert-info">
Your account information is available.
</div>
Program Output
3. Understanding Alert Types
| Class | Purpose | Example Message |
|---|---|---|
alert-success |
Indicates a successful operation. | Registration completed. |
alert-danger |
Indicates an error or serious problem. | Unable to process the request. |
alert-warning |
Highlights something requiring attention. | Please complete all required fields. |
alert-info |
Provides general information. | Your session will expire soon. |
alert-primary |
Displays a primary-themed message. | New course information is available. |
alert-secondary |
Displays a secondary-themed message. | This is an additional notification. |
4. Alert with a Heading
Use a heading inside an alert when you want to separate the message title from its detailed explanation.
Program Code
<div class="alert alert-success" role="alert">
<h4 class="alert-heading">Well Done!</h4>
<p>Your registration was completed successfully.</p>
<hr>
<p class="mb-0">
You can now access your student dashboard.
</p>
</div>
Program Output
Well Done!
Your registration was completed successfully.
You can now access your student dashboard.
5. Alert with a Link
The alert-link class styles a link to match the
alert's contextual appearance.
Program Code
<div class="alert alert-info" role="alert">
Course details are available.
<a href="#course-details" class="alert-link">
View Course Details
</a>
</div>
Program Output
6. Dismissible Alerts
A dismissible alert allows users to close a message using a
close button. Use alert-dismissible and
data-bs-dismiss="alert".
Program Code
<div class="alert alert-warning alert-dismissible fade show"
role="alert">
Please review your information before submitting.
<button type="button"
class="btn-close"
data-bs-dismiss="alert"
aria-label="Close"></button>
</div>
Program Output
7. Practical Example: Form Submission
In this example, clicking the Submit button displays a success message. This demonstrates the user interface only; no data is sent to a server or saved to a database.
Program Code
<button type="button" class="btn btn-primary"
onclick="showSuccess()">
Submit Form
</button>
<div id="successMessage"
class="alert alert-success mt-3 d-none"
role="alert">
Form submitted successfully!
</div>
<script>
function showSuccess() {
document.getElementById('successMessage')
.classList.remove('d-none');
}
</script>
Program Output
8. Practical Example: Error Message
Alerts can also display errors when an operation fails. In a real application, show an error based on the actual server response rather than assuming every operation failed.
Program Code
<button type="button" class="btn btn-danger"
onclick="showError()">
Simulate Error
</button>
<div id="errorMessage"
class="alert alert-danger mt-3 d-none"
role="alert">
Unable to process your request. Please try again.
</div>
<script>
function showError() {
document.getElementById('errorMessage')
.classList.remove('d-none');
}
</script>
Program Output
9. Alert vs. Form Validation
| Bootstrap Alert | Form Validation Message |
|---|---|
| Communicates a general status or result. | Explains a problem with a specific form field. |
| Can appear anywhere on the page. | Usually appears near the relevant input. |
| Example: Profile saved successfully. | Example: Enter a valid email address. |
10. Common Mistakes
- Using
alertwithout a contextual class when a specific message style is needed. - Forgetting
data-bs-dismiss="alert"on a dismiss button. - Displaying a success message before the server confirms success.
- Using an alert for every small field validation error.
- Forgetting that a client-side demo does not save data to a database.
Summary
alertcreates an alert component.- Contextual classes communicate the type of message.
alert-headingadds a heading style.alert-linkstyles a link inside an alert.alert-dismissiblesupports dismissible alerts.- Use alerts to communicate actual application status and feedback.