Skip to content

Notifications

Badge

Bit.BlazorUI

Badge is the small marker that sits on another element and reports what is waiting behind it. It takes a number, a string, an icon or a template, caps a count at a max, hides itself when that count reaches zero, or drops its content altogether and becomes a dot. It anchors to any of fifteen positions around its child - physical or direction-aware - and can be nudged, overlapped, ringed against a busy background or given a pulse while something is in progress. It can also step out of the overlay and sit beside its child in the flow, or stand entirely on its own where there is nothing to overlay. It turns into a real keyboard-operable button as soon as it is given a click handler and into a real link as soon as it is given a URL, and carries a text alternative and a live region so that what it says visually also reaches a screen reader.

Usage

Every example is live. Open its code to see exactly what produced the component running underneath.

Basic

The badge overlays whatever it wraps and Content is what it shows. A number is the common case: how much of something is waiting behind the element the badge is attached to. A number is also the only content the badge counts, which is what Max and ShowZero act on. The badge scales itself in as it lands on the page and gives a short bump whenever what it shows changes, so a count ticking over is noticed rather than silently different; both collapse to nothing when the reader asks for reduced motion, which is why this page keeps them off until the animation toggle turns them back on.


63

Variant

The badge offers three variants: Fill (default) paints it in the role color, Outline keeps only the border and the label, and Text drops the border too. A disabled badge keeps its variant and swaps the role color for the disabled one.

84 84 84


84 84 84

Shape

Shape is the corner of the badge. Circular (default) rounds it fully, so a single digit is a circle and a longer label a pill; Rounded takes the corner the current theme gives its controls, which is what makes a badge sit next to the rest of a form; and Square drops the radius altogether. A dot is always a circle, whatever the shape says.


9 9 9


New New New

Dot

Dot shrinks the badge to a plain mark and drops its content and its icon. Use it when the fact that something changed matters more than how much of it there is. The dot scales with the Size of the badge, and since it says nothing on its own, give it a Description so its meaning is not carried by color alone.


Online

Max

Max caps a numeric content: anything above it renders as the max followed by a plus sign, so a counter never grows wider than the room it was given. It reads every numeric type - integral or fractional, not only int - and leaves a string content untouched. A capped badge is the one a reader cannot get the real figure out of, so it carries that figure as its tooltip on its own: hover the second badge below and it reads 100. Title overrides that wherever there is something better to say than the number itself, as the third one does.


60 63+ 99+

ShowZero

A counter that reaches zero has nothing left to report. ShowZero is true by default, so the zero is rendered; set it to false and the badge disappears on its own, which saves keeping a Hidden flag of your own next to the count. An icon or a ContentTemplate is content of its own rather than a number, so it keeps the badge on the page and only the emptied count is taken off it.


0


Content

Beyond a number, the badge takes a string, an icon (IconName or Icon), both together, or - when text is not enough - a ContentTemplate of your own markup. Reversed flips the order of the icon and the content. Any other value of Content is rendered through its own ToString(). A template is content of its own, so it is what the badge shows and what keeps it on the page: neither Max nor ShowZero reads it. A badge given no content, no icon and no template at all has nothing to report and is not rendered, so a counter bound to nothing leaves no empty mark behind.


String content

Text



Icon





Icon and content, and the same pair reversed

Sent Sent




ContentTemplate

99%

Position

Position anchors the badge to one of the fifteen points around its child. The Left and Right ones are physical and stay where they are in right-to-left, while the Start and End ones follow the direction of writing and swap sides with it.

63

Overlap

Overlap pulls the badge in towards its child instead of leaving it on the bounding box. Turn it on for a child with a rounded outline - an avatar, a circular button - where a badge sitting on the corner of the box would float away from it.


63 63

Offset

OffsetX and OffsetY nudge the badge by any CSS length on top of its position, for the cases the fifteen anchors and Overlap do not land exactly on. A positive value moves the badge right and down in both directions of writing, since an offset is a correction for one specific child rather than a part of the layout.


63 63

Bordered

Bordered draws a ring around the badge in the color of the page behind it, so a badge landing on a picture or an avatar keeps an edge of its own instead of dissolving into whatever is underneath it.


Avatar
Online
Avatar
Online
Avatar
8

Pulse

Pulse sends a ring expanding out of the badge and fading, for a state that is still going: a sync in flight, a device connecting, a live stream. The ring is decorative - it slows down rather than stops when the reader asks for reduced motion - so pair it with a Description when the state itself matters.


Connected Syncing 3

Standalone

With no child content there is nothing to overlay, so the badge renders standalone: in the normal flow of the page, at its own size, with Position and Overlap no longer applying. That is how a badge is used inside a table cell, a list row or a heading, where it labels the line it is on rather than an element it sits over. To keep the child content and still lay the badge out beside it, use Inline.


