Bootstrap 5 Alert Example: Displaying Messages Effectively

Ruth Jul 09, 2026

Bootstrap, a popular CSS framework, has evolved significantly over the years, with the latest version, Bootstrap 5, offering a plethora of new features and improvements. One of the key components that Bootstrap 5 excels in is alert messages, which are crucial for providing feedback to users. In this article, we will explore various examples of Bootstrap 5 alerts, their usage, and customization options.

Bootstrap 5 Buttons with Icon and Text Tutorial & Demo — CodeHim
Bootstrap 5 Buttons with Icon and Text Tutorial & Demo — CodeHim

Bootstrap 5 alerts are designed to be flexible, customizable, and responsive. They can be used to display success messages, error notifications, or any other type of alert that requires user attention. By default, Bootstrap 5 provides five alert variants: primary, secondary, success, danger, and warning. However, you can easily create your own variants by customizing the colors and styles.

Bootstrap Notification Bar with Close Button — CodeHim
Bootstrap Notification Bar with Close Button — CodeHim

Understanding Bootstrap 5 Alerts

Before diving into the examples, let's first understand the basic structure of a Bootstrap 5 alert. An alert in Bootstrap 5 is created using the .alert class. The alert variant is specified using the .alert-{color} class, where {color} can be one of the predefined variants or a custom color.

Best Free Bootstrap Login Forms » CSS Author
Best Free Bootstrap Login Forms » CSS Author

The content of the alert is placed inside the .alert element, and you can include any HTML elements, such as text, buttons, or icons, within the alert. Additionally, Bootstrap 5 provides the .alert-dismissible class to add a close button to the alert, allowing users to dismiss it easily.

Basic Alert Example

Bootstrap Navbar Guide and Free Navigation Examples
Bootstrap Navbar Guide and Free Navigation Examples

Here's a simple example of a Bootstrap 5 alert with the primary variant:

<div class="alert alert-primary" role="alert">
  This is a primary alert with default styles.
</div>

In this example, we have a basic alert with the primary variant. The role="alert" attribute is added for accessibility purposes, ensuring that screen readers can identify the alert and provide appropriate feedback to users.

Adding an Icon to an Alert

HTML/CSS Bootstrap Simple Success Confirmation Popup
HTML/CSS Bootstrap Simple Success Confirmation Popup

To enhance the visual appeal of the alerts, Bootstrap 5 allows you to add icons using the .bi class and the appropriate icon class from the Bootstrap Icons library. Here's an example of adding a checkmark icon to the previous alert:

<div class="alert alert-primary" role="alert">
  <i class="bi bi-check-circle-fill"></i> This is a primary alert with a checkmark icon.
</div>

In this example, we've added the bi bi-check-circle-fill class to the <i> element to display a checkmark icon before the alert text.

Customizing Alerts in Bootstrap 5

100+ Best Free Bootstrap Templates 2024
100+ Best Free Bootstrap Templates 2024

Bootstrap 5 provides several options for customizing alerts to match your project's design and requirements. You can customize the colors, backgrounds, borders, and other styles to create unique alerts that stand out from the default variants.

Creating Custom Alert Variants

Ynex - Blazor Server Bootstrap Admin Template
Ynex - Blazor Server Bootstrap Admin Template
Bootstrap Buttons Guide: Examples and Tutorials
Bootstrap Buttons Guide: Examples and Tutorials
WebkitX Bootstrap 5 Admin Dashboard Template with LTR Mini Sidebar
WebkitX Bootstrap 5 Admin Dashboard Template with LTR Mini Sidebar
an image of a computer screen with blue lights and black trees in the back ground
an image of a computer screen with blue lights and black trees in the back ground
Wizard
Wizard
How to Create Bootstrap 5 Navigation Header? – WebNots
How to Create Bootstrap 5 Navigation Header? – WebNots
Bootstrap Forms
Bootstrap Forms
four different colored text boxes with question marks on the top and bottom, along with an information message box below
four different colored text boxes with question marks on the top and bottom, along with an information message box below
Esteem Admin Panel a Flat Bootstrap Responsive Web Template
Esteem Admin Panel a Flat Bootstrap Responsive Web Template
100+ Admin Templates & Dashboards - Free Download
100+ Admin Templates & Dashboards - Free Download
the bootstrap 4 what's new logo on a purple and white background
the bootstrap 4 what's new logo on a purple and white background
Bootstrap 5 vs Bootstrap 4 - What's New & What Changed? - Super Dev Resources
Bootstrap 5 vs Bootstrap 4 - What's New & What Changed? - Super Dev Resources
BOOTSTRAP INPUT FORM (2020) | HOW TO MAKE FORM IN BOOTSTRAP
BOOTSTRAP INPUT FORM (2020) | HOW TO MAKE FORM IN BOOTSTRAP
How not to write an error message | Webflow Blog
How not to write an error message | Webflow Blog
the bootstap login and register forms page
the bootstap login and register forms page
two different types of text and numbers are shown in the table below it is an image of
two different types of text and numbers are shown in the table below it is an image of
the sign in screen for an account that has been blocked by someone's computer
the sign in screen for an account that has been blocked by someone's computer
502 Bad Gateway page design usign html,css
502 Bad Gateway page design usign html,css
Datta Able Bootstrap 5 Admin Template
Datta Able Bootstrap 5 Admin Template
どんな頼みも聞いてくれるチャットボット「Sensay」、ユーザ数が100万人を突破
どんな頼みも聞いてくれるチャットボット「Sensay」、ユーザ数が100万人を突破

To create a custom alert variant, you can use the .alert-{color} class as a starting point and apply additional CSS styles to customize the appearance. For example, let's create a custom alert variant with a light blue background and dark text:

<div class="alert alert-custom" role="alert">
  This is a custom alert with a light blue background and dark text.
</div>

To apply the custom styles, you can add the following CSS to your stylesheet:

.alert-custom {
  background-color: #e3f2fd;
  color: #333;
  border-color: #b3e0ff;
}

In this example, we've created a custom alert variant with a light blue background, dark text, and a light blue border. You can further customize the styles to match your project's design guidelines.

Adding a Close Button to Alerts

To dismiss an alert, you can add a close button using the .alert-dismissible class and the <button> element with the .btn-close class. Here's an example of adding a close button to the previous custom alert:

<div class="alert alert-dismissible alert-custom" role="alert">
  This is a custom alert with a close button.
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

In this example, we've added the .alert-dismissible class to the alert and included a close button using the <button> element with the .btn-close class. The data-bs-dismiss="alert" attribute is used to dismiss the alert when the close button is clicked.

In conclusion, Bootstrap 5 alerts provide a powerful and flexible way to display feedback and notifications to users. With the examples and customization options discussed in this article, you can create engaging and visually appealing alerts that enhance the user experience in your web applications. Start exploring the possibilities of Bootstrap 5 alerts today and take your projects to the next level!