Skip to content

Avatar

The Avatar component provides a versatile way to display user or entity representations. It supports images, initials, and fallback icons with customizable sizes, shapes, and color schemes. The component is designed to be accessible and adapts to both light and dark modes automatically.

Use the Avatar component when you need to:

  • Display user profile pictures
  • Show team members or contributors
  • Represent entities in a visual way
  • Create user directories or listings
  • Show authors of content
  • Indicate participants in discussions
  • Display account information
JD
JD
JS
RJ
AL
MT

The Avatar component handles different content types and provides options for accessible fallbacks.

Learn how to implement the Avatar component in your project, from basic usage to advanced configurations.

---
import { Avatar } from 'accessible-astro-components'
---
<!-- With image -->
<Avatar
img="https://example.com/profile.jpg"
label="User profile"
/>
<!-- With initials -->
<Avatar
initials="JD"
label="John Doe"
/>
<!-- Default fallback icon -->
<Avatar
label="Unknown user"
/>
<Avatar
initials="SM"
size="sm"
label="Small avatar"
/>
<Avatar
initials="MD"
size="md"
label="Medium avatar"
/>
<Avatar
initials="LG"
size="lg"
label="Large avatar"
/>
<!-- Custom size -->
<Avatar
initials="CS"
size="10rem"
label="Custom size avatar"
/>
<!-- Different types (color schemes) -->
<Avatar initials="DF" type="default" label="Default type" />
<Avatar initials="IN" type="info" label="Info type" />
<Avatar initials="SU" type="success" label="Success type" />
<Avatar initials="WA" type="warning" label="Warning type" />
<Avatar initials="ER" type="error" label="Error type" />
<!-- Square shape -->
<Avatar
initials="SQ"
shape="square"
label="Square avatar"
/>
<Avatar
img="https://example.com/profile.jpg"
title="Jane Smith"
subtitle="Product Designer"
/>
<Avatar
initials="JS"
title="Jane Smith"
subtitle="Product Designer"
/>
---
import { Avatar, AvatarGroup } from 'accessible-astro-components'
---
<!-- Condensed group (overlapping) -->
<AvatarGroup overlapAmount="-1rem">
<Avatar initials="JD" label="John Doe" />
<Avatar initials="JS" label="Jane Smith" />
<Avatar initials="RJ" label="Robert Johnson" />
</AvatarGroup>
<!-- Grid layout -->
<AvatarGroup
display="grid"
gridItemsSize="200px"
gridGap="1rem"
>
<Avatar
img="https://example.com/user1.jpg"
title="John Doe"
subtitle="Developer"
/>
<Avatar
img="https://example.com/user2.jpg"
title="Jane Smith"
subtitle="Designer"
/>
<Avatar
initials="RJ"
title="Robert Johnson"
subtitle="Manager"
/>
</AvatarGroup>

Configure the Avatar component using these available props to customize its behavior and appearance.

PropTypeDefaultDescription
imgstring-Image source URL for the avatar
initialsstring-Initials to display when no image is available
type'default' | 'info' | 'success' | 'warning' | 'error''default'Type/color variant of the avatar
shape'round' | 'square''round'Shape of the avatar
size'sm' | 'md' | 'lg' | string'md'Size of the avatar (predefined or custom value)
titlestring-Primary text to display next to the avatar
subtitlestring-Secondary text to display next to the avatar
labelstring-Accessible label for screen readers (required when no title is provided)
classstring''Additional CSS classes to apply

Configure the AvatarGroup component to control how multiple avatars are displayed.

PropTypeDefaultDescription
display'condensed' | 'grid''condensed'Display mode for the avatar group
gridItemsSizestring'250px'Size of the grid items (used for grid display mode)
gridGapstring'1rem'Gap between grid items (used for grid display mode)
overlapAmountstring'-1rem'Overlap amount for condensed mode (negative margin)
classstring''Additional CSS classes to apply

Accessibility isn’t an afterthought - it’s built into the core of this component through semantic HTML elements and proper ARIA attributes. The Avatar component is built with accessibility in mind:

  • Requires an accessible label via label prop or title prop
  • Uses empty alt attributes for decorative images
  • Provides fallback content when images fail to load
  • High contrast color combinations
  • Automatic light/dark mode adaptation
  • Semantic HTML structure

Make the Avatar component your own with custom styling while maintaining its accessibility features.

/* Option 1: Using :global() in your style block */
<style>
:global(.avatar .initials-container) {
border-width: 3px;
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
}
:global(.avatar .title) {
font-weight: 700;
}
:global(.avatar .subtitle) {
font-style: italic;
}
</style>
/* Option 2: Using is:global on the style tag */
<style is:global>
.avatar .initials-container {
border-width: 3px;
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1);
}
.avatar .title {
font-weight: 700;
}
.avatar .subtitle {
font-style: italic;
}
</style>

See the Avatar component in action with these practical examples demonstrating different variants and styling options.

John Doe

Astro Developer

Jane Smith

Astro Designer

William Johnson

Astro Manager

DF
IN
SU
WA
ER
MT

Condensed group

JD
JS
RJ
AL
MT

Grid layout

JD

Jane

Designer

JD

John

Developer

SM

Sarah

Manager

MT

Mike

Tester