The Avatar component displays user profile pictures with intelligent fallback support and multiple variants. It's designed to represent users in interfaces, automatically showing their profile image when available and gracefully falling back to initials, icons, or custom content when the image fails to load or is unavailable.
import { Avatar } from '@dashflowx/core'export default function AvatarExample() {return (<AvatarimageSrc="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=32&h=32&fit=crop&crop=face"imageAlt="John Doe"fallback={<span>JD</span>}variant="basic"size="md"/>)}
Preview:
Standard avatar with rounded corners and clean design.
<Avatarvariant="basic"imageSrc="https://images.unsplash.com/photo-1494790108755-2616b612b786?w=32&h=32&fit=crop&crop=face"imageAlt="Jane Smith"fallback={<span>JS</span>}size="md"/>
Preview:
Fully rounded avatar with circular design.
<Avatarvariant="rounded"imageSrc="https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=32&h=32&fit=crop&crop=face"imageAlt="John Doe"fallback={<span>JD</span>}size="md"/>
Preview:
Square avatar with sharp corners.
<Avatarvariant="square"imageSrc="https://images.unsplash.com/photo-1438761681033-6461ffad8d80?w=32&h=32&fit=crop&crop=face"imageAlt="Sarah Wilson"fallback={<span>SW</span>}size="md"/>
Preview:
Avatar with a ring border around it.
<Avatarvariant="ring"imageSrc="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=32&h=32&fit=crop&crop=face"imageAlt="Mike Johnson"fallback={<span>MJ</span>}size="md"/>
Preview:
Compact avatar for tight spaces.
<Avatarvariant="basic"imageSrc="https://images.unsplash.com/photo-1494790108755-2616b612b786?w=24&h=24&fit=crop&crop=face"imageAlt="Small Avatar"fallback={<span>SA</span>}size="sm"/>
Preview:
Standard size for most use cases.
<Avatarvariant="basic"imageSrc="https://images.unsplash.com/photo-1494790108755-2616b612b786?w=32&h=32&fit=crop&crop=face"imageAlt="Medium Avatar"fallback={<span>MA</span>}size="md"/>
Preview:
Larger avatar for prominent display.
<Avatarvariant="basic"imageSrc="https://images.unsplash.com/photo-1494790108755-2616b612b786?w=48&h=48&fit=crop&crop=face"imageAlt="Large Avatar"fallback={<span>LA</span>}size="lg"/>
Preview:
Extra large avatar for hero sections.
<Avatarvariant="basic"imageSrc="https://images.unsplash.com/photo-1494790108755-2616b612b786?w=64&h=64&fit=crop&crop=face"imageAlt="Extra Large Avatar"fallback={<span>XL</span>}size="xl"/>
Preview:
| Prop | Type | Default | Description |
|---|---|---|---|
imageSrc | string | - | The URL of the avatar image |
imageAlt | string | - | Alt text for the avatar image (required for accessibility) |
fallback | JSX.Element | 'UD' | Fallback content to display when image fails to load |
variant | 'basic' | 'rounded' | 'square' | 'ring' | 'basic' | The visual style variant of the avatar |
size | 'sm' | 'md' | 'lg' | 'xl' | 'md' | The size of the avatar |
imageClassName | string | - | Custom CSS classes for the image element |
fallbackClassName | string | - | Custom CSS classes for the fallback element |
<Avatarvariant="ring"imageSrc="https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=64&h=64&fit=crop&crop=face"imageAlt="Professional headshot of John Doe"fallback={<div className="flex items-center justify-center w-full h-full bg-gradient-to-br from-gray-600 to-gray-700 text-white font-semibold">JD</div>}size="lg"/>
Preview:
<Avatarvariant="square"imageSrc="https://images.unsplash.com/photo-1438761681033-6461ffad8d80?w=32&h=32&fit=crop&crop=face"imageAlt="Sarah Wilson"fallback={<div className="flex items-center justify-center w-full h-full bg-gradient-to-br from-green-500 to-green-600 text-white font-medium text-xs">SW</div>}size="md"/>
Preview:
<Avatarvariant="rounded"imageSrc=""imageAlt="Guest User"fallback={<div className="flex items-center justify-center w-full h-full bg-gradient-to-br from-purple-500 to-purple-600 text-white font-medium">GU</div>}size="sm"/>
Preview:
<Avatarvariant="ring"imageSrc="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=48&h=48&fit=crop&crop=face"imageAlt="Custom Styled Avatar"fallback="CS"size="lg"imageClassName="shadow-lg border-2 border-blue-300"fallbackClassName="bg-gradient-to-br from-blue-500 to-blue-600 text-white font-bold text-lg"/>
Preview:
<Avatarvariant="square"imageSrc="https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=32&h=32&fit=crop&crop=face"imageAlt="Advanced Styled Avatar"fallback="AS"size="md"imageClassName="rounded-lg shadow-md hover:shadow-xl transition-shadow duration-300"fallbackClassName="bg-gradient-to-br from-purple-500 to-purple-700 text-white font-semibold text-sm rounded-lg"/>
Preview:
<div className="flex items-center space-x-3 p-4 bg-white rounded-lg shadow-sm"><Avatarvariant="ring"imageSrc={user.profileImage}imageAlt={`${user.name}'s profile picture`}fallback={<div className="flex items-center justify-center w-full h-full bg-gradient-to-br from-primary-light to-primary text-white font-semibold">{user.initials}</div>}size="lg"/><div className="flex-1"><p className="text-sm font-medium text-gray-900">{user.name}</p><p className="text-xs text-gray-500">{user.email}</p><p className="text-xs text-gray-400">{user.role}</p></div></div>
<div className="grid grid-cols-2 gap-4">{teamMembers.map((member, index) => {const variants = ['basic', 'rounded', 'square', 'ring'];const variant = variants[index % variants.length] as 'basic' | 'rounded' | 'square' | 'ring';return (<div key={member.id} className="flex items-center space-x-3 p-3 bg-gray-50 rounded-lg"><Avatarvariant={variant}imageSrc={member.avatar}imageAlt={`${member.name}'s avatar`}fallback={<div className="flex items-center justify-center w-full h-full bg-gradient-to-br from-blue-500 to-blue-600 text-white font-medium text-sm">{member.initials}</div>}size="md"/><div><p className="text-sm font-medium">{member.name}</p><p className="text-xs text-gray-500">{member.position}</p><p className="text-xs text-gray-400 capitalize">{variant}</p></div></div>);})}</div>
<header className="flex items-center justify-between p-4 bg-white border-b border-gray-200"><div className="flex items-center space-x-4"><h1 className="text-xl font-semibold text-gray-900">Dashboard</h1><nav className="flex space-x-6"><a href="#" className="text-gray-600 hover:text-gray-900">Projects</a><a href="#" className="text-gray-600 hover:text-gray-900">Tasks</a><a href="#" className="text-gray-600 hover:text-gray-900">Reports</a></nav></div><div className="flex items-center space-x-3"><span className="text-sm text-gray-600">Welcome back, {currentUser.name}</span><Avatarvariant="basic"imageSrc={currentUser.avatar}imageAlt={`${currentUser.name}'s avatar`}fallback={<div className="flex items-center justify-center w-full h-full bg-gradient-to-br from-primary-light to-primary text-white font-semibold">{currentUser.initials}</div>}size="lg"/></div></header>
The Avatar component includes comprehensive accessibility features:
- Alt text: Required
imageAltattribute for screen readers - Fallback content: Meaningful fallback when images fail to load
- Semantic structure: Uses appropriate HTML elements
- Focus management: Proper focus handling when interactive
- ARIA support: Compatible with assistive technologies
- Size variants: Appropriate sizing for different contexts
Use specific, descriptive alt text that identifies the person.
// ❌ Bad - Generic alt text<Avatar imageSrc={user.photo} imageAlt="Photo" />// ✅ Good - Descriptive alt text<Avatar imageSrc={user.photo} imageAlt="John Doe's professional headshot" />
Use consistent variants across your application for similar contexts.
// ✅ Good - Consistent variant for user profiles<Avatar variant="ring" fallback={<span className="font-semibold text-white">JD</span>} /><Avatar variant="ring" fallback={<span className="font-semibold text-white">JS</span>} /><Avatar variant="ring" fallback={<span className="font-semibold text-white">AB</span>} />
Choose sizes that fit the context and importance.
// ✅ Good - Size hierarchy<Avatar size="sm" /> {/* For lists and compact views */}<Avatar size="md" /> {/* For standard user interfaces */}<Avatar size="lg" /> {/* For profile headers and important displays */}<Avatar size="xl" /> {/* For hero sections and prominent features */}
Use appropriately sized images for better performance and quality.
// ✅ Good - Optimized image size with face detection<AvatarimageSrc="https://example.com/avatar.jpg?w=64&h=64&fit=crop&crop=face"imageAlt="User avatar"fallback={<span>U</span>}size="lg"/>
Always provide meaningful fallback content that represents the user.
// ✅ Good - Meaningful fallback with user initials<AvatarimageSrc={user.avatar}imageAlt={user.name}fallback={<span className="font-semibold text-white">{user.name.split(' ').map(n => n[0]).join('')}</span>}/>
Use variants to convey different meanings or contexts.
// ✅ Good - Variant-based stylingconst getAvatarVariant = (userRole) => {switch (userRole) {case 'admin': return 'ring';case 'moderator': return 'rounded';case 'user': return 'basic';default: return 'square';}};<Avatarvariant={getAvatarVariant(user.role)}fallback={<span>{user.initials}</span>}/>
Create different avatar sizes using the size prop:
// Small avatar (24x24)<Avatarsize="sm"imageSrc={user.avatar}imageAlt={user.name}fallback={<span className="text-xs font-medium">{user.initials}</span>}/>// Medium avatar (32x32) - Default<Avatarsize="md"imageSrc={user.avatar}imageAlt={user.name}fallback={<span className="text-sm font-medium">{user.initials}</span>}/>// Large avatar (48x48)<Avatarsize="lg"imageSrc={user.avatar}imageAlt={user.name}fallback={<span className="text-base font-semibold">{user.initials}</span>}/>// Extra large avatar (64x64)<Avatarsize="xl"imageSrc={user.avatar}imageAlt={user.name}fallback={<span className="text-lg font-bold">{user.initials}</span>}/>
Use the imageClassName and fallbackClassName props to apply custom styling:
// Custom image styling<AvatarimageSrc={user.avatar}imageAlt={user.name}fallback="JD"imageClassName="shadow-lg border-2 border-blue-300 rounded-full"fallbackClassName="bg-gradient-to-br from-blue-500 to-blue-600 text-white font-bold"/>// Advanced custom styling<Avatarvariant="ring"imageSrc={user.avatar}imageAlt={user.name}fallback="CS"size="lg"imageClassName="shadow-xl border-4 border-purple-300 hover:scale-105 transition-transform"fallbackClassName="bg-gradient-to-br from-purple-500 to-purple-700 text-white font-semibold text-lg"/>
Mix and match variants with sizes for different use cases:
// Professional profile - Large ring variant<Avatarvariant="ring"size="lg"imageSrc={user.avatar}imageAlt={user.name}fallback={<span className="font-semibold text-white">{user.initials}</span>}/>// Team member - Medium rounded variant<Avatarvariant="rounded"size="md"imageSrc={member.avatar}imageAlt={member.name}fallback={<span className="font-medium text-white">{member.initials}</span>}/>// Compact list - Small square variant<Avatarvariant="square"size="sm"imageSrc={user.avatar}imageAlt={user.name}fallback={<span className="text-xs font-medium">{user.initials}</span>}/>
Apply custom styles to match your design system:
<Avatarvariant="basic"size="lg"imageSrc={user.avatar}imageAlt={user.name}fallback={<div className="flex items-center justify-center w-full h-full bg-gradient-to-br from-primary-light to-primary text-white font-bold shadow-lg">{user.initials}</div>}/>
Make avatars interactive with hover effects and click handlers:
<buttononClick={() => handleAvatarClick(user.id)}className="transition-transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-2 rounded-full"><Avatarvariant="ring"size="lg"imageSrc={user.avatar}imageAlt={user.name}fallback={<div className="flex items-center justify-center w-full h-full bg-gradient-to-br from-primary-light to-primary text-white font-semibold">{user.initials}</div>}/></button>
- UserMenu: Dropdown menu triggered by avatar clicks
- UserCard: Card component with avatar and detailed user information
- TeamList: List component for displaying team members with avatars
- ProfilePicture: Specialized avatar component for profile pages
- AvatarGroup: Component for displaying multiple avatars together
For the complete API reference and advanced usage patterns, see the Avatar API documentation.
- Use appropriate image sizes (24x24, 32x32, 48x48, 64x64)
- Implement lazy loading for large avatar lists
- Use WebP format when possible for better compression
- Cache user initials to avoid recalculation
- Use consistent fallback patterns across your app
- Consider implementing skeleton loaders for better UX
- Always provide meaningful alt text
- Test with screen readers
- Ensure sufficient color contrast in fallback content
- Use appropriate sizes for different contexts
- Choose variants based on context and importance
- Use consistent variants for similar user types
- Consider accessibility implications of different variants
