Skip to content

CdsAvatar

New in v4.0

An avatar shows a person, a team or a company: a photo, initials or an icon inside a round container. Use it in member lists, comments and profile headers — anywhere the reader needs to recognize "who is this" at a glance.

Storybook GitLab
Test coverage: 100% — good coverage
API in specs
17 / 17 props, events, slots and exposed members with a test
Test cases
25 unit · 0 browser · 2 planned
Known bugs
2 pinned by an it.fails test
Code coverage
100% lines · 100% branches measured in v4.0.0-alpha.13
A11y (axe)
8 recommendations in 10 stories measured in v4.0.0-alpha.11

The built-in badge shows a status or a counter right on the avatar; for a marker on arbitrary content take CdsBadge, and for a text label inside a line — CdsTag.

Anatomy ​

An avatar is a container with one of three kinds of content and an optional badge in the top corner.

Variations ​

Content ​

The content comes from one of three props: image — a photo, icon — an icon, text — initials. If you pass several of them, image wins, then icon.

Sizes ​

The size prop takes a token from xs to xl or a number of pixels. The default size is md.

Colors ​

The color prop paints the background of an avatar with initials or an icon: a theme token or any CSS color will do.

Badge ​

The badge prop set to true shows a dot in the corner of the avatar. An object is passed through as props of CdsBadge: this is how you change the color of the dot, show a counter (dot: false plus content) or move the badge away from the edge (floating). The badge slot replaces the content of the badge with anything you like — an icon, for example.

Spacing ​

The start and end props add a margin on the side of the neighboring content: start — for an avatar at the beginning of a line, end — at the end. This way the avatar fits into text without wrappers that carry outer margins.

Slots ​

SlotDescription
defaultAvatar content. Replaces image, icon and text
badgeBadge content. Shown instead of the dot when badge is on

Guidelines ​

Scenarios ​

Member list ​

In a list of people the avatar is a fast visual anchor. The start prop gives the margin before the text, photos and initials alternate depending on what the user has, and the badge dot shows who is online right now.

Group of members ​

When there are many members and little space, avatars are stacked with a slight overlap — a border in the background color separates the neighbors from each other. The last avatar with text="+3" tells how many people did not fit.

Uploading a profile photo ​

While there is no photo yet, the avatar shows the initials from text — the page is not left with an empty circle. As soon as image gets a link, the photo replaces the initials; after the photo is removed, the initials come back on their own.

API ​

Props

NameTypeDefaultDescription
activeColorstring | undefined—Color of the component in its active state
badgeboolean | CdsBadgeProps | undefinedfalseAvatar badge. true shows a dot, an object is passed through as CdsBadge props.
baseColorstring | undefined—Color of the component in its inactive state
bgColorstring | undefined—Background color of the component
colorstring | undefined—Component color
contentstring | number | undefined—Content displayed in the component.
endboolean | undefined—Adds a margin on the left — for an avatar at the end of a line of content.
iconIconValue | undefined—Icon shown in the avatar when no image is set.
imagestring | undefined—Source of the avatar image. Takes priority over icon and text.
sizenumber | "xs" | "sm" | "md" | "lg" | "xl" | undefined'md'Size of the component
startboolean | undefined—Adds a margin on the right — for an avatar at the beginning of a line of content.
tagstring | JSXComponent<any> | undefined'div'Custom HTML tag or component to render
textstring | number | undefined—Text displayed in the component.
textColorstring | undefined—Text color of the component
themestring | undefined—Component theme

Slots

NameTypeDescription
badgeanyContent of the avatar badge.
defaultanyAvatar content. Overrides image, icon and text.