About Lesson
1. Badge Sizing
Badges automatically adjust their size based on the parent element. You can combine them with size classes to customize the appearance.
Example:
HTML
<h1>Large Heading <span class="badge bg-primary">Large Badge</span></h1>
<h3>Small Heading <span class="badge bg-secondary">Small Badge</span></h3>
2. Badge Usage with JavaScript
You can dynamically update badge counts using JavaScript.
Example:
HTML
<button id="counterBtn" type="button" class="btn btn-primary">
Clicks <span id="badgeCount" class="badge bg-secondary">0</span>
</button>
<script>
let count = 0;
document.getElementById("counterBtn").addEventListener("click", function () {
count++;
document.getElementById("badgeCount").innerText = count;
});
</script>
Badge Utilities in Bootstrap 5
- Positioning: Badges can be placed inline or floated using Bootstrap utilities like
.float-end
. - Colors: Use contextual color classes like
.bg-success
,.bg-danger
, etc., for different badge meanings. - Text Styling: Add classes like
.text-dark
or.text-light
for better contrast.
Practical Applications of Badges
- Notifications: Highlight unread messages or new updates.
- Counters: Display counts, such as items in a cart or tasks to complete.
- Status Indicators: Show status (e.g., active, inactive, processing).
- Labels: Tag items with categories or priorities.