---
title: "Avatar · Base UI — StyleX Blocks"
description: "User portraits with image and initials fallback, rounded or circular, from 2xs to 2xl."
url: "https://stylexblocks.com/components/base-ui/avatar/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Avatar

User portraits with image and initials fallback, rounded or circular, from 2xs to 2xl.

- Behavior engine: Base UI
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/avatar.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/avatar.md)
- Category: Display
- Variants: subtle, solid, outline
- Sizes: xs, sm, md, lg, xl
- HTML page: https://stylexblocks.com/components/base-ui/avatar/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style base-ui
```

Then add the item as source you own:

```sh
npx stylexblocks add avatar
```

Equivalent: `npx shadcn add @stylexblocks/avatar`. Registry item JSON: https://stylexblocks.com/r/base-ui/avatar.json

## Usage

```tsx
import { Avatar } from "#components/ui/base-ui/avatar.tsx";

export function Example() {
  return (
    // Base UI's avatar machine wears the recipe: image load/error state
    // drives the fallback, while variant/size/shape resolve once on Root
    <Avatar.Root variant="subtle" size="md" shape="full">
      <Avatar.Image src="/avatar.png" alt="Eelco Wiersma" />
      <Avatar.Fallback>EW</Avatar.Fallback>
    </Avatar.Root>
  );
}
```

## Source

`components/ui/base-ui/avatar.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { Avatar as BaseAvatar } from "@base-ui/react/avatar";
import { createSlotRecipeContext } from "#lib/base-ui/create-slot-recipe-context.tsx";
import { defineSlotRecipe } from "#lib/recipe-props.ts";
import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'
import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'
import { radii } from '@stylexblocks/ui/tokens/radii.stylex.ts'
import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'

const avatarRoot = stylex.create({
  base: {
    '--avatar-font-size': 'calc(var(--avatar-size) / 2)',
    display: 'inline-flex',
    alignItems: 'center',
    justifyContent: 'center',
    fontWeight: fontWeights.medium,
    position: 'relative',
    verticalAlign: 'top',
    flexShrink: 0,
    userSelect: 'none',
    width: 'var(--avatar-size)',
    height: 'var(--avatar-size)',
    fontSize: 'var(--avatar-font-size)',
    borderRadius: 'var(--avatar-radius)',
  },
})

const avatarRootSizes = stylex.create({
  full: {
    '--avatar-size': '100%',
    '--avatar-font-size': 'calc(var(--tag-avatar-size) / 2)',
  },
  '2xs': {
    '--avatar-size': sizes._4,
  },
  xs: {
    '--avatar-size': sizes._5,
  },
  sm: {
    '--avatar-size': sizes._6,
  },
  md: {
    '--avatar-size': sizes._9,
  },
  lg: {
    '--avatar-size': sizes._12,
  },
  xl: {
    '--avatar-size': sizes._16,
  },
  '2xl': {
    '--avatar-size': sizes._20,
  },
})

const avatarRootVariants = stylex.create({
  solid: {
    backgroundColor: colorPalette.solid,
    color: colorPalette.contrast,
  },
  subtle: {
    backgroundColor: colorPalette.muted,
    color: colorPalette.fg,
  },
  outline: {
    color: colorPalette.fg,
    borderWidth: '1px',
    borderColor: colorPalette.muted,
  },
})

const avatarRootShapes = stylex.create({
  rounded: {
    '--avatar-radius': semanticRadii.indicatorMd,
  },
  full: {
    '--avatar-radius': radii.full,
  },
})

const avatarRootBorderless = stylex.create({
  true: {},
})

const avatarImage = stylex.create({
  base: {
    width: '100%',
    height: '100%',
    objectFit: 'cover',
    borderRadius: 'var(--avatar-radius)',
  },
})

const avatarFallback = stylex.create({
  base: {
    lineHeight: 1,
    textTransform: 'uppercase',
    fontWeight: fontWeights.medium,
    fontSize: 'var(--avatar-font-size)',
    borderRadius: 'var(--avatar-radius)',
  },
})

export type AvatarSize = keyof typeof avatarRootSizes

export type AvatarVariant = keyof typeof avatarRootVariants

export type AvatarShape = keyof typeof avatarRootShapes

export type AvatarBorderless = keyof typeof avatarRootBorderless

// defineSlotRecipe validates the shape at compile time: default values must
// exist in the axis maps, and slot keys must be `styles` or a declared axis
// (axis maps are looked up by pluralized axis name: `shape` → `shapes`)
const avatarSlotRecipe = defineSlotRecipe({
  slots: {
    root: {
      styles: avatarRoot,
      sizes: avatarRootSizes,
      variants: avatarRootVariants,
      shapes: avatarRootShapes,
    },
    image: { styles: avatarImage },
    fallback: { styles: avatarFallback },
  },
  defaultVariants: { size: "md", shape: "full", variant: "solid" },
});

/**
 * Avatar — recipe and component colocated in one file, worn by Base UI's
 * real avatar machine: the styled slots render `Avatar.Root/Image/Fallback`
 * from `@base-ui/react/avatar` through the `render` prop, so
 * image load/error state drives the fallback while `<Avatar.Root variant
 * size shape>` resolves the recipe once and every part inherits it through
 * context.
 */
const { withProvider, withContext } = createSlotRecipeContext(avatarSlotRecipe);

const StyledRoot = withProvider("div", "root", "AvatarRoot");
const StyledImage = withContext("img", "image", "AvatarImage");
const StyledFallback = withContext("span", "fallback", "AvatarFallback");

export const Avatar = {
  Root: (props: React.ComponentProps<typeof StyledRoot>) => (
    <StyledRoot {...props} render={<BaseAvatar.Root />} />
  ),
  Image: (props: React.ComponentProps<typeof StyledImage>) => (
    <StyledImage {...props} render={<BaseAvatar.Image />} />
  ),
  Fallback: (props: React.ComponentProps<typeof StyledFallback>) => (
    <StyledFallback {...props} render={<BaseAvatar.Fallback />} />
  ),
};
```
