Skip to content
14:21
Badge

Badge

Introduction ​

Display a badge number or a red dot at the top-right corner of an icon or text, or standalone.

Examples ​

Basic Usage ​

After setting the content prop, the badge is displayed at the top-right corner of the wrapped child; the dot prop renders a small red dot (ignores content). Wrapping an icon composes with the Icon component.

vue
<m-badge content="5">
	<view class="child"></view>
</m-badge>
<m-badge content="Hot">
	<view class="child"></view>
</m-badge>
<m-badge dot>
	<view class="child"></view>
</m-badge>

<!-- Composed with an icon -->
<m-badge content="5">
	<m-icon name="chat-o" size="28" />
</m-badge>

Max ​

After setting the max prop, when the numeric content exceeds the maximum, {max}+ is displayed (only effective when content is a number).

vue
<m-badge content="20" max="9">
	<view class="child"></view>
</m-badge>
<m-badge content="200" max="99">
	<view class="child"></view>
</m-badge>

Custom Color ​

Use the color prop to set the badge background. Semantic color names (primary / success / warning / danger / white, mapped to theme variables — overriding uni.scss variables takes effect immediately) and CSS color values are supported; defaults to danger red.

vue
<m-badge content="5" color="primary">
	<view class="child"></view>
</m-badge>
<m-badge dot color="primary">
	<view class="child"></view>
</m-badge>

Custom Position ​

Use the position prop to set the badge position, defaulting to top-right. The badge center sits on the corner.

vue
<m-badge content="10" position="top-left">
	<view class="child"></view>
</m-badge>
<m-badge content="10" position="bottom-left">
	<view class="child"></view>
</m-badge>
<m-badge content="10" position="bottom-right">
	<view class="child"></view>
</m-badge>

Offset ​

Use the offset-x / offset-y props to set the offset (split into two props for the most solid UTS type constraints): positive values point right / down respectively, negative values reverse, pure numbers are treated as px.

vue
<m-badge content="10" offset-x="4" offset-y="4">
	<view class="child"></view>
</m-badge>
<m-badge content="10" offset-x="-4" offset-y="-4">
	<view class="child"></view>
</m-badge>

Standalone ​

When the badge has no child element, it renders as a standalone element.

vue
<m-badge content="20" />
<m-badge content="200" max="99" />
<m-badge dot />

Custom Badge Content ​

Use the content slot to customize the badge content, such as inserting an icon.

vue
<m-badge>
	<view class="child"></view>
	<template #content>
		<view class="badge-icon-box">
			<m-icon name="success" custom-style="font-size: 10px;" />
		</view>
	</template>
</m-badge>

Zero and show-zero ​

When content is numeric 0, the badge is displayed by default; setting :show-zero="false" hides it.

vue
<m-badge content="0">
	<view class="child"></view>
</m-badge>
<m-badge content="0" :show-zero="false">
	<view class="child"></view>
</m-badge>

Platform Note: Parent Clipping ​

In uni-app x, the view component's overflow defaults to hidden (unlike W3C's visible, see the official CSS default value table). The badge center-on-corner rendering extends about half a badge beyond the component root node; when the direct parent is a shrink-to-fit view (e.g. a flex row item), the overflowing part gets clipped. In that case, explicitly set overflow: visible on the parent container:

vue
<!-- Parent shrink-to-fit the badge size requires disabling clipping -->
<view class="badge-item">
	<m-badge content="5">
		<m-icon name="chat-o" size="28" />
	</m-badge>
</view>
scss
.badge-item {
	overflow: visible;
}

If the parent has padding or spacing buffers (the badge does not exceed its box), no extra handling is needed.

API ​

Props ​

PropDescriptionTypeDefault
contentBadge content (numeric semantics expressed as string; not effective when dot is true)string-
dotWhether to show as a small red dotbooleanfalse
colorBadge background color, semantic color names and CSS color values supportedstring$m-badge-bg (danger red)
maxMaximum value, {max}+ is displayed when numeric content exceeds itstring-
show-zeroWhether to show the badge when content is numeric 0booleantrue
positionBadge position, one of top-right / top-left / bottom-left / bottom-rightstringtop-right
offset-xHorizontal offset, positive points right, pure numbers treated as pxstring-
offset-yVertical offset, positive points down, pure numbers treated as pxstring-
custom-classCustom class name of the root nodestring.ClassString-
custom-styleCustom inline style of the root nodestring-

Slots ​

NameDescription
defaultWrapped child element (badge renders standalone when absent)
contentCustom badge content (takes precedence over the content text)

Theming ​

The badge default styles are defined by the following variables (all !default); define same-name variables in the project root uni.scss to override:

VariableDefaultDescription
$m-badge-height16pxBadge height
$m-badge-color$m-whiteBadge text color
$m-badge-bg$m-color-dangerBadge background color
$m-badge-font-size12pxBadge text font size
$m-badge-font-weightboldBadge text font weight
$m-badge-padding0 3pxBadge padding
$m-badge-min-width16pxBadge min-width (single digit renders as a circle)
$m-badge-dot-size8pxRed dot size
$m-badge-dot-bg$m-color-dangerRed dot background color

See Theming for all variables and how to override them.

Released under the MIT License.