Avatar
An image element with a fallback for representing the user.
CN
tsx
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
export function AvatarDemo() { return ( <Avatar> <AvatarImage src="http://github.com/fivex-labs.png" alt="@fivexlabs" /> <AvatarFallback>CN</AvatarFallback> </Avatar> )}
Installation
CLI
bash
npx fivui add avatar
Manual
Copy and paste the following code into your project.
bash
npm install @radix-ui/react-avatar
components/ui/avatar.tsx
"use client"
import * as React from "react"import * as AvatarPrimitive from "@radix-ui/react-avatar"
import { cn } from "@/lib/utils"
const Avatar = React.forwardRef< React.ElementRef<typeof AvatarPrimitive.Root>, React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>>(({ className, ...props }, ref) => ( <AvatarPrimitive.Root ref={ref} className={cn( "relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full", className )} {...props} />))Avatar.displayName = AvatarPrimitive.Root.displayName
const AvatarImage = React.forwardRef< React.ElementRef<typeof AvatarPrimitive.Image>, React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Image>>(({ className, ...props }, ref) => ( <AvatarPrimitive.Image ref={ref} className={cn("aspect-square h-full w-full", className)} {...props} />))AvatarImage.displayName = AvatarPrimitive.Image.displayName
const AvatarFallback = React.forwardRef< React.ElementRef<typeof AvatarPrimitive.Fallback>, React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback>>(({ className, ...props }, ref) => ( <AvatarPrimitive.Fallback ref={ref} className={cn( "flex h-full w-full items-center justify-center rounded-full bg-muted", className )} {...props} />))AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName
export { Avatar, AvatarImage, AvatarFallback }
Usage
tsx
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
tsx
<Avatar> <AvatarImage src="http://github.com/fivex-labs.png" /> <AvatarFallback>CN</AvatarFallback></Avatar>
Examples
Default
CN
tsx
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
export function AvatarDemo() { return ( <Avatar> <AvatarImage src="http://github.com/fivex-labs.png" alt="@fivexlabs" /> <AvatarFallback>CN</AvatarFallback> </Avatar> )}
Fallback
JD
tsx
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
export function AvatarFallback() { return ( <Avatar> <AvatarImage src="/broken-image.jpg" alt="Broken" /> <AvatarFallback>JD</AvatarFallback> </Avatar> )}
Sizes
CNCNCNCN
tsx
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
export function AvatarSizes() { return ( <div className="flex items-center gap-4"> <Avatar className="h-8 w-8"> <AvatarImage src="http://github.com/fivex-labs.png" alt="@fivexlabs" /> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar> <AvatarImage src="http://github.com/fivex-labs.png" alt="@fivexlabs" /> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar className="h-12 w-12"> <AvatarImage src="http://github.com/fivex-labs.png" alt="@fivexlabs" /> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar className="h-16 w-16"> <AvatarImage src="http://github.com/fivex-labs.png" alt="@fivexlabs" /> <AvatarFallback>CN</AvatarFallback> </Avatar> </div> )}
Group
FLU2U3+2
tsx
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
export function AvatarGroup() { return ( <div className="flex -space-x-2"> <Avatar className="border-2 border-background"> <AvatarImage src="http://github.com/fivex-labs.png" alt="@fivexlabs" /> <AvatarFallback>FL</AvatarFallback> </Avatar> <Avatar className="border-2 border-background"> <AvatarImage src="https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=400&h=400&q=80&crop=face&fit=crop" alt="@user2" /> <AvatarFallback>U2</AvatarFallback> </Avatar> <Avatar className="border-2 border-background"> <AvatarImage src="https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?w=400&h=400&q=80&crop=face&fit=crop" alt="@user3" /> <AvatarFallback>U3</AvatarFallback> </Avatar> <Avatar className="border-2 border-background"> <AvatarFallback>+2</AvatarFallback> </Avatar> </div> )}
API Reference
Avatar Components
Component | Description |
---|---|
Avatar | The root avatar container |
AvatarImage | The image element of the avatar |
AvatarFallback | The fallback element when image fails to load |
Avatar components inherit all props from Radix UI Avatar
primitives.