Draft Active Failed Beta Degraded

Inline

Inline keeps the child content and lays the badge out beside it instead of over it, so the badge is given room of its own rather than having to stay small enough not to bury what it sits on. That is the counter at the end of a navigation item, the status next to a heading, the total after a tab label. Overlap stops applying and only the side of Position is read: the Start and Left families put the badge before the child, every other one after it.


Inbox

24

Alerts

3

Build server

Operational

Reports

Beta

Hidden

Hidden removes the badge from the DOM while its child keeps rendering, for a marker you control yourself. When what you are hiding is an empty counter, ShowZero does it without a flag.



63

Events

Giving the badge an OnClick handler turns it into a real button: it takes the pointer back from the element underneath it, answers to hover and to the Enter and Space keys, and shows a focus ring. The click stops there rather than carrying on to whatever the badge is sitting on, so a badge over a button is its own target. Without a handler the badge never takes focus and lets every pointer through - it is a label on the element it belongs to, and that element is what a keyboard user reaches. Disabling the badge takes it out of the tab order and stops the handler. For a badge that opens what it counts rather than acting on the page it is already on, give it an Href instead of a handler.


Link

Href turns the badge into a real anchor, for a counter that opens what it counts rather than acting on the page it is already on. It brings everything a click handler cannot: the context menu, the middle click, the address in the status bar and the announcement of a link. Target picks the browsing context it opens in and Rel the relationship to the linked document - a badge opening in a new tab with no Rel of its own is given rel="noopener", which keeps the opened page from reaching back into this one. OnClick can be set alongside it, in which case the handler runs and the navigation still happens. Disabling the badge drops the href and takes the badge out of the tab order, so a disabled link cannot be followed by either the pointer or the keyboard.


Accessibility

A badge says what it says visually, and a number or a colored dot reaches a screen reader as nothing at all. Description is its text alternative: it is rendered into the badge for assistive technologies only, and the visible content is hidden from them so the two are never announced twice. Live adds a polite live region, so a count that changes while the page stays open is announced as it changes - keep it off for a count that only mirrors what is already on the screen. The region is kept on the page whether or not the badge itself is, which is what makes a counter that appears, changes and then disappears announced every time: a live region inserted together with its own text is announced by nothing. What the region reads out is the Description when there is one and the counter itself otherwise; a ContentTemplate is markup neither of them can be read out of, so a badge showing one keeps its own voice and needs a Description before Live has anything to announce. AriaLabel is the other half: where a Description says what the badge shows, the label names the badge as a control. A badge that is a button or a link is what a screen reader lands on, so the label moves onto that control itself - which is what an icon-only or dotted one, carrying no text to be named by, needs before it becomes either. A Description already names the control, so the two are not set together.


3 unread messages Online


Color

Color picks the role the badge is painted in. The eight semantic roles carry a meaning - Error for something that needs attention, Success for something that went through - while the background, foreground and border roles are the neutral surfaces of the theme, for a badge that should not shout. Every role works with all three variants.


Primary

84 84 84


Secondary

84 84 84


Tertiary

84 84 84


Info

84 84 84


Success

84 84 84


Warning

84 84 84


SevereWarning

84 84 84


Error

84 84 84


Backgrounds, foregrounds & borders:

PrimaryBackground, SecondaryBackground, TertiaryBackground

84 84 84


PrimaryForeground, SecondaryForeground, TertiaryForeground

84 84 84


PrimaryBorder, SecondaryBorder, TertiaryBorder

84 84 84


Disabled:

Primary

84 84 84


Secondary

84 84 84


Tertiary

84 84 84


Info

84 84 84


Success

84 84 84


Warning

84 84 84


SevereWarning

84 84 84


Error

84 84 84

External Icons

Use icons from external libraries like FontAwesome and Bootstrap Icons with the Icon parameter.
See the BitIconInfo section in the parameters table for usage.


4 63


3

Size

Size sets the height, the type size and the inset of the badge, and the diameter of a dot along with them. A counter is never narrower than it is tall, so a single digit stays a circle at every size.


Small

84 84 84


Medium

84 84 84


Large

84 84 84

Style & Class

Style and Class land on the root of the component - the box that wraps the child content - while Styles and Classes reach every part of it on its own: the root, the wrapper the badge is positioned inside of, the badge itself, its icon, its content, its visually hidden description and its live region. The badge also reads a handful of CSS variables off its root, for what no parameter covers: --bit-bdg-clr and --bit-bdg-clr-txt for a color outside the theme roles, --bit-bdg-clr-ring for the color a Bordered ring is drawn in, --bit-bdg-gap for the room between an inline badge and its child, and --bit-bdg-zin for the layer an overlaid badge is painted on, which it sits one above its child on by default so nothing the child raises can cover it.


Component's Style & Class:

84
Anchor
84



CSS variables:

84

Wider gap

84



Styles & Classes:

84 84

RTL

Use BitBadge in right-to-left (RTL). The Start and End positions follow the direction of writing and move to the other side of the child with it, while the Left and Right ones stay where they are. An icon and its content swap sides along with the layout.


63 63

جدید


پیش‌نویس

API

Every parameter, public member, sub-class and enum this component exposes.

BitBadge parameters

Name Type Default value Description
Bordered bool false Draws a ring around the badge in the color of the page behind it, so it stays legible over a busy child such as an avatar or an image.
ChildContent RenderFragment? null Child content of component, the content that the badge will apply to. When it is not set the badge renders standalone, in the normal flow of the page.
Classes BitBadgeClassStyles? null Custom CSS classes for different parts of the BitBadge.
Color BitColor? null The general color of the badge.
Content object? null Content you want inside the badge. A number is capped by Max and hidden by ShowZero when it is zero, a string is rendered as it is, and any other value is rendered through its ToString(). A badge given no content, no icon and no template at all is not rendered.
ContentTemplate RenderFragment? null The custom template to render inside the badge, in place of Content. A template is content of its own, so neither Max nor ShowZero reads it, and it is markup rather than words, so a Live badge showing one needs a Description before its live region has anything to announce.
Description string? null The text alternative of the badge for assistive technologies, for example "5 unread messages". It is rendered into the badge visible only to assistive technologies, and hides the visual content from them so the two are not announced twice.
Dot bool false Reduces the size of the badge and hide any of its content.
Hidden bool false The visibility of the badge. A hidden badge is removed from the DOM while its child content keeps rendering.
Href string? null The URL the badge navigates to, which also turns the badge into a link: an anchor that is focusable, offers the context menu and the middle click, and is announced as a link. While IsEnabled is false the href is dropped and the badge leaves the tab order.
Icon BitIconInfo? null Gets or sets the icon to display using custom CSS classes for external icon libraries. Takes precedence over IconName when both are set.
IconName string? null Gets or sets the name of the icon to display from the built-in Fluent UI icons.
Inline bool false Lays the badge out next to its child content in the normal flow of the page instead of over it. Overlap stops applying and only the side of Position is read: the Start and Left families put the badge before the child content, every other one after it.
Live bool false Announces the badge to assistive technologies whenever its content changes, by turning it into a polite live region. The region is kept on the page whether or not the badge itself is, so a counter that appears, changes and disappears is announced every time. It reads out the Description when there is one and the counter itself otherwise.
Max int? null Max value to display when content is a number. A content above it renders as the max followed by a plus sign, for example 99+, and the badge carries the figure it shortened as its tooltip unless a Title of its own says something better.
OffsetX string? null Moves the badge along the horizontal axis by the given CSS length, on top of its Position. A positive value moves the badge to the right in both directions of writing.
OffsetY string? null Moves the badge along the vertical axis by the given CSS length, on top of its Position. A positive value moves the badge down.
OnClick EventCallback<MouseEventArgs> The click event of the badge, which also turns the badge into a keyboard-operable button.
Overlap bool false Overlaps the badge on top of the child content.
Position BitPosition? null The position of the badge. The Left/Right positions are physical, while the Start/End ones follow the direction of writing.
Pulse bool false Renders an expanding ring around the badge to report that something is in progress.
Rel BitLinkRels? null The relationship between the current document and the one the Href of the badge leads to. With no value of its own, a badge opening in a new browsing context gets rel="noopener" automatically.
Reversed bool false Reverses the direction flow of the content of the badge, which puts the icon after the content.
Shape BitBadgeShape? null The corner shape of the badge.
ShowZero bool true Renders the badge when its content is the number zero. Turn it off for a counter that should disappear once it is emptied. Only a numeric Content counts as zero, and a string is rendered as it is. An icon or a ContentTemplate is content of its own, so it keeps the badge on the page and only the emptied number is taken off it.
Size BitSize? null The size of badge, Possible values: Small | Medium | Large
Styles BitBadgeClassStyles? null Custom CSS styles for different parts of the BitBadge.
Target string? null The browsing context the Href of the badge is opened in, for example _blank.
Title string? null The tooltip to show when the mouse is placed on the badge. It is rendered on the badge itself rather than on the child content underneath it. A badge whose Max has capped its count already spells that count out on hover, so this is only needed when there is something better to say than the figure itself. A title is not a text alternative, so what a screen reader should hear belongs in Description.
Variant BitVariant? null The visual variant of the badge.

BitComponentBase parameters

Name Type Default value Description
AriaLabel string? null Gets or sets the accessible label for the component, used by assistive technologies.
Class string? null Gets or sets the CSS class name(s) to apply to the rendered element.
Dir BitDir? null Gets or sets the text directionality for the component's content.
ForceAnimation bool false Gets or sets a value indicating whether the component's animations play at their full duration even when reduced motion is requested.
HtmlAttributes Dictionary<string, object> new Dictionary<string, object>() Captures additional HTML attributes to be applied to the rendered element, in addition to the component's parameters.
Id string? null Gets or sets the unique identifier for the component's root element.
IsEnabled bool true Gets or sets a value indicating whether the component is enabled and can respond to user interaction.
Style string? null Gets or sets the CSS style string to apply to the rendered element.
TabIndex string? null Gets or sets the tab order index for the component when navigating with the keyboard.
Visibility BitVisibility BitVisibility.Visible Gets or sets the visibility state (visible, hidden, or collapsed) of the component.

BitComponentBase public members

Name Type Default value Description
UniqueId Guid Guid.NewGuid() Gets the readonly unique identifier for the component's root element, assigned when the component instance is constructed.
RootElement ElementReference Gets the reference to the root HTML element associated with this component.

BitBadgeClassStyles properties

Name Type Default value Description
Root string? null Custom CSS classes/styles for the root element of the BitBadge.
BadgeWrapper string? null Custom CSS classes/styles for the badge wrapper of the BitBadge.
Badge string? null Custom CSS classes/styles for the badge of the BitBadge.
Icon string? null Custom CSS classes/styles for the icon of the BitBadge.
Content string? null Custom CSS classes/styles for the content of the BitBadge.
Description string? null Custom CSS classes/styles for the visually hidden description of the BitBadge.
LiveRegion string? null Custom CSS classes/styles for the visually hidden live region of the BitBadge, rendered while Live is on and the badge is not a button.

BitIconInfo properties

Name Type Default value Description
Name string? null Gets or sets the name of the icon.
BaseClass string? null Gets or sets the base CSS class for the icon. For built-in Fluent UI icons, this defaults to "bit-icon". For external icon libraries like FontAwesome, you might set this to "fa" or leave empty.
Prefix string? null Gets or sets the CSS class prefix used before the icon name. For built-in Fluent UI icons, this defaults to "bit-icon--". For external icon libraries, you might set this to "fa-" or leave empty.

BitColor enum

Name Value Description
Primary 0 Info Primary general color.
Secondary 1 Secondary general color.
Tertiary 2 Tertiary general color.
Info 3 Info general color.
Success 4 Success general color.
Warning 5 Warning general color.
SevereWarning 6 SevereWarning general color.
Error 7 Error general color.
PrimaryBackground 8 Primary background color.
SecondaryBackground 9 Secondary background color.
TertiaryBackground 10 Tertiary background color.
PrimaryForeground 11 Primary foreground color.
SecondaryForeground 12 Secondary foreground color.
TertiaryForeground 13 Tertiary foreground color.
PrimaryBorder 14 Primary border color.
SecondaryBorder 15 Secondary border color.
TertiaryBorder 16 Tertiary border color.

BitSize enum

Name Value Description
Small 0 The small size badge.
Medium 1 The medium size badge.
Large 2 The large size badge.

BitBadgeShape enum

Name Value Description
Circular 0 Fully rounded corners, so a counter reads as a circle and a longer label as a pill.
Rounded 1 The corner radius the current theme gives to its controls.
Square 2 Square corners with no radius at all.

BitPosition enum

Name Value Description
TopLeft 0
TopCenter 1
TopRight 2
TopStart 3
TopEnd 4
CenterLeft 5
Center 6
CenterRight 7
CenterStart 8
CenterEnd 9
BottomLeft 10
BottomCenter 11
BottomRight 12
BottomStart 13
BottomEnd 14

BitVariant enum

Name Value Description
Fill 0 Fill styled variant.
Outline 1 Outline styled variant.
Text 2 Text styled variant.

BitVisibility enum

Name Value Description
Visible 0 The content of the component is visible.
Hidden 1 The content of the component is hidden, but the space it takes on the page remains (visibility:hidden).
Collapsed 2 The component is hidden (display:none).

BitDir enum

Name Value Description
Ltr 0 Ltr (left to right) is to be used for languages that are written from the left to the right (like English).
Rtl 1 Rtl (right to left) is to be used for languages that are written from the right to the left (like Arabic).
Auto 2 Auto lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element.

Feedback

Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.