Skip to content

Avatar

Visual component for representing users with support for images, text initials, and status indicators. Available in multiple sizes and shapes with customizable borders and backgrounds.

Installation

Add Avatar to your project with the CK-UI CLI. It copies the source into your project, so the code is yours to change.

Terminal
npx ck-ui-cli@latest add avatar
Adds
Avatar.tsxto your CK-UI folder (src/components/ck-ui by default)
Installs
@radix-ui/react-avatarif your project does not have them yet

First time with CK-UI in this project? Run npx ck-ui-cli@latest init once before adding components. The installation guide explains what it sets up.

Manual installationInstall the packages and copy the files yourself.

Install the following dependencies

Run these commands in your terminal to install the required packages

Terminal
npm install @radix-ui/react-avatar

Create or update your Avatar component

Copy this code to create your Avatar component

/components/ui/Avatar.tsx
import * as React from "react";
import * as AvatarPrimitive from "@radix-ui/react-avatar";
import { cn } from "@/lib/utils";

// Server-compatible: this file uses no hooks. Image loading state lives in the Radix Avatar
// primitives (client components), so Avatar can be rendered from server components.

export type AvatarSize = "xs" | "sm" | "md" | "lg" | "xl";
export type AvatarShape = "rounded" | "square";

const SIZE_CLASSES: Record<AvatarSize, string> = {
    xs: "size-6",
    sm: "size-8",
    md: "size-10",
    lg: "size-14",
    xl: "size-20",
};

/** Canonical sizes map to Tailwind size classes; any other string is used as class names (e.g. `"size-12"`). */
export function resolveAvatarSize(size?: AvatarSize | (string & {}) | null): string {
    if (!size) return SIZE_CLASSES.lg;
    return Object.prototype.hasOwnProperty.call(SIZE_CLASSES, size) ? SIZE_CLASSES[size as AvatarSize] : size;
}

// Clipped with CSS (clip-path) instead of an SVG <clipPath>, so no element ids are needed.
const UserPlaceholder = () => (
    <svg aria-hidden="true" focusable="false" fill="none" viewBox="0 0 80 80" className="size-full text-neutral-300 [clip-path:circle(50%)] dark:text-neutral-600">
        <g fill="currentColor">
            <ellipse cx="40" cy="78" fillOpacity="0.72" rx="32" ry="24" />
            <circle cx="40" cy="32" r="16" opacity="0.9" />
        </g>
    </svg>
);

const UserIcon = () => (
    <svg aria-hidden="true" focusable="false" className="size-full text-gray-300 dark:text-gray-600" width="16" height="16" viewBox="0 0 16 16" fill="none">
        <rect x="0.62854" y="0.359985" width="15" height="15" rx="7.5" fill="white" />
        <path
            d="M8.12421 7.20374C9.21151 7.20374 10.093 6.32229 10.093 5.23499C10.093 4.14767 9.21151 3.26624 8.12421 3.26624C7.0369 3.26624 6.15546 4.14767 6.15546 5.23499C6.15546 6.32229 7.0369 7.20374 8.12421 7.20374Z"
            fill="currentColor"
        />
        <path
            d="M11.818 10.5975C10.2992 12.6412 7.42106 13.0631 5.37731 11.5537C5.01171 11.2818 4.69296 10.9631 4.42107 10.5975C4.28982 10.4006 4.27107 10.1475 4.37419 9.94123L4.51482 9.65059C4.84296 8.95684 5.53671 8.51624 6.30546 8.51624H9.95231C10.7023 8.51624 11.3867 8.94749 11.7242 9.62249L11.8742 9.93184C11.968 10.1475 11.9586 10.4006 11.818 10.5975Z"
            fill="currentColor"
        />
    </svg>
);

export interface AvatarRootProps extends React.HTMLAttributes<HTMLDivElement> {
    /** `xs` (24px) · `sm` (32px) · `md` (40px) · `lg` (56px, default) · `xl` (80px), or Tailwind size classes such as `"size-12"`. */
    size?: AvatarSize | (string & {});
    /** Placeholder shown when the avatar has no children: a person silhouette (`image`) or a user icon (`text`). */
    type?: "image" | "text";
    /** Adds a 1px border in the `border` token colour. */
    border?: boolean;
    /** Background classes, e.g. `"bg-muted"`. */
    background?: string;
    /** `rounded` (circle) or `square` (rounded-md). Avatar.Image follows the Root's corner radius. */
    shape?: AvatarShape;
}

