UI Patterns
Display

Avatar

Avatar patterns — initials placeholder with a deterministic solid or gradient color from a hash of the name.

Examples

Initials with hashed color

When you don't have a profile picture (new account, no upload, image 404), fall back to the user's initials on a colored disc. The color is picked deterministically from a hash of the name, so the same person always gets the same swatch — that gives the avatar real recognition value across pages and lists, instead of the visual noise a single grey placeholder produces.

Install with the shadcn CLI (it also installs lib/avatar-initials.ts and shadcn's avatar), or copy the source below.

npx shadcn@latest add https://ui-patterns.stereobooster.com/r/avatar-initials.json
lib/avatar-initials.ts
// Hand-picked so every entry, alone or next to any other, reads well with
// white text. Random HSL gives muddy yellows and clashing pairs.
export const AVATAR_COLORS = [
  "#f43f5e", // rose-500
  "#ec4899", // pink-500
  "#d946ef", // fuchsia-500
  "#a855f7", // purple-500
  "#8b5cf6", // violet-500
  "#6366f1", // indigo-500
  "#3b82f6", // blue-500
  "#0284c7", // sky-600
  "#0891b2", // cyan-600
  "#0d9488", // teal-600
  "#059669", // emerald-600
  "#16a34a", // green-600
  "#65a30d", // lime-600
  "#d97706", // amber-600
  "#f97316", // orange-500
  "#ef4444", // red-500
];

// djb2 — small, well-distributed string hash. Locale-insensitive on the
// UTF-16 code units, which is what we want for a deterministic mapping.
function hash(str: string): number {
  let h = 5381;
  for (let i = 0; i < str.length; i++) {
    h = ((h << 5) + h + str.charCodeAt(i)) | 0;
  }
  return h;
}

export function avatarColor(name: string): string {
  return AVATAR_COLORS[
    Math.abs(hash(name.toLowerCase())) % AVATAR_COLORS.length
  ];
}

// Slice three independent values out of one 32-bit hash: two palette indices
// (forced to differ) and a gradient rotation in degrees.
export function avatarGradient(name: string): string {
  const h = Math.abs(hash(name.toLowerCase()));
  const i1 = h % AVATAR_COLORS.length;
  const i2 =
    (i1 + 1 + ((h >> 8) % (AVATAR_COLORS.length - 1))) % AVATAR_COLORS.length;
  const angle = (h >> 16) % 360;
  return `linear-gradient(${angle}deg, ${AVATAR_COLORS[i1]}, ${AVATAR_COLORS[i2]})`;
}

// Take first code point so surrogate-pair scripts (emoji, some CJK) don't
// get sliced in half.
function firstGlyph(token: string): string {
  const cp = token.codePointAt(0);
  return cp ? String.fromCodePoint(cp) : "";
}

export function getInitials(name: string): string {
  const tokens = name.trim().split(/\s+/).filter(Boolean);
  if (tokens.length === 0) return "?";
  if (tokens.length === 1) return firstGlyph(tokens[0]).toUpperCase();
  return (
    firstGlyph(tokens[0]) + firstGlyph(tokens[tokens.length - 1])
  ).toUpperCase();
}
components/ui/avatar-initials.tsx
import * as React from "react";

import { cn } from "@/lib/utils";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import {
  avatarColor,
  avatarGradient,
  getInitials,
} from "@/lib/avatar-initials";

interface AvatarInitialsProps extends React.ComponentProps<typeof Avatar> {
  name: string;
  src?: string;
  variant?: "solid" | "gradient";
}

function AvatarInitials({
  name,
  src,
  variant = "solid",
  className,
  ...props
}: AvatarInitialsProps) {
  return (
    <Avatar
      role="img"
      aria-label={name}
      className={cn("text-white", className)}
      {...props}
    >
      {src ? <AvatarImage src={src} alt="" /> : null}
      <AvatarFallback
        aria-hidden="true"
        className={cn(
          "text-[length:inherit] text-inherit",
          variant === "gradient" && "drop-shadow-sm",
        )}
        style={
          variant === "gradient"
            ? { backgroundImage: avatarGradient(name) }
            : { backgroundColor: avatarColor(name) }
        }
      >
        {getInitials(name)}
      </AvatarFallback>
    </Avatar>
  );
}

export { AvatarInitials, type AvatarInitialsProps };

Usage:

<AvatarInitials name="Ada Lovelace" />
<AvatarInitials name="Ada Lovelace" src={user.photoUrl} className="size-20 text-2xl" />

With src, the initials show while the photo loads and if it fails.

Notes:

  • Initials. Take the first code point of the first and last whitespace- separated token. codePointAt(0) (not charAt(0)) avoids slicing surrogate pairs in half — important for emoji and supplementary CJK. Single-token names get one letter; empty input gets ?.
  • Hash. djb2 is small, branch-free, and well-distributed for short ASCII strings. Don't use String.prototype numeric coercion or Date.now() — the whole point is determinism: refresh the page, see the same color. Lowercase before hashing so Jane Doe and jane doe collide.
  • Curated palette beats random HSL. A naive hsl(${hash % 360}, 70%, 50%) produces muddy yellows around hue 60° and unreadable cyans around 180°. Picking from a hand-picked list of ~16 Tailwind hues guarantees every avatar pairs cleanly with white text and stays in your design system.
  • Keep the palette length coprime with common name patterns — 16 works well; avoid 26 (alphabet length) which clusters by surname initial.
  • Accessibility. The initials mean nothing to a screen reader. The component gives the Avatar root role="img" and aria-label with the full name, so AT announces "Ada Lovelace" rather than "A L", and hides the initials with aria-hidden. aria-label alone on a plain <div> is ignored: without a role, there is nothing to name.
  • Color is not data. The hue is a recognition aid, not a category. Don't encode role, status, or membership in the avatar color — users will pattern-match and you'll regret it the first time someone changes their display name.
  • Hex with an inline style, not Tailwind classes. The same palette works for the gradient below, in email, and in any project, whatever its Tailwind setup.
  • For something richer than initials — geometric patterns, cartoon faces, pixel identicons — see Alternatives.

Initials with gradient

Same pattern, richer swatch: instead of one palette entry, pick two and blend them into a linear gradient, with the rotation angle also derived from the hash. Same determinism (one name, always the same gradient), dramatically more visual variety — 16 colors give 16 solid swatches but 16 × 15 × 360 ≈ 86,400 distinct gradients. This is the technique behind Vercel's avatar endpoint and Mark Miro's hash-gradient explorer.

Same component, variant="gradient":

<AvatarInitials name="Ada Lovelace" variant="gradient" />

Notes:

  • Three values from one hash. h % N for the first color index, (h >> 8) % (N-1) for the offset to the second (so they never collide), (h >> 16) % 360 for the angle. A 32-bit hash has more than enough entropy; no need to call hash() three times.
  • Hex, not Tailwind classes. The gradient is built at runtime, so the colors have to exist as strings the CSS engine can read — background-image: linear-gradient(...) via inline style. Tailwind's JIT can't see into dynamically-built class names; trying to cn together from-[${color}] will silently fail in production.
  • Pair the palette, don't randomize it. Two randomly-chosen HSL colors will eventually produce a puce-and-olive avatar. A hand-picked list where every pair is known-readable keeps the output consistently attractive. If you want more variety, grow the palette — don't switch to random HSL.
  • drop-shadow-sm on the initials gives them a faint outline that preserves contrast when the gradient passes through a lighter stop. Without it, white letters can disappear into yellows and limes.
  • Same accessibility rules apply: role="img" and aria-label on the Avatar root carry the full name; the gradient is decorative.
  • For SVG output (email, social share cards, OG images) build the same gradient as an <linearGradient> in a <defs> block and fill a <circle> or <rect> with it. That's what Vercel's avatar endpoint does — the algorithm is identical, only the rendering target changes.

Alternatives

Initials + color is the cheapest visual identity you can ship. When you want something richer — unique per user, harder to collide, or more stylistically distinctive — reach for one of these:

  • Boring Avatars — six SVG variants (beam, marble, pixel, sunset, ring, bauhaus) derived from a name plus a palette. Drops straight into AvatarFallback as an SVG, keeps the same "one name, one look" property as this page's demos.
  • DiceBear — 63 styles, from initials and geometric shapes to illustrated faces and pixel art, each generated from a seed. The code is MIT, but each style has its own license.
  • awesome-identicons — curated list of identicon libraries, algorithms, and research across many languages and visual styles. Start here before writing your own.
  • hash-avataaars — feeds a hash into the Avataaars cartoon-face construction kit, so each input yields a deterministic illustrated character with varied hair, clothing, and accessories.
  • blockies — the 8×8 pixel identicon style used as the default avatar in Ethereum wallets. Compact, high-contrast visual fingerprint of an address or any arbitrary string.
  • Opepen standard — Zapper's per-address avatar spec combining an Opepen-style illustration with a deterministic palette. Designed for web3 identity but the algorithm is general.
  • ACM 10.5555/3489212.3489338 — academic reference on visual identity hashing, for background on why these systems work (and where they fail: near-collisions for similar inputs, or large palettes where users can't distinguish two swatches).

On this page