Dismissible alerts
A dismissible alert has a close button that removes it from the page.
In this page:
Syntax
<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
<!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
- Forgetting alert-dismissible padding
- Missing data-bs-dismiss
- 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: