TExcelTAD
Home › Bootstrap › BS5 Alerts

Bootstrap 5 Alerts

An alert is a coloured message box for feedback: "Saved!", "Your password is too short", "New version available". Add .alert plus one colour class .alert-{color}.

Alert colours

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <div class="alert alert-success py-2" role="alert">✅ <strong>Saved!</strong> Your profile was updated.</div>
  <div class="alert alert-danger py-2" role="alert">⛔ <strong>Error:</strong> the file is too large.</div>
  <div class="alert alert-warning py-2" role="alert">⚠️ Your trial ends in 3 days.</div>
  <div class="alert alert-info py-2" role="alert">ℹ️ Maintenance tonight at 22:00.</div>
  <div class="alert alert-primary py-2" role="alert">Primary</div>
  <div class="alert alert-secondary py-2" role="alert">Secondary</div>
  <div class="alert alert-light border py-2" role="alert">Light</div>
  <div class="alert alert-dark py-2" role="alert">Dark</div>
</body>
</html>

role="alert" tells assistive technology to announce the message. Use it for important, time-sensitive messages.

Links and extra content

Inside an alert, .alert-link gives links a bold, matching colour. Alerts can hold any HTML: headings (.alert-heading), paragraphs, horizontal rules.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <div class="alert alert-warning" role="alert">
    <h5 class="alert-heading">Update available</h5>
    <p>Version 2.4 fixes a sync bug. <a href="#" class="alert-link">Read the release notes</a>.</p>
    <hr>
    <p class="mb-0 small">You can update later from Settings → About.</p>
  </div>
</body>
</html>

Alerts with icons

Make the alert a flex container to line up an icon and the message:

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <div class="alert alert-info d-flex align-items-center gap-3" role="alert">
    <span class="fs-3">💡</span>
    <div>Tip: press <kbd>?</kbd> anywhere to see keyboard shortcuts.</div>
  </div>
</body>
</html>

Dismissible alerts

To let users close an alert, add .alert-dismissible, a close button with data-bs-dismiss="alert", and load Bootstrap's JavaScript. Add .fade .show for a fade-out animation.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <div class="alert alert-success alert-dismissible fade show" role="alert" id="welcome">
    <strong>Welcome back!</strong> Click the × to dismiss me.
    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
  </div>
  <p class="small text-body-secondary">Press ▶ Run again to bring it back.</p>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    document.getElementById('welcome').addEventListener('closed.bs.alert', () => {
      console.log('The alert was closed and removed from the page');
    });
  </script>
</body>
</html>

When the alert closes, Bootstrap removes it from the DOM and fires the close.bs.alert (before) and closed.bs.alert (after) events.

Class reference

Class / attribute Purpose
.alert Base alert styles (padding, border, radius)
.alert-{color} Colour: primary, secondary, success, danger, warning, info, light, dark
.alert-heading Heading inside an alert (inherits the alert colour)
.alert-link Bold link matching the alert colour
.alert-dismissible Makes room for the close button
.btn-close The × close button (works anywhere, not only alerts)
data-bs-dismiss="alert" Closes the parent alert (needs JS)
.fade .show Fade animation on close

From JavaScript you can close an alert with bootstrap.Alert.getOrCreateInstance(el).close().

Tip: Don't rely on colour alone. "Error:" or an icon in the text helps users who can't distinguish red from green.

Test yourself

Which attribute makes a button close its parent alert?

  1. data-bs-close="alert"
  2. data-bs-dismiss="alert"
  3. data-dismiss="true"
  4. onclick="close()"

What happens to a dismissed alert?

  1. It is hidden with display:none
  2. It is removed from the DOM
  3. It moves to the bottom
  4. It becomes transparent but stays clickable

Which class styles a link so it matches the alert's colour?

  1. .link-alert
  2. .alert-link
  3. .text-alert
  4. .btn-link