Avatar

The Avatar component displays a user’s profile picture, initials, or fallback icon. It’s designed to be used in user interfaces to represent a person or entity.

Usage

<script>
  import { 
    Avatar,
    AvatarImage,
    AvatarFallback
  } from "$lib/components/ui/avatar";
</script>

<Avatar>
  <AvatarImage src="https://example.com/avatar.jpg" alt="User's Name" />
  <AvatarFallback>JD</AvatarFallback>
</Avatar>

Examples

Basic Avatar

A simple avatar with an image and fallback text.

@shadcn
SC

With Fallback

An avatar with a fallback for when the image fails to load or is not provided.

User
JD
User
PG
AB

Various Sizes

Avatars in different sizes using Tailwind classes.

@shadcn
SC
@shadcn
SC
@shadcn
SC
@shadcn
SC
@shadcn
SC

Custom Fallback Content

Avatars with different types of fallback content.

👤
MG
KS
FS

Within a User Profile

Avatar used in a user profile card.

User
JD

John Doe

Director of Photography

Avatar Group

A group of overlapping avatars to represent a team or group.

User 1
U1
U2
U3
U4
+3

API Reference

Avatar Components

ComponentDescription
AvatarThe root container for the avatar component.
AvatarImageThe image element for the avatar.
AvatarFallbackThe fallback element displayed when the image is not available.

Avatar Properties

PropertyTypeDefaultDescription
classstringundefinedCustom CSS classes for the avatar container.

AvatarImage Properties

PropertyTypeDefaultDescription
srcstringundefinedThe source URL of the avatar image.
altstring""The alt text description of the image for accessibility.
classstringundefinedCustom CSS classes for the avatar image.

AvatarFallback Properties

PropertyTypeDefaultDescription
classstringundefinedCustom CSS classes for the fallback element.

Implementation

The Avatar component is implemented as a set of three components that work together to provide a consistent and accessible user representation. The components use a context to ensure that they’re used in the proper hierarchy:

<!-- Avatar component -->
<div class={cn(
  'relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full border border-border-weak bg-muted',
  className
)}>
  <slot />
</div>

<!-- AvatarImage component -->
<img {src} {alt} class={cn('aspect-square h-full w-full object-cover', className)} />

<!-- AvatarFallback component -->
<div class={cn('flex h-full w-full items-center justify-center rounded-full bg-muted', className)}>
  <slot />
</div>

The Avatar component’s default size is h-10 w-10 (40px × 40px), but this can be customized using Tailwind classes. The AvatarImage component maintains the aspect ratio and fills the container with the image, while the AvatarFallback component centers its content and provides a background.

The components work together to create a seamless experience, with the fallback content displaying only when the image is not available. This provides a consistent representation for users regardless of whether their image can be loaded.