Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/angular/components/llms.txt

Use this file to discover all available pages before exploring further.

Directive to display a text badge on UI elements to show notifications or status.

Import

import { MatBadge } from '@angular/material/badge';
import { MatBadgeModule } from '@angular/material/badge';

Basic Usage

<button mat-icon-button [matBadge]="3">
  <mat-icon>notifications</mat-icon>
</button>

API Reference

MatBadge

Selector: [matBadge]

Properties

NameTypeDescription
@Input() matBadgestring | number | null | undefinedBadge content to display
@Input() matBadgeColorThemePaletteTheme color. Options: ‘primary’, ‘accent’, ‘warn’
@Input() matBadgeOverlapbooleanWhether badge should overlap its contents. Default: true
@Input() matBadgeDisabledbooleanWhether the badge is disabled
@Input() matBadgePositionMatBadgePositionPosition of the badge. Default: ‘above after’
@Input() matBadgeDescriptionstringMessage for aria-describedby
@Input() matBadgeSizeMatBadgeSizeSize of the badge. Options: ‘small’, ‘medium’, ‘large’. Default: ‘medium’
@Input() matBadgeHiddenbooleanWhether the badge is hidden

Methods

NameDescription
getBadgeElement(): HTMLElement | undefinedGets the badge content element
isAbove(): booleanWhether the badge is above the host
isAfter(): booleanWhether the badge is after the host

Position Options

The badge can be positioned in 8 different locations:
<!-- Above positions -->
<button matBadge="1" matBadgePosition="above after">Above After</button>
<button matBadge="1" matBadgePosition="above before">Above Before</button>

<!-- Below positions -->
<button matBadge="1" matBadgePosition="below after">Below After</button>
<button matBadge="1" matBadgePosition="below before">Below Before</button>

<!-- Simplified positions -->
<button matBadge="1" matBadgePosition="above">Above</button>
<button matBadge="1" matBadgePosition="below">Below</button>
<button matBadge="1" matBadgePosition="before">Before</button>
<button matBadge="1" matBadgePosition="after">After</button>

Examples

With Color

<button mat-icon-button 
        [matBadge]="5" 
        matBadgeColor="warn">
  <mat-icon>shopping_cart</mat-icon>
</button>

Different Sizes

<button mat-icon-button [matBadge]="1" matBadgeSize="small">
  <mat-icon>mail</mat-icon>
</button>

<button mat-icon-button [matBadge]="10" matBadgeSize="medium">
  <mat-icon>mail</mat-icon>
</button>

<button mat-icon-button [matBadge]="100" matBadgeSize="large">
  <mat-icon>mail</mat-icon>
</button>

Dynamic Content

export class BadgeComponent {
  notificationCount = 15;
}
<button mat-icon-button [matBadge]="notificationCount">
  <mat-icon>notifications</mat-icon>
</button>

Hidden Badge

<button mat-icon-button 
        [matBadge]="count" 
        [matBadgeHidden]="count === 0">
  <mat-icon>mail</mat-icon>
</button>

With Description

<button mat-icon-button 
        [matBadge]="3" 
        matBadgeDescription="3 unread messages">
  <mat-icon>mail</mat-icon>
</button>

Accessibility

  • Badge uses aria-describedby for interactive elements
  • For non-interactive elements, description is added inline
  • Badge element itself is aria-hidden="true"
  • Avoid placing badges on mat-icon elements with aria-hidden="true"

Types

type MatBadgePosition = 
  | 'above after'
  | 'above before' 
  | 'below before'
  | 'below after'
  | 'before'
  | 'after'
  | 'above'
  | 'below';

type MatBadgeSize = 'small' | 'medium' | 'large';