Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Avatar for Astro

Astro profile images and text fallbacks in a shared size system.

Example teammate
SV
Example teammate
SV
MR
Example teammate
SV
Example teammate
SV
Example teammate
SV

Installation

bunx --bun shadcn@latest add @gnostic/avatar
npx shadcn@latest add @gnostic/avatar
pnpm dlx shadcn@latest add @gnostic/avatar

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/ui/avatar/avatar-badge.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"span">

const { class: className, ...props } = Astro.props
---

<span
  data-slot="avatar-badge"
  class={cn("bg-primary text-primary-foreground ring-background absolute right-0 bottom-0 z-10 inline-flex items-center justify-center rounded-full bg-blend-color ring-2 select-none group-data-[size=sm]/avatar:size-2 group-data-[size=sm]/avatar:[&>svg]:hidden group-data-[size=default]/avatar:size-2.5 group-data-[size=default]/avatar:[&>svg]:size-2 group-data-[size=lg]/avatar:size-3 group-data-[size=lg]/avatar:[&>svg]:size-2", className)}
  {...props}
>
  <slot />
</span>
src/components/ui/avatar/avatar-fallback.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="avatar-fallback"
  class={cn("absolute inset-0 flex size-full items-center justify-center rounded-full bg-muted group-data-[size=lg]/avatar:text-sm group-data-[size=sm]/avatar:text-xs", className)}
  {...props}
>
  <slot />
</div>
src/components/ui/avatar/avatar-group-count.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="avatar-group-count"
  class={cn("bg-muted text-muted-foreground ring-background relative flex size-8 shrink-0 items-center justify-center rounded-full text-sm ring-2 group-has-data-[size=lg]/avatar-group:size-10 group-has-data-[size=sm]/avatar-group:size-6 [&>svg]:size-4 group-has-data-[size=lg]/avatar-group:[&>svg]:size-5 group-has-data-[size=sm]/avatar-group:[&>svg]:size-3", className)}
  {...props}
>
  <slot />
</div>
src/components/ui/avatar/avatar-group.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="avatar-group"
  class={cn(
    "*:data-[slot=avatar]:ring-background group/avatar-group flex -space-x-2 *:data-[slot=avatar]:ring-2",
    className,
  )}
  {...props}
>
  <slot />
</div>
src/components/ui/avatar/avatar-image.astro
---
import { Image } from "astro:assets"
import type { ComponentProps } from "astro/types"

import { cn } from "cn"

type Props = Partial<ComponentProps<typeof Image>>

const {
  class: className,
  alt = "",
  width = 44,
  height = 44,
  sizes = "44px",
  widths = [44, 88, 176],
  ...props
} = Astro.props
---

{props.src && (
  <Image
    data-slot="avatar-image"
    class={cn("absolute inset-0 z-10 aspect-square size-full rounded-full object-cover", className)}
    alt={alt}
    width={width}
    height={height}
    sizes={sizes}
    widths={widths}
    {...(props as any)}
  />
)}
src/components/ui/avatar/avatar.astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div"> & {
  size?: "default" | "sm" | "lg"
}

const { class: className, size = "default", ...props } = Astro.props
---

<div
  data-slot="avatar"
  data-size={size}
  class={cn("relative isolate inline-flex size-8 shrink-0 select-none items-center justify-center overflow-hidden rounded-full bg-background align-middle font-medium text-xs data-[size=lg]:size-10 data-[size=sm]:size-6 group/avatar", className)}
  {...props}
>
  <slot />
</div>
src/components/ui/avatar/index.ts
export { default as Avatar } from "./avatar.astro"
export { default as AvatarBadge } from "./avatar-badge.astro"
export { default as AvatarFallback } from "./avatar-fallback.astro"
export { default as AvatarGroup } from "./avatar-group.astro"
export { default as AvatarGroupCount } from "./avatar-group-count.astro"
export { default as AvatarImage } from "./avatar-image.astro"

Usage

import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
<Avatar>
  <AvatarImage src="/avatar.jpg" alt="Team member" />
  <AvatarFallback>TM</AvatarFallback>
</Avatar>

Composition

Arrange the parts as shown for an Avatar:

Avatar
├── AvatarImage
├── AvatarFallback
└── AvatarBadge

Arrange the parts as shown for an AvatarGroup:

AvatarGroup
├── Avatar
│   ├── AvatarImage
│   ├── AvatarFallback
│   └── AvatarBadge
├── Avatar
│   ├── AvatarImage
│   ├── AvatarFallback
│   └── AvatarBadge
└── AvatarGroupCount

Examples

Fallback

Use AvatarFallback when an image is unavailable or intentionally omitted.

MR

Group

Use AvatarGroup when you need overlapping participant or collaborator lists.

Example teammate
SV
Example teammate
TM
+3

Notes

  • Always include AvatarFallback so initials or placeholder content remain visible when the image fails to load.
  • Use size variants on Avatar instead of custom width and height classes when possible.

API Reference

Read the installed source for the complete prop types.

When to use Avatar

Use Avatar beside a person or team name. Put AvatarImage and AvatarFallback inside the same root. Use initials or a short mark for the fallback, and choose one size for a row of related people.

Keyboard and behavior

Supply meaningful image alternative text if the image conveys identity that is absent from nearby text. Use empty alt text when the adjacent name already supplies it. This Astro version does not include Base UI image loading state; a broken image URL does not automatically switch to the fallback.

Theme and layout

The root clips content to its rounded shape and the fallback uses muted colors. Check long initials and transparent logos in both modes. Resolve an absent image in your data before rendering when you need a reliable fallback.