Avatar
An Avatar shows users someone’s image or, when no image loads, their initials.
When to Use
- to show someone next to their name, such as in the Sidebar’s account button, in a Table row, or on a Message
- as an
AvatarGroup, for everyone on one record, withAvatarGroupCountfor those who don’t fit, such as “+3” - for a mark on the Avatar’s corner, like a check, with
AvatarBadge
When Not to Use
- for an image with a fixed ratio, like a cover photo (use an Aspect Ratio)
- for a status with a label (use a Badge)
Anatomy
| Element | Usage |
|---|---|
| Image* | Optional |
| Container | Required |
| Badge | Optional |
| Fallback* | Optional |
| Group | Optional |
| Count | Optional |
* An Avatar has an image, a fallback, or both.
Shapes
To set the corner, pass shape. By default, it is round.
Sizes
| Size | Width and Height | Placement |
|---|---|---|
sm | 24px | A Table row, a menu, or a Field |
default | 32px | The Sidebar and a comment |
lg | 40px | Next to a page title |
In a Table, the Avatar sets the height of its row.
| EMElise Martin |
| JMJonas Meyer |
| SOSofia Ortiz |
sm in a Table row; each row is 40px tall.| EMElise Martin |
| JMJonas Meyer |
| SOSofia Ortiz |
lg is used in a Table row, each row grows to 56px, and users see fewer records in the Table.Behavior
An Avatar is a square with a fixed size, and it doesn’t shrink in a flex row.
Loading
AvatarImage isn’t rendered until its image has loaded. Until then, and when the image fails or has no src, AvatarFallback is shown. If you want to delay the fallback, set delay on it to a number of milliseconds. Users then don’t see the initials flash before an image that loads sooner.
Badge
AvatarBadge contains an icon and no text, and the icon is hidden at sm.
Content
| String | Rule | Example | Counterexample |
|---|---|---|---|
| Fallback | The person’s initials | “CR” | “Camille” |
alt of the image | The person’s name | “Camille Roy” | “Avatar” |
| Count | A plus sign and the number of Avatars not shown | “+3” | “3 more” |
Accessibility
An Avatar isn’t focusable and has no role. Screen readers read the alt of the image or, when no image is shown, the text of the fallback.
An AvatarImage needs alt so that screen readers can announce who the image shows, as WCAG 2.2 SC 1.1.1 Non-text Content
requires. Use the person’s name. If the name is shown next to the Avatar, set alt="" so that it isn’t read twice.
An AvatarBadge has no text, so a status that it marks must also be stated in words for screen reader users, as
WCAG 2.2 SC 1.1.1 Non-text Content requires. Place the words next to the Avatar or in sr-only text inside the badge, and set
aria-hidden on the icon.
Installation
npx shadcn@latest add @summit/avatarUsage
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
<Avatar>
<AvatarImage src="/camille-roy.jpg" alt="Camille Roy" />
<AvatarFallback>CR</AvatarFallback>
</Avatar>;Initials Only
To show only the initials, omit AvatarImage.
<Avatar size="sm">
<AvatarFallback>CR</AvatarFallback>
</Avatar>Square
Set shape="square".
<Avatar shape="square">
<AvatarFallback>CR</AvatarFallback>
</Avatar>With a Badge
Place AvatarBadge last in the Avatar, with one icon inside it.
import { AvatarBadge } from '@/components/ui/avatar';
import { CheckIcon } from '@phosphor-icons/react';
<Avatar>
<AvatarImage src="/camille-roy.jpg" alt="Camille Roy" />
<AvatarFallback>CR</AvatarFallback>
<AvatarBadge>
<CheckIcon aria-hidden />
</AvatarBadge>
</Avatar>;A Group
Use the same size and the same shape for all the Avatars in a group. AvatarGroupCount follows them.
import { AvatarGroup, AvatarGroupCount } from '@/components/ui/avatar';
<AvatarGroup>
<Avatar>
<AvatarImage src="/camille-roy.jpg" alt="Camille Roy" />
<AvatarFallback>CR</AvatarFallback>
</Avatar>
<Avatar>
<AvatarImage src="/jonas-meyer.jpg" alt="Jonas Meyer" />
<AvatarFallback>JM</AvatarFallback>
</Avatar>
<AvatarGroupCount>+3</AvatarGroupCount>
</AvatarGroup>;API Reference
Avatar, AvatarImage, and AvatarFallback accept the props of Avatar.Root, Avatar.Image, and Avatar.Fallback in Base UI. See the Base UI Avatar documentation for delay, keepMounted, and onLoadingStatusChange. The other three parts pass their props to their element.
Avatar
| Prop | Type | Default | Description |
|---|---|---|---|
size | 'default' | 'sm' | 'lg' | 'default' | The width and the height |
shape | 'round' | 'square' | 'round' | The corner of the Avatar |