Avatar

A versatile avatar component for displaying user profile images and fallback text.

Size Variants

Fallback Text

SM
MD
LG
XL

Custom Styling

XX
YY
ZZ

Overview

The Avatar component is designed for displaying user profile images with automatic fallback support. When an image fails to load or is not provided, it gracefully displays fallback text. The component supports multiple sizes and can be customized with different color schemes.

Avatar

Description

A circular avatar component that displays profile images with fallback text support and multiple size options.

Usage

import { Avatar } from "@crafted-ui/react";

<Avatar src="https://github.com/username.png" fallback="JD" />

Props

The Avatar component accepts the following props:

PropTypeDefaultDescription
size’sm’ | ‘md’ | ‘lg’ | ‘xl''md’Sets the avatar’s size
fallbackstringundefinedText to display when image is not available
srcstringundefinedSource URL for the avatar image
heightstringundefinedCustom height for the image
widthstringundefinedCustom width for the image
imgClassNamestringundefinedAdditional CSS class names for the image
classNamestringundefinedAdditional CSS class names for the container
childrenReactNodeundefinedCustom content inside the avatar

The component also accepts all standard HTML div attributes.

Size Variants

Available Sizes

The Avatar component comes in four predefined sizes to fit different contexts.

SM
MD
LG
XL
<Avatar size="sm" fallback="SM" src="https://github.com/username.png" />
<Avatar size="md" fallback="MD" src="https://github.com/username.png" />
<Avatar size="lg" fallback="LG" src="https://github.com/username.png" />
<Avatar size="xl" fallback="XL" src="https://github.com/username.png" />
SizeDimensionsUse Case
sm40x40pxSmall lists, comments
md48x48pxDefault size, general use
lg56x56pxProfile cards, larger lists
xl64x64pxProfile headers, main displays

Fallback Support

With Images

When a valid image source is provided, the avatar displays the image.

<Avatar src="https://github.com/username.png" fallback="NM" />
<Avatar src="https://github.com/username.png" fallback="JD" size="lg" />

Fallback Text Only

When no image is provided or the image fails to load, the fallback text is displayed.

JD
SM
LG
XL
<Avatar fallback="JD" />
<Avatar fallback="SM" size="sm" />
<Avatar fallback="LG" size="lg" />
<Avatar fallback="XL" size="xl" />

Custom Styling

Color Variants

Use the className prop to apply custom color schemes that match your design system.

A
B
C
D
E
<Avatar fallback="A" />
<Avatar fallback="B" className="bg-destructive text-destructive-foreground" />
<Avatar fallback="C" className="bg-secondary text-secondary-foreground" />
<Avatar fallback="D" className="bg-accent text-accent-foreground" />
<Avatar fallback="E" className="bg-muted text-muted-foreground" />

Custom Image Styling

Use the imgClassName prop to style the image element specifically.

<Avatar 
  src="https://github.com/username.png" 
  fallback="A" 
  imgClassName="grayscale" 
/>
<Avatar 
  src="https://github.com/username.png" 
  fallback="B" 
  imgClassName="sepia" 
/>

Accessibility

The Avatar component follows accessibility best practices:

  • Uses semantic HTML structure with proper image elements
  • Provides fallback content when images are unavailable
  • Maintains proper color contrast ratios in default states
  • Uses alt="" for decorative avatar images (since context is provided by surrounding content)

When implementing avatars, consider:

  1. Context: Ensure the avatar’s purpose is clear from surrounding content
  2. Fallback: Always provide meaningful fallback text (initials, first letter of name)
  3. Contrast: Verify color combinations meet accessibility standards
  4. Size: Choose appropriate sizes for the context and viewing distance

Examples

Basic Avatar with Image

<Avatar 
  src="https://github.com/username.png" 
  fallback="JD" 
/>

Avatar with Custom Size

<Avatar 
  src="https://github.com/username.png" 
  fallback="JD" 
  size="lg"
/>

Avatar Group

<div className="flex -space-x-2">
  <Avatar src="https://github.com/user1.png" fallback="U1" />
  <Avatar src="https://github.com/user2.png" fallback="U2" />
  <Avatar src="https://github.com/user3.png" fallback="U3" />
  <Avatar fallback="+5" className="bg-muted text-muted-foreground" />
</div>

Avatar with Status Indicator

<div className="relative">
  <Avatar src="https://github.com/username.png" fallback="JD" />
  <span className="absolute bottom-0 right-0 h-3 w-3 rounded-full bg-green-500 ring-2 ring-white"></span>
</div>

Custom Content Avatar

<Avatar>
  <svg className="h-6 w-6" fill="currentColor" viewBox="0 0 20 20">
    <path fillRule="evenodd" d="M10 9a3 3 0 100-6 3 3 0 000 6zm-7 9a7 7 0 1114 0H3z" clipRule="evenodd" />
  </svg>
</Avatar>