Avatar
React Avatar components are used to display user profile images, initials, or general fallback content. They are commonly used in user profiles, comment lists, team members, and account menus.
This Avatar component is built with React and styled using Tailwind CSS, giving developers control over size, layout, and presentation. It supports various sizes, user status indicators, an optional name/email label group, and overlapping avatar groups.
Installation
Install the component using the Tailgrids CLI.
npx @tailgrids/cli add avatarAnatomy
Import the component parts and combine them.
import {
Avatar,
AvatarImage,
AvatarFallback,
AvatarBadge
} from "@/components/tailgrids/core/avatar";
export const AvatarExample = () => (
<Avatar size="md">
<AvatarImage
src="path/to/image.jpg"
alt="Profile picture of Random Person"
/>
<AvatarFallback>JD</AvatarFallback>
<AvatarBadge status="online" />
</Avatar>
);Examples
Sizes
The Avatar component supports six predefined sizes: xs, sm, md, lg, xl, and xxl.
Status
Show predefined status indicator with online, offline, and busy on <AvatarBadge>. Omit the badge to render an avatar without a status indicator.
Note:
The status indicator uses a white ring to visually separate it from the avatar's background.
Animated Status
Set ping on <AvatarBadge> to animate the status indicator.
Label
Wrap an <Avatar> with a name and subtitle inside a <figure> to associate the visual with descriptive text.
John Doe
Software Engineer
Avatar Group
Multiple avatars can be displayed in a group using the <AvatarGroup> component. Avatars overlap using negative margin and stack via a shared background ring.
Group with Count
Use <AvatarGroupCount> to display an overflow indicator for the group.
API Reference
Avatar
The root container for an avatar. Wraps an image, fallback, and optional badge.
| Prop | Type | Default | Description |
|---|---|---|---|
size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl' | 'md' | Sets the size of the avatar |
className | string | - | Additional CSS classes to apply to the root |
children | React.ReactNode | - | AvatarImage, AvatarFallback, AvatarBadge |
| Data Attribute | Values | Description |
|---|---|---|
data-slot | 'avatar' | Identifies the avatar root element |
data-size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl' | Reflects the current size prop |
AvatarImage
Renders the avatar's image. Falls back to AvatarFallback while loading or on error.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes |
src | string | Image URL (passed to the underlying img) |
alt | string | Image alt text for accessibility |
| Data Attribute | Values | Description |
|---|---|---|
data-slot | 'avatar-image' | Identifies the avatar image |
AvatarFallback
Rendered when the image is loading or has failed. Typically holds initials.
| Prop | Type | Description |
|---|---|---|
className | string | Additional CSS classes |
children | React.ReactNode | Fallback content (e.g. initials) |
| Data Attribute | Values | Description |
|---|---|---|
data-slot | 'avatar-fallback' | Identifies the avatar fallback |
AvatarBadge
Displays a status indicator at the bottom-right corner of the avatar.
| Prop | Type | Default | Description |
|---|---|---|---|
status | 'online' | 'offline' | 'busy' | 'online' | Status color for the badge |
size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl' | 'md' | Sets the badge size; usually matches the parent avatar's size |
live | boolean | false | When true, sets aria-live="polite" so status changes are announced |
ping | boolean | false | When true, renders a ping animation around the badge |
className | string | - | Additional CSS classes |
| Data Attribute | Values | Description |
|---|---|---|
data-slot | 'avatar-badge' | Identifies the avatar badge |
data-status | 'online' | 'offline' | 'busy' | Reflects the current status prop |
AvatarGroup
Wraps a collection of avatars with an overlapping layout.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | - | Additional CSS classes |
children | React.ReactNode | - | <Avatar> and/or <AvatarGroupCount> elements |
| Data Attribute | Values | Description |
|---|---|---|
data-slot | 'avatar-group' | Identifies the avatar group container |
AvatarGroupCount
Displays an overflow indicator (e.g. 5+) inside an <AvatarGroup>.
| Prop | Type | Default | Description |
|---|---|---|---|
size | 'xs' | 'sm' | 'md' | 'md' | Sets the size of the count indicator |
className | string | - | Additional CSS classes |
children | React.ReactNode | - | Overflow content (e.g. 5+) |
aria-label | string | derived | When children is a string, the default label is "${children} more". Pass a string to override. |
| Data Attribute | Values | Description |
|---|---|---|
data-slot | 'avatar-group-count' | Identifies the group count indicator |
data-size | 'xs' | 'sm' | 'md' | Reflects the current size prop |
Accessibility
- Accessible name: Pass
altto<AvatarImage>to give the avatar a label. The label is announced separately from the status badge. - Image alt text: When the avatar stands alone, give
<AvatarImage>a descriptivealt. When it sits next to a text label (e.g. inside a<figure>/<figcaption>), passalt=""so it's treated as decorative. - Status indicator: The badge is announced as a separate
role="img"with anaria-labelof the current status (online/offline/busy). Color is reinforced with a shape glyph (a dash for offline, a minus for busy) so the indicator is not color-only. Passliveto make the badge announce status changes viaaria-live="polite". - Fallback: Initials are visually clear. When the image fails, you can pass
aria-labelto<AvatarFallback>to give the initials a name. - Group semantics:
<AvatarGroup>renders asrole="group". Passaria-label(e.g."Team members") to describe the collection.<AvatarGroupCount>derives anaria-labelof"${children} more"by default and accepts an explicitaria-labeloverride. - Semantic HTML: When pairing an avatar with a name/subtitle, wrap them in a
<figure>with the text in a<figcaption>.
