← Back to Bootstrap Course | Chapter 7: Components — Cards & Alerts | Lesson 5 of 7

Dismissible alerts

A dismissible alert has a close button that removes it from the page.

In this page:

  1. Dismissible alerts
Syntax
markup
<div class="alert alert-warning alert-dismissible fade show" role="alert">
  Message
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>

Dismissible alerts

Add .alert-dismissible, a .btn-close button with data-bs-dismiss="alert" and typically .fade .show for animation. The JavaScript bundle removes the alert when the button is clicked. Give the close button an aria-label.

Note: Use fade and show together for a smooth fade-out.

Example: Dismissible alerts

markup
<!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/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body class="p-3">
<div class="container">
  <div class="alert alert-warning alert-dismissible fade show" role="alert">
    <strong>Heads up!</strong> Click the X to dismiss this alert.
    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
  </div>
</div>
</body>
</html>
Live Example
Related Topics
Common Mistakes
  1. Forgetting alert-dismissible padding
  2. Missing data-bs-dismiss
  3. No aria-label on the close button
Chapter Summary
  • alert-dismissible adds room
  • btn-close with data-bs-dismiss="alert"
  • fade show animates
  • Needs the JS bundle
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.