Menu

Components

Avatar

A small picture of a person or thing, with initials as the fallback, and a group that stacks them.

Introduction

Avatar puts a face to a name: the author of a comment, a row in a member list, the account in a menu. It shows a picture and, when there is none, something useful in its place.

  • Falls back to the initials of name, then to your own fallback, then to a person icon, when there is no picture or it fails to load.
  • Five sizes, a circle or a rounded square, and a status dot for online, away, busy and offline.
  • Decorative by default, because the name is normally written next to it. Pass alt when it stands alone.
  • AvatarGroup stacks avatars and counts the ones that do not fit.

Select, Autocomplete and Combobox use Avatar for options that have an avatar.

Installation

centerUI has no CLI or registry yet: components are copied into your project.

# bun
bun add tailwind-variants tailwind-merge clsx

# pnpm
pnpm add tailwind-variants tailwind-merge clsx

# npm
npm install tailwind-variants tailwind-merge clsx

Manual installation

Copy these files into your project and keep the paths, or adjust the imports.

1. Add the styles and the group context

src/types/avatar-styles.tsShow code
import { tv } from "tailwind-variants";

// Styles for Avatar. The root is not clipped, so the status dot can sit on its
// edge; the picture and the fallback live in `frame`, which is.
export const avatarStyles = tv({
  slots: {
    root: "relative inline-flex shrink-0 align-middle",
    frame:
      "inline-flex size-full select-none items-center justify-center overflow-hidden bg-muted font-medium text-muted-fg",
    image: "size-full object-cover",
    status: "absolute rounded-full ring-2 ring-bg",
  },
  variants: {
    size: {
      xs: { root: "size-5", frame: "text-[9px]", status: "size-1.5" },
      sm: { root: "size-6", frame: "text-[10px]", status: "size-2" },
      md: { root: "size-8", frame: "text-xs", status: "size-2.5" },
      lg: { root: "size-10", frame: "text-sm", status: "size-3" },
      xl: { root: "size-14", frame: "text-base", status: "size-3.5" },
    },
    shape: {
      circle: {
        root: "rounded-full",
        frame: "rounded-full",
        status: "right-0 bottom-0",
      },
      square: {
        root: "rounded-md",
        frame: "rounded-md",
        status: "-right-0.5 -bottom-0.5",
      },
    },
    status: {
      online: { status: "bg-success" },
      away: { status: "bg-warning" },
      busy: { status: "bg-danger" },
      offline: { status: "bg-muted-fg/60" },
    },
    // Inside an AvatarGroup: a ring in the page color separates the overlaps.
    grouped: {
      true: { root: "ring-2 ring-bg" },
    },
  },
  defaultVariants: { size: "sm", shape: "circle" },
});

export const avatarGroupStyles = tv({
  base: "flex w-fit items-center -space-x-2",
  variants: {
    size: {
      xs: "-space-x-1.5",
      sm: "-space-x-2",
      md: "-space-x-2.5",
      lg: "-space-x-3",
      xl: "-space-x-4",
    },
  },
  defaultVariants: { size: "sm" },
});
src/types/avatar-group-context.tsShow code
import { createContext, useContext } from "react";
import type { VariantProps } from "tailwind-variants";
import type { avatarStyles } from "./avatar-styles";

/** Defaults an AvatarGroup passes down to the avatars inside it. */
export type AvatarGroupContextValue = Pick<
  VariantProps<typeof avatarStyles>,
  "size" | "shape"
>;

const AvatarGroupContext = createContext<AvatarGroupContextValue | null>(null);

export const AvatarGroupProvider = AvatarGroupContext.Provider;

/** The enclosing AvatarGroup's defaults, or null outside of one. */
export function useAvatarGroup() {
  return useContext(AvatarGroupContext);
}

2. Add the Avatar file

src/components/ui/avatar/avatar.tsxShow code
"use client";

