How to use Bootstrap badges for notifications
Bootstrap badges are used to display notifications, counts, or status indicators; 1. Use .badge with .bg-* and .text-white classes, adding .rounded-pill for pill-shaped styles; 2. Position badges using .position-relative on the parent and .position-absolute with utilities like .top-0 .start-100 .translate-middle for corner alignment; 3. Combine with buttons or icons by nesting the badge inside, adjusting color and position for visual clarity; 4. Update dynamically with JavaScript to show or hide based on count, improving usability; always use semantic colors, keep text short, ensure accessibility with visually-hidden text, and avoid overuse to maintain clean UI, resulting in effective and responsive notification indicators.

Bootstrap badges are lightweight and flexible components perfect for adding notifications, counts, or status indicators to your web interface. They’re commonly used alongside navigation items, buttons, or list elements to show things like unread messages, cart items, or update alerts.

Here’s how to effectively use Bootstrap badges for notifications:
1. Basic Badge Setup
To create a simple notification badge, use the .badge class with a utility background color (like .bg-primary, .bg-danger, etc.). Always include .text-white for readable text on colored backgrounds.

<span class="badge bg-danger text-white">3</span>
This creates a red circular badge showing "3"—ideal for unread notifications.
Note: In Bootstrap 5 , badges no longer have default styling like pills or padding unless you include additional classes. Use
.rounded-pillfor a pill-shaped badge.
<span class="badge bg-danger text-white rounded-pill">3</span>
This gives a softer, pill-style look commonly used in modern UIs.
2. Positioning Badges on Elements
You’ll often want badges to appear in the corner of buttons, avatars, or nav links. Use positioning utilities to place them precisely.
Example: Badge on a Navigation Link
<a href="#" class="position-relative">
Messages
<span class="position-absolute top-0 start-100 translate-middle badge bg-danger text-white rounded-pill">
9
<span class="visually-hidden">unread messages</span>
</span>
</a>position-relativeon the parent allows absolute positioning of the badge.top-0 start-100places the badge at the top-right.translate-middlecenters it perfectly by shifting it -50% in both directions.<span class="visually-hidden">improves accessibility.
This pattern is widely used in dashboards and mobile-style menus.
3. Using Badges with Buttons or Icons
Badges work well with icons (e.g., from Bootstrap Icons or Font Awesome) to show notification counts.
<button type="button" class="btn btn-outline-secondary position-relative">
<i class="bi bi-bell"></i> Alerts
<span class="position-absolute top-0 start-100 translate-middle badge bg-warning text-dark rounded-pill">
5
</span>
</button>Tips:
- Use
bg-warning text-darkfor yellow badges since white text doesn’t contrast well. - Adjust
top,start, or useend/bottomclasses depending on where you want the badge (e.g., top-right vs. bottom-left).
4. Dynamic Badge Control with JavaScript (Optional)
You can update badge content dynamically. For example, hiding the badge when there are no notifications:
function updateNotificationCount(count) {
const badge = document.getElementById('notification-badge');
if (count > 0) {
badge.textContent = count > 9 ? '9 ' : count;
badge.style.display = 'inline'; // Show badge
} else {
badge.style.display = 'none'; // Hide badge
}
}In HTML:
<a href="#" class="position-relative">
Notifications
<span id="notification-badge" class="position-absolute top-0 start-100 translate-middle badge bg-danger text-white rounded-pill" style="display: none;">
0
</span>
</a>This keeps the interface clean when no alerts are present.
Key Tips for Effective Notification Badges
-
Use semantic colors:
-
.bg-dangerfor urgent alerts -
.bg-warningfor warnings -
.bg-secondaryfor less important counts
-
Limit text length: Keep badge text short—numbers or short labels like "New", "On", or "!".
Ensure accessibility: Always include
visually-hiddentext for screen readers when the badge conveys meaning.Avoid stacking too many: Too many badges clutter the UI. Prioritize only the most relevant notifications.
Basically, Bootstrap badges are easy to set up and highly customizable for notifications. With a few utility classes and smart positioning, you can create clean, responsive indicators that enhance user experience without overwhelming the design.
The above is the detailed content of How to use Bootstrap badges for notifications. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20522
7
13634
4
How to ensure your Bootstrap site is fully accessible (WCAG)? (Accessibility)
Mar 08, 2026 am 01:25 AM
The Bootstrap component does not automatically add the aria attributes required for complete WCAG. You need to manually complete aria-expanded, role="dialog", aria-labelledby, etc.; the form must be equipped with an explicit label; :focus needs to use :focus-visible to ensure that the keyboard focus is visible; the color contrast must reach 4.5:1.
How to animate Bootstrap components for a more dynamic feel? (Advanced CSS)
Mar 13, 2026 am 01:13 AM
The fade and show classes of Bootstrap5 are only status markers and do not contain animation logic. You need to manually add @keyframes or transitions to achieve fade-in and fade-out; custom components need to configure additional animation rules, and when JS controls the display, the show class must be added with delay to ensure redrawing.
How to integrate Bootstrap with a JavaScript framework like React or Vue? (Integration)
Mar 12, 2026 am 01:22 AM
Only BootstrapCSS should be introduced in React/Vue instead of JS to avoid global style pollution and jQuery dependency; SCSS modules should be imported on demand to reduce the size; interactive functions must be imported with encapsulation libraries (such as react-bootstrap) or ESM; grid classes need to adapt to JSX/Vue syntax, and explicit and implicit classes should be used with SSR with caution.
How to create a professional-looking landing page with Bootstrap? (Project-Based)
Mar 10, 2026 am 12:22 AM
Bootstrap’sdefaultstylingfeelsgenericduetouncustomizedspacing,typography,andcolor—fixableviaCSSvariablesorSass,notdirectedits;avoidfixedwidths,misuseofgridclasses,andrender-blockingassetstoensureresponsivenessandfastLCP.
How to use Bootstrap utility classes to speed up your workflow? (Efficiency)
Mar 11, 2026 am 12:18 AM
Use mt-3 to represent the top margin, mb-3 to represent the bottom margin, and the suffixes t/b/s/e correspond to top/bottom/start/end respectively; ms-auto implements right alignment in the flex container, me-2 replaces the abandoned mr-2, and responsive classes such as mt-md-4 need to have breakpoint prefixes.
How to properly install and set up Bootstrap 5 in your web project? (Getting Started)
Mar 16, 2026 am 12:40 AM
To correctly introduce Bootstrap5, you need to load bootstrap.min.css first, then load bootstrap.bundle.min.js (including Popper), and ensure that the viewport tag exists; after npm installation, you need to manually introduce compiled files in the dist directory. SCSS customization must be preceded by variable declarations, and JS components must be explicitly initialized to avoid jQuery contamination.
How to implement offcanvas menus for mobile navigation in Bootstrap? (Mobile UX)
Mar 15, 2026 am 12:04 AM
Bootstrap5's offcanvas needs to be initialized through the data-bs-toggle="offcanvas" data-bs-target attribute or newbootstrap.Offcanvas(); bootstrap.bundle.min.js must be used, and the button must be added with type="button" to prevent submission. iOS requires overscroll-behavior and the correct DOM position. Manual focus management is required for accessibility.
How to create a responsive sidebar navigation with Bootstrap? (UI Patterns)
Mar 16, 2026 am 12:37 AM
The only sidebar solution natively supported by Bootstrap 5 is offcanvas. It defaults to a drawer for small screens and can be resident for large screens. It has backdrop, keyboard close, animation and accessibility support. It needs to use dual structure or JS to dynamically control the responsive display, and customize the width, height and background through CSS variables.






