Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Avatar

An element used to visually represent a person or entity.

When To Use It

Use Avatar for profile photos, user initials, presence indicators, or other compact identity markers. Avatars work well in lists, headers, comments, chat UIs, and grouped participant displays.

Constructing an Avatar

A Avatar takes content such as an icon, text, or an image. By default, the avatar uses the Circle variant.

Avatar::new(cx, |cx| {
	Svg::new(cx, ICON_USER);
});

Use the variant modifier to change the shape, and badge to attach a status or count indicator:

Avatar::new(cx, |cx| {
	Label::new(cx, "GA");
})
.variant(AvatarVariant::Square);

Avatar::new(cx, |cx| {
	Image::new(cx, "profile.png");
})
.variant(AvatarVariant::Rounded)
.badge(|cx| Badge::empty(cx).class("success"));

Use AvatarGroup to display multiple overlapping avatars together:

AvatarGroup::new(cx, |cx| {
	Avatar::new(cx, |cx| {
		Svg::new(cx, ICON_USER);
	});

	Avatar::new(cx, |cx| {
		Label::new(cx, "GA");
	});

	Avatar::new(cx, |cx| {
		Image::new(cx, "profile.png");
	});
});

Use max_visible to cap how many avatars are shown. Any overflow is replaced with a +N overflow avatar automatically:

AvatarGroup::new(cx, |cx| {
	for _ in 0..6 {
		Avatar::new(cx, |cx| { Svg::new(cx, ICON_USER); });
	}
})
.max_visible(3);

AvatarGroup Modifiers

ModifierTypeDescriptionDefault
variantimpl Res<AvatarVariant>Sets the shape of all avatars in the group.Circle
control_sizeimpl Res<ControlSize>Sets the size of all avatars in the group.Medium
max_visibleimpl Res<usize>Maximum number of visible avatars; overflows show a +N avatar.No limit

Avatar Modifiers

ModifierTypeDescriptionDefault
variantimpl Res<AvatarVariant>Sets the avatar shape to Circle, Square, or Rounded.Circle
control_sizeimpl Res<ControlSize>Sets the avatar size: ExtraSmall (24px), Small (32px), Medium (42px), Large (56px).Medium
badgeFnOnce(&mut Context) -> Handle<Badge>Adds a badge to the avatar, typically for status or count.No badge

Components and Styling

A basic avatar is rendered as a single avatar element containing your provided content. AvatarGroup renders as an avatar-group container around multiple avatars.

SelectorDescription
avatarThe outermost avatar element.
avatar.circleApplied by default for circular avatars.
avatar.squareApplied when using AvatarVariant::Square.
avatar.roundedApplied when using AvatarVariant::Rounded.
avatar.xsmallApplied when using ControlSize::ExtraSmall (24px).
avatar.smallApplied when using ControlSize::Small (32px).
avatar.mediumApplied when using ControlSize::Medium (42px, default).
avatar.largeApplied when using ControlSize::Large (56px).
avatar > svgTargets icon content inside the avatar.
avatar > imageTargets image content inside the avatar.
avatar-groupThe container for grouped avatars.
avatar-group > avatarIndividual avatars inside an avatar group.
avatar-group.circle > avatarApplies circular clipping to group members.
avatar-group.xsmall > avatarSize classes applied to group members via control_size.

Theming

SelectorPropertyValue
avatarbackground-colorvar(--accent)
avatarcolor, fillvar(--foreground)
avatar.circlecorner-radius50%
avatar.squarecorner-radius0px
avatar.roundedcorner-radius4px
avatar.mediumsize42px (default)
avatar > svgsize1s (stretches to fill padding box)
avatar-group > avatarborder2px solid #fff

Customize avatar appearance using shape selectors and your own content styling:

avatar {
	background-color: var(--secondary);
	color: var(--secondary-foreground);
}

avatar.rounded {
	corner-radius: 8px;
}

avatar-group > avatar {
	border: 2px solid var(--background);
}

Accessibility

Avatar does not add interactive behavior or a specialized accessibility role by default. Its accessibility depends on the content it contains and the context in which it is used.

Decorative Avatars

If the avatar is purely decorative and nearby text already identifies the person or entity, no additional accessibility labeling is usually needed.

Meaningful Avatars

If the avatar conveys meaningful information on its own, provide an accessible name with name:

Avatar::new(cx, |cx| {
	Image::new(cx, "profile.png");
})
.name("George Atkinson");

Avatar with Visible Text

If the avatar is paired with visible text, keep the text adjacent so assistive technologies and sighted users receive the same context:

HStack::new(cx, |cx| {
	Avatar::new(cx, |cx| {
		Label::new(cx, "GA");
	});

	Label::new(cx, "George Atkinson");
})
.height(Auto)
.gap(Pixels(8.0));