import Image from "next/image";
import { Children, type ReactNode, useState } from "react";
import { UserIcon } from "@/components/icons";
import { cn } from "@/lib/cn";
import {
  AvatarGroupProvider,
  useAvatarGroup,
} from "@/types/avatar-group-context";
import { avatarGroupStyles, avatarStyles } from "@/types/avatar-styles";

type Size = "xs" | "sm" | "md" | "lg" | "xl";

// The size in pixels, so the image is requested at the size it is drawn.
const pixels: Record<Size, number> = { xs: 20, sm: 24, md: 32, lg: 40, xl: 56 };

export interface AvatarProps {
  /** Image URL. Falls back to the initials of `name` if missing or if it fails to load. */
  src?: string;
  /** Used for the initials. Also the accessible name when the avatar has a `status`. */
  name?: string;
  /** Describes the image. Leave it out when the name is written next to the avatar. */
  alt?: string;
  /** Shown when there is no image and no `name`. Defaults to a person icon. */
  fallback?: ReactNode;
  size?: Size;
  shape?: "circle" | "square";
  /** A small dot on the corner. */
  status?: "online" | "away" | "busy" | "offline";
  className?: string;
}

function initialsOf(name?: string) {
  if (!name) return "";
  return name
    .trim()
    .split(/\s+/)
    .slice(0, 2)
    .map((word) => word[0]?.toUpperCase() ?? "")
    .join("");
}

/**
 * A small picture of a person or thing. When there is no picture, or it fails
 * to load, it shows the initials of `name`, then `fallback`, then a person
 * icon. It is decorative unless you pass `alt` (or a `status`), because the
 * name is normally written next to it.
 *
 * Inside an `AvatarGroup` it takes its size and shape from the group.
 *
 * @example
 * <Avatar src="/ana.png" name="Ana Wijaya" status="online" />
 */
export function Avatar({
  src,
  name,
  alt,
  fallback,
  size,
  shape,
  status,
  className,
}: AvatarProps) {
  const group = useAvatarGroup();
  // Remember which URL failed, so a new `src` gets its own chance.
  const [failedSrc, setFailedSrc] = useState<string | null>(null);
  const showImage = Boolean(src) && failedSrc !== src;
  const resolvedSize = size ?? group?.size ?? "sm";

  const styles = avatarStyles({
    size: resolvedSize,
    shape: shape ?? group?.shape,
    status,
    grouped: group ? true : undefined,
  });

  const initials = initialsOf(name);
  const content =
    showImage && src ? (
      <Image
        src={src}
        alt=""
        width={pixels[resolvedSize]}
        height={pixels[resolvedSize]}
        unoptimized
        onError={() => setFailedSrc(src)}
        className={styles.image()}
      />
    ) : (
      initials ||
      fallback || <UserIcon className="size-1/2" aria-hidden="true" />
    );

  // A status is information, so it is spoken along with the name.
  const label = alt ?? (status ? name : undefined);
  const spoken = label ? (status ? `${label}, ${status}` : label) : undefined;

  const classes = cn(styles.root(), className);
  const inner = (
    <>
      <span className={styles.frame()}>{content}</span>
      {status ? <span aria-hidden="true" className={styles.status()} /> : null}
    </>
  );

  if (spoken) {
    return (
      <span role="img" aria-label={spoken} className={classes}>
        {inner}
      </span>
    );
  }
  return (
    <span aria-hidden="true" className={classes}>
      {inner}
    </span>
  );
}

export interface AvatarGroupProps {
  /** The avatars. */
  children: ReactNode;
  /** Show at most this many avatars, then a "+N" for the rest. */
  max?: number;
  /** Passed down to every avatar in the group. */
  size?: Size;
  /** Passed down to every avatar in the group. */
  shape?: "circle" | "square";
  /** Names the group for screen readers, for example "Project members". */
  "aria-label"?: string;
  className?: string;
}

