An image of a person or a team with a fallback, usually initials. The image is painted as a background layer on top of the fallback: while it loads, and when the URL is broken, it paints nothing, so the fallback stays visible. No onLoad or onError handlers, no client JavaScript.
Preview
MT
Installation
Usage
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
export default function Example() {
return (
<Avatar>
<AvatarImage src="/avatars/amara.webp" alt="Amara Okafor" />
<AvatarFallback>AO</AvatarFallback>
</Avatar>
)
}Examples
Fallback
The first avatar has a working image, the second a broken URL and the third no image at all. The last two show the fallback.
AO
<Avatar>
<AvatarImage src="/broken-url.webp" alt="Lukas Brenner" />
<AvatarFallback>LB</AvatarFallback>
</Avatar>Size and shape
Size and shape come from className: size-* on Avatar, rounded-* on Avatar and AvatarFallback.
HNHNHN
<Avatar className="size-14 rounded-xl">
<AvatarImage src="/avatars/hana.webp" alt="" />
<AvatarFallback className="rounded-xl">HN</AvatarFallback>
</Avatar>Stacked
Overlap avatars with negative spacing and a ring in the background color. For a ready group with a +N counter see Avatar Group.
<div className="flex -space-x-2 *:data-[slot=avatar]:ring-2 *:data-[slot=avatar]:ring-background">
<Avatar>…</Avatar>
<Avatar>…</Avatar>
<Avatar>…</Avatar>
</div>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| src | string | - | Image URL. AvatarImage renders nothing without it (AvatarImage) |
| alt | string | - | Accessible name, e.g. the person's name. Leave empty for a decorative image (AvatarImage) |
| children | React.ReactNode | - | Fallback content, usually initials or an icon (AvatarFallback) |
| hoverEffect | boolean | true | On Avatar: on hover a ring appears and the image zooms in slightly; the fallback switches to the accent color. Pass false to turn it off |
| className | string | - | Additional CSS classes. Avatar is size-8 and round by default |
Features
- Server component, no client JavaScript.
- The fallback shows while the image loads and stays when it fails to load.
- With alt text the image names the avatar (role="img"), and the fallback is hidden from screen readers so the name is not read twice.
- Unlike an <img>, AvatarImage is a background image: srcSet and loading="lazy" are not supported.