Avatar Component — Astro
User avatar with image or initials fallback, sizes and shapes
Avatar Component
An accessible avatar for users: show a profile image when available, or initials derived from a name (or custom initials) when no image is provided. Supports multiple sizes and circle or square shape.
Add this component
The command below includes <strong>Avatar</strong>—run it in your project directory to install this component (and the CSS if needed). No prompts.
Choose your package manager — click a tab to select, then copy the command.
Props
src(string, optional) - Image URL; when provided, the image is shownalt(string, optional) - Alt text for the imagename(string, optional) - Full name used to derive initials when no image (e.g. "Jane Doe" → "JD")initials(string, optional) - Override initials when no image (e.g. "JD"); ignored ifnameis providedsize(string, optional) -sm,md,lg(default: md)shape(string, optional) -circle,square(default: circle)class(string, optional) - Additional CSS classes
Basic Usage (initials)
When no src is provided, use name to show initials (first letter of first and last word, or first two letters of a single word).
Usage
Full example for each framework (Astro, Vanilla, Svelte, Vue, React). Switch framework via View as or by clicking a Usage tab—both stay in sync.
Sizes
<Avatar name="Jane Doe" size="sm" />
<Avatar name="Jane Doe" size="md" />
<Avatar name="Jane Doe" size="lg" /> Shape
Use shape="square" for a rounded square; default is circle.
<Avatar name="Jane Doe" shape="circle" />
<Avatar name="Jane Doe" shape="square" /> With image
Pass src (and alt) to show a profile image. The image is scaled to cover the avatar with object-fit: cover.
<Avatar
src="/path/to/photo.jpg"
alt="Jane Doe"
size="md"
/> Features
- Accessible -
role="img"andaria-labelfrom alt, name, or initials - Initials - Derived from
name(e.g. "Jane Doe" → "JD") or set viainitials - Image - Optional
srcwithloading="lazy"andobject-fit: cover - Sizes - sm, md, lg
- Shape - circle (default) or square with rounded corners