/**
 * Avatars in an overlapping row, for members of a team or people in a thread.
 * The group sets the size and shape of the avatars inside it, and with `max`
 * it shows the first few and a "+N" for the rest.
 *
 * @example
 * <AvatarGroup max={3} aria-label="Project members">
 *   <Avatar name="Ana Wijaya" />
 *   <Avatar name="Budi Santoso" />
 * </AvatarGroup>
 */
export function AvatarGroup({
  children,
  max,
  size = "sm",
  shape = "circle",
  className,
  ...props
}: AvatarGroupProps) {
  const all = Children.toArray(children);
  const shown = max !== undefined && all.length > max ? all.slice(0, max) : all;
  const hidden = all.length - shown.length;

  return (
    <AvatarGroupProvider value={{ size, shape }}>
      {/* biome-ignore lint/a11y/useSemanticElements: a div with role="group" is the standard pattern for a labelled set of avatars; <fieldset> brings form styling quirks. */}
      <div
        role="group"
        aria-label={props["aria-label"]}
        className={cn(avatarGroupStyles({ size }), className)}
      >
        {shown}
        {hidden > 0 ? (
          <Avatar
            fallback={`+${hidden}`}
            alt={`${hidden} more`}
            className="[&>span]:bg-muted [&>span]:text-fg"
          />
        ) : null}
      </div>
    </AvatarGroupProvider>
  );
}

It imports UserIcon from @/components/icons, and uses next/image for the picture. Add the icon, or swap it for your own, and use a plain img outside Next.js.

3. Use it

import { Avatar, AvatarGroup } from "@/components/ui/avatar/avatar";

Anatomy

A clipped frame with the picture or the fallback, an optional status dot, and a group around several.

<AvatarGroup max={3} aria-label="Project members">
  <Avatar src="/ana.png" name="Ana Wijaya" status="online" />
  <Avatar name="Budi Santoso" />
  <Avatar name="Citra Lestari" />
  <Avatar name="Dimas Pratama" />
</AvatarGroup>
PartRendered asNotes
AvatarspanThe box. It is not clipped, so the status dot can sit on its edge.
Picturenext/imageFills the box. If the URL is missing or fails to load, the fallback is shown.
Fallbacktext or iconThe initials of name, then the fallback prop, then a person icon.
Status dotspanA small colored dot on the corner, set with status.
AvatarGroupdiv with role groupOverlaps its avatars, sets their size and shape, and with max adds a +N for the rest.

Examples

Avatars are small, so the examples below use real pictures where it helps.

Usage

Fallback

With no picture, or one that fails to load, the avatar shows the initials of the name, then the fallback prop, then a person icon.

Sizes

Shapes

A circle for people, a rounded square for things such as teams, workspaces or products.

Status

A dot on the corner. The ring around it keeps it readable on any picture.

Accessible name

An avatar next to the person's name adds nothing for a screen reader, so it is hidden from it. When it stands alone, pass alt. A status is always spoken, with the name.

Ana Wijaya

With text

The usual pairing: an avatar and the details of the person beside it.

Ana Wijaya

ana@example.com

Group

AvatarGroup overlaps its avatars. A ring in the page color separates them.

Group with a maximum

With max, the group shows that many avatars and a +N for the rest.

+3

Group size and shape

The group sets the size and shape of every avatar in it.

+2

Component API

Avatar and AvatarGroup take the props below.

Avatar

PropTypeDefault
srcstring-
namestring-
altstring-
fallbackReactNodeperson icon
size"xs" | "sm" | "md" | "lg" | "xl""sm"
shape"circle" | "square""circle"
status"online" | "away" | "busy" | "offline"-
classNamestring-

AvatarGroup

PropTypeDefault
childrenReactNode- (required)
maxnumber-
size"xs" | "sm" | "md" | "lg" | "xl""sm"
shape"circle" | "square""circle"
aria-labelstring-
classNamestring-