Bootstrap 5 Tutorial
v5.3.0Bootstrap 5 Tutorial
Alerts in Bootstrap 5
Basic Alerts
Alerts are available for any length of text, as well as an optional close button.
<!-- Basic Alert Structure --> <div class="alert alert-primary" role="alert"> A simple primary alert </div> <div class="alert alert-success" role="alert"> Operation successful! </div> <div class="alert alert-danger" role="alert"> Error occurred! </div>
Alert with Links
Use the .alert-link utility class to quickly provide matching colored links within any alert.
<!-- Alert with Link --> <div class="alert alert-success" role="alert"> File uploaded successfully! <a href="#" class="alert-link">View file</a> </div> <div class="alert alert-warning" role="alert"> Your subscription expires in 3 days. <a href="#" class="alert-link">Renew now</a> </div>
Additional Content
Alerts can also contain additional HTML elements like headings, paragraphs and dividers.
Well done!
Aww yeah, you successfully read this important alert message. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.
Whenever you need to, be sure to use margin utilities to keep things nice and tidy.
<!-- Alert with Heading and Content --> <div class="alert alert-success" role="alert"> <h4 class="alert-heading">Well done!</h4> <p>Detailed message goes here...</p> <hr> <p class="mb-0">Additional information...</p> </div>
Dismissible Alerts
Add a close button and the .alert-dismissible class to make alerts dismissible.
<!-- Dismissible Alert --> <div class="alert alert-warning alert-dismissible fade show" role="alert"> <strong>Warning!</strong> This is a dismissible alert. <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div>
Alert with Icons
<!-- Alert with Icon -->
<div class="alert alert-success d-flex align-items-center" role="alert">
<svg class="bi flex-shrink-0 me-2" width="24" height="24" role="img">
<!-- SVG icon code -->
</svg>
<div>
Success message here
</div>
</div>Alert Variations
Background Alerts
Outline Alerts
JavaScript Behavior
Programmatic Control
// JavaScript to trigger alerts
const alertPlaceholder = document.getElementById('liveAlertPlaceholder');
const appendAlert = (message, type) => {
const wrapper = document.createElement('div');
wrapper.innerHTML = [
`<div class="alert alert-${type} alert-dismissible" role="alert">`,
` <div>${message}</div>`,
' <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>',
'</div>'
].join('');
alertPlaceholder.append(wrapper);
}
const trigger = document.getElementById('liveAlertBtn');
trigger.addEventListener('click', () => {
appendAlert('Nice, you triggered this alert!', 'success');
});Accessibility
Accessibility Best Practices
- Always include the
role="alert"attribute for screen readers - Use appropriate color contrast for text and background
- Provide clear and concise messages
- For dismissible alerts, ensure the close button has proper aria-label
- Consider using ARIA live regions for dynamic content
Best Practices
Do's
- ✅ Use appropriate alert types for different situations
- ✅ Keep messages clear and concise
- ✅ Use dismissible alerts for non-critical messages
- ✅ Include helpful links when relevant
- ✅ Test color contrast for accessibility
Don'ts
- ❌ Don't overuse alerts
- ❌ Avoid vague or generic messages
- ❌ Don't use alerts for success confirmation of every action
- ❌ Avoid making critical errors dismissible
- ❌ Don't forget accessibility attributes