const AvatarRoot = React.forwardRef<HTMLDivElement, AvatarRootProps>(
    ({ className, size, type = "image", border = false, background = "bg-transparent", shape = "rounded", children, ...props }, ref) => {
        const hasChildren = React.Children.count(children) > 0;

        return (
            <AvatarPrimitive.Root asChild>
                <div
                    ref={ref}
                    data-shape={shape}
                    className={cn(
                        "relative inline-flex shrink-0 items-center justify-center [&_img]:my-0 [&_img]:max-w-none",
                        resolveAvatarSize(size),
                        shape === "square" ? "rounded-md" : "rounded-full",
                        background,
                        border && "border border-border",
                        className
                    )}
                    {...props}
                >
                    {hasChildren ? children : type === "image" ? <UserPlaceholder /> : <UserIcon />}
                </div>
            </AvatarPrimitive.Root>
        );
    }
);
AvatarRoot.displayName = "Avatar.Root";

export interface AvatarImageProps extends Omit<React.ImgHTMLAttributes<HTMLImageElement>, "src"> {
    src?: string;
    /**
     * Alternative text. Defaults to `""` (decorative), which is right when the person's name is shown next to
     * the avatar. Pass a name (e.g. `alt="Jane Doe"`) when the avatar is the only identification.
     */
    alt?: string;
    /** Sets `loading="lazy"` on the rendered <img>. */
    lazy?: boolean;
    /**
     * Shown while the image loads, when it fails to load, or when `src` is empty.
     * Defaults to a user icon; pass `null` to render nothing.
     */
    fallback?: React.ReactNode;
}

/** Must be rendered inside Avatar.Root. The <img> is rendered once the image has loaded. */
const AvatarImage = React.forwardRef<HTMLImageElement, AvatarImageProps>(({ src, alt = "", lazy = false, className, fallback, ...props }, ref) => {
    return (
        <>
            <AvatarPrimitive.Image
                ref={ref}
                src={src}
                alt={alt}
                loading={lazy ? "lazy" : "eager"}
                // Inherit the Root's radius so shape="square" (or a custom rounded-* class on Root) applies to the image.
                className={cn("size-full rounded-[inherit] object-cover", className)}
                {...props}
            />
            {fallback !== null && (
                <AvatarPrimitive.Fallback className="flex size-full items-center justify-center rounded-[inherit]">{fallback === undefined ? <UserIcon /> : fallback}</AvatarPrimitive.Fallback>
            )}
        </>
    );
});
AvatarImage.displayName = "Avatar.Image";

export interface AvatarTextProps extends React.HTMLAttributes<HTMLSpanElement> {
    /** Text to show, typically initials. Falls back to `children`. */
    text?: string;
}

const AvatarText = React.forwardRef<HTMLSpanElement, AvatarTextProps>(({ text, className, children, ...props }, ref) => {
    return (
        <span ref={ref} className={cn("flex h-full w-full items-center justify-center text-gray-600 size-label-md dark:text-neutral-400", className)} {...props}>
            {text ?? children}
        </span>
    );
});
AvatarText.displayName = "Avatar.Text";

export interface AvatarStatusProps extends React.HTMLAttributes<HTMLSpanElement> {
    /** Background class for the dot, e.g. `"bg-green-500"`. */
    color?: string;
    position?: "top" | "bottom";
}

/** Status dot. Decorative (`aria-hidden`) unless it gets an accessible name via `aria-label`, e.g. `aria-label="Online"`. */
const AvatarStatus = React.forwardRef<HTMLSpanElement, AvatarStatusProps>(({ color = "bg-green-500", position = "bottom", className, style, ...props }, ref) => {
    const labelled = props["aria-label"] !== undefined || props["aria-labelledby"] !== undefined;
    return (
        <span
            ref={ref}
            role={labelled ? "img" : undefined}
            aria-hidden={labelled ? undefined : true}
            className={cn("absolute right-0 block h-3 w-3 rounded-full ring-2 ring-white dark:ring-gray-900", color, position === "top" ? "top-0" : "bottom-0", className)}
            style={{ transform: position === "top" ? "translate(-20%, 20%)" : "translate(-20%, -20%)", ...style }}
            {...props}
        />
    );
});
AvatarStatus.displayName = "Avatar.Status";

export interface AvatarIndicatorProps extends React.HTMLAttributes<HTMLDivElement> {
    position?: "top" | "bottom";
}

/** Positions custom content (e.g. a LegendIndicator) at the avatar's top-right or bottom-right corner. */
const AvatarIndicator = React.forwardRef<HTMLDivElement, AvatarIndicatorProps>(({ position = "bottom", className, ...props }, ref) => {
    return <div ref={ref} className={cn("absolute right-0 block", position === "top" ? "top-0" : "bottom-0", className)} {...props} />;
});
AvatarIndicator.displayName = "Avatar.Indicator";

export { AvatarRoot as Root, AvatarImage as Image, AvatarText as Text, AvatarStatus as Status, AvatarIndicator as Indicator };

Test Your Component

  • 1. Add the component to a test page
  • 2. Check all props and variants work as expected
  • 3. Verify responsive behavior
  • 4. Test with different content/data
  • 5. Ensure accessibility features work correctly

Sizes

Choose a predefined size (`xs` to `xl`) or pass Tailwind size classes, from large profile displays to compact navigation elements and lists.

Shape square

Use the `shape="square"` prop to create square-shaped avatars. This option is particularly useful for uniform layouts or when you need a consistent visual style across your application.

Placeholder image

Create minimal avatar placeholders by omitting the image or text content. If an image fails to load, Avatar.Image shows its fallback instead. Set custom backgrounds using standard Tailwind classes (like "bg-gray-100" or "bg-blue-50") through the background prop or className. Perfect for loading states or default user representations.

With text

Add text initials to avatars for quick identification. Use the `text` prop to set the text content or the `className` prop to customize the text style.

RK

Custom border

Add custom borders to avatars for visual differentiation. Use the `border` prop to add a border and the `className` prop to set its color or width.

RK
RK
RK
RK
RK

With background

Add custom backgrounds to avatars for visual differentiation. Use the `background` prop to set the background color or the `className` prop to customize the background style.

RK
RK
RK
RK
RK

With indicator

Add indicators to avatars for additional information. Place content in `Avatar.Indicator` and use its `position` prop (top or bottom) or the `className` prop to customize it.

Other example with custom icons

Enhance avatars with custom status indicators using icon components. Combine colored indicators with appropriate icons to represent various states like favorites, pinned items, approved status, or errors. Perfect for interactive user lists and activity feeds.

Stacked

Stack multiple avatars to display a group of users or entities. Wrap them in a flex container with negative spacing (e.g. `-space-x-4`) and give each avatar a border in the background colour.

+99
+6

API Reference

The Avatar component provides a flexible way to display user avatars with various styling options.

Avatar.Root

Container element that holds all avatar components and provides common styling.

PropTypeDefaultDescription
size
"xs""sm""md""lg""xl""string"
"lg"

xs 24px · sm 32px · md 40px · lg 56px · xl 80px, or Tailwind size classes such as "size-12"

type
"image""text"
"image"

Placeholder shown when the avatar has no children: a person silhouette (image) or a user icon (text)

border"boolean""false"

Adds a 1px border; set the colour with className (e.g. border-blue-500)

background"string""bg-transparent"

Background classes

shape
"rounded""square"
"rounded"

Circle or rounded square. Avatar.Image follows the root's corner radius.

className"string""undefined"

Additional CSS classes for the avatar element, merged last

Avatar.Image

Displays an image within the avatar with proper sizing and fallback handling. Must be rendered inside Avatar.Root.

PropTypeDefaultDescription
src"string""undefined"

URL of the image. The <img> is rendered once the image has loaded.

alt"string"""""

Empty by default (decorative), which suits avatars shown next to the person's name. Pass the name when the avatar is the only identification.

lazy"boolean""false"

Sets loading="lazy" on the rendered <img>

fallback"ReactNode""user icon"

Shown while the image loads, if it fails, or without src. Pass null to render nothing.

Avatar.Text

Displays text (typically initials) when an image is not available or preferred.

PropTypeDefaultDescription
text"string""undefined"

Text content to display (typically initials); falls back to children

Avatar.Status

Adds a colored dot indicator to show user status (online, away, busy, etc.).

PropTypeDefaultDescription
color"string""bg-green-500"

CSS class for the status indicator color

position
"top""bottom"
"bottom"

Position of the status indicator

aria-label"string""undefined"

Accessible name, e.g. "Online". Without it the dot is decorative (aria-hidden).

Avatar.Indicator

Allows for custom indicators like badges or icons to be placed on the avatar.

PropTypeDefaultDescription
children"ReactNode""undefined"

Custom indicator content

position
"top""bottom"
"bottom"

Position of the custom indicator

className"string""undefined"

Additional CSS classes

Used in these blocks

Block families with at least one block built on this component. Each page previews its blocks and lists the components and packages they use.