Button with Notification Badge Template
Draw attention to new items or updates with this button and notification badge combination.
This is a standard and highly effective way to signal unread messages, new alerts, or the number of items in a shopping cart.
About the Button with Notification Badge
A notification badge is a small overlay on a button that indicates a change in status or a number of new items requiring the user's attention. Its bright color and strategic placement make it stand out. For accessibility, it's important that the button's text or aria-label reflects the notification's content, so the information isn't purely visual.
Features
- High Visibility: The badge uses a high-contrast color and is positioned on the corner of the button to be easily noticed.
- Flexible Content: The badge can display a number or be used as a simple dot for a more minimal look (by removing the number).
- Accessible Implementation: The button text includes the notification count so screen reader users hear the full context, such as "Notifications, 3 new items." The badge itself is hidden from screen readers with
aria-hidden="true"to avoid redundant announcements. - Pure CSS Positioning: The badge is perfectly positioned using CSS relative and absolute positioning.
Code Breakdown
The structure consists of a button element with its position set to relative. Inside the button are the button text and a span element for the badge. The badge's position is set to absolute, which allows it to be positioned relative to the corners of its parent button. By adding aria-hidden="true" to the badge and including its information in the button's primary text, we ensure the component is usable by everyone.
Code
Here is the complete code for the button with a notification badge.