October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Bootstrap Alerts: Free HTML Examples for Success, Warnings, and Errors

Learn how to create Bootstrap 5 alerts, choose a contextual variant, add a dismiss button, manage keyboard focus, and fix version-specific markup issues.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Bootstrap alerts display contextual feedback—such as success, warning, or error messages—using HTML classes and CSS. In Bootstrap 5, add .alert and one of eight contextual classes. To let users dismiss an alert, add a close button with data-bs-dismiss="alert" and load Bootstrap’s JavaScript. Use markup that matches your project’s Bootstrap version.

Choose the right Bootstrap alert variant

Bootstrap 5 provides eight contextual classes: .alert-primary, .alert-secondary, .alert-success, .alert-danger, .alert-warning, .alert-info, .alert-light, and .alert-dark. Choose the class that best reinforces the message, but state the meaning in words as well. Bootstrap cautions that color alone does not communicate meaning to screen-reader users (Bootstrap 5.3 Alerts documentation).

As an Amazon Associate I earn from qualifying purchases.

Create a basic alert

Use the alert class with a contextual variant and an appropriate role. This example presents a success message:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<div class="alert alert-success" role="alert">
  Your changes have been saved.
</div>

Replace the message and variant to match the state you need to communicate. An alert that only displays information does not need a dismiss button.

#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Add a dismiss button in Bootstrap 5

For an alert users can close, add .alert-dismissible and a real button with data-bs-dismiss="alert". The class reserves space for the button. Give it an accessible name, such as aria-label="Close":

<div class="alert alert-warning alert-dismissible fade show" role="alert">
  Your session is about to expire.
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

The Bootstrap alert JavaScript plugin must be available for the dismissal control to work. If you are using Bootstrap’s data API, the data-bs-dismiss attribute initializes the behavior; you do not need to manually create an alert instance just to dismiss it. The .fade and .show classes enable a fade transition before removal (Bootstrap 5.3 Alerts documentation).

Manage focus after dismissal

Dismissing an alert removes it from the DOM. For keyboard users, that removal can leave focus lost or reset to the start of the page. Listen for closed.bs.alert and move focus to the next relevant control or location. If that destination is not normally keyboard-focusable, Bootstrap’s v5.0 documentation advises adding tabindex="-1" so code can move focus there (Bootstrap 5.0 Alerts documentation).

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Bootstrap fires close.bs.alert when closing begins and closed.bs.alert after closing and any CSS transition. Use the latter when focus should move after the alert has finished closing (Bootstrap 5.3 Alerts documentation).

Rank #3
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Match the dismissal markup to your Bootstrap version

Dismissal attributes and JavaScript setup differ across major versions. Check which version your project uses before copying an example.

Version Dismissal detail Documentation
Bootstrap 5 Use data-bs-dismiss="alert"; Bootstrap’s data API handles initialization when the alert JavaScript is loaded. Bootstrap 5.3 Alerts
Bootstrap 4 The documented attribute is data-dismiss="alert", used with the jQuery plugin. Bootstrap 4.6 Alerts
Bootstrap 3 The alert JavaScript plugin is required for dismissal. Bootstrap 3.4 Alerts

Troubleshoot a close button that does not work

  • Check the attribute for your version. Bootstrap 5 uses data-bs-dismiss; Bootstrap 4 uses data-dismiss. A version mismatch can prevent the expected behavior.
  • Confirm the alert JavaScript is loaded. The dismiss attribute alone does not provide the behavior if the required plugin is unavailable.
  • Use a button element. The documented pattern uses a button with type="button" and an accessible label.
  • Do not promise dismissal when JavaScript is disabled. Bootstrap documents no special fallback for its plugins in that case. A static alert can still convey its message, but a close control should not imply that it will work (Bootstrap JavaScript documentation).
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When to make an alert dismissible

Use a dismiss control when removing the message is useful to the person using the page. Keep important status or error information visible if closing it could hide information needed to complete a task. Whether the alert is static or dismissible, make its meaning clear in text and ensure keyboard focus has a predictable destination when the alert is removed.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.