Overview
An avatar is an element with the nds-avatar class that holds one item: a photo (<img>), initials (nds-label) or an icon. It is round by default. The element can be a <div>, a link or a button. Several avatars sit together in an nds-avatar-group, spaced apart or stacked so they overlap.
Pick another component when:
- the picture sits beside a name and a role: Persona
- the icon marks a status or a type, not a person: Featured Icons
Markup
Variants
The Link, Menu trigger and Button structures take any of the three contents: a photo, initials or an icon. Each size has two rows: the first sizes a single avatar, the second a group. Make the change that fits the markup. A row on .nds-avatar:not(.nds-avatar-group > .nds-avatar) changes a single avatar only, not one inside a group. :has(> img) means the avatar holds a photo.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Icon (default) | — | — | A person icon, for an anonymous user or a system account |
| Structure | Initials | canon #avatar-initials |
— | Two letters from the person’s name, when there is no photo |
| Structure | Image | canon #avatar-image |
— | A photo of the person. The alt text names them |
| Structure | Link | canon #avatar-link |
— | An avatar that opens a profile. The image alt names the link |
| Structure | Menu trigger | canon #avatar-menu |
— | An avatar that opens the user menu, as the trigger of a Dropmenu. aria-label names the button |
| Structure | Button | canon #avatar-button |
— | An avatar that runs an action on the page, such as opening a profile panel. The image alt names the button |
| Structure | Group | canon #avatar-group |
— | Several avatars in a row, such as the members of a team. The last one counts the people it does not show |
| Size | XS | .nds-xs |
.nds-avatar:not(.nds-avatar-group > .nds-avatar) |
24px, for a line of text or a dense list |
| Size | XS | .nds-xs |
.nds-avatar-group |
The same size for every avatar in the group, 2px apart |
| Size | SM | .nds-sm |
.nds-avatar:not(.nds-avatar-group > .nds-avatar) |
32px, for a table row |
| Size | SM | .nds-sm |
.nds-avatar-group |
The same size for every avatar in the group, 4px apart |
| Size | MD | .nds-md |
.nds-avatar:not(.nds-avatar-group > .nds-avatar) |
40px, the same as no size class |
| Size | MD | .nds-md |
.nds-avatar-group |
The same size for every avatar in the group, 6px apart |
| Size | LG | .nds-lg |
.nds-avatar:not(.nds-avatar-group > .nds-avatar) |
48px, for a card |
| Size | LG | .nds-lg |
.nds-avatar-group |
The same size for every avatar in the group, 8px apart |
| Size | XL (default) | .nds-xl |
.nds-avatar:not(.nds-avatar-group > .nds-avatar) |
64px, for a card header |
| Size | XL (default) | .nds-xl |
.nds-avatar-group |
The same size for every avatar in the group, 10px apart |
| Size | 2XL | .nds-2xl |
.nds-avatar:not(.nds-avatar-group > .nds-avatar) |
80px, for a profile card |
| Size | 2XL | .nds-2xl |
.nds-avatar-group |
The same size for every avatar in the group, 12px apart |
| Size | 3XL | .nds-3xl |
.nds-avatar:not(.nds-avatar-group > .nds-avatar) |
120px with a 4px ring, for a profile header |
| Size | 3XL | .nds-3xl |
.nds-avatar-group |
The same size and ring for every avatar in the group, 14px apart |
| Square | Square | .nds-square |
.nds-avatar |
Rounded corners in place of a circle, for an organization or a service account. In a group, give it to every avatar |
| Image border | Image border (hint: A thin dark line inside the photo edge) | .nds-image-border |
.nds-avatar:has(> img) |
A thin dark line inside the edge of the photo, so a light photo stays apart from a light page |
| Stacked | Stacked (default) (hint: The avatars overlap) | .nds-stacked |
.nds-avatar-group |
The avatars overlap. Leave it out to space them apart by the group gap. Each stacked avatar has a ring in the border color. The ring is 1px at XS and SM, 2px at MD and LG, and 4px at XL and larger. At 3XL it replaces the border, so the photo keeps its full size |
Built-in Features
-
Pure CSS
Avatars need no JavaScript. They show at the first paint, from the markup alone.
-
Seven Sizes
From 24px (XS) to 120px (3XL). The initials are a third of the size and the icon is half, so both scale with it.
-
Any Element
The same class works on a
<div>, a link or a button. A link avatar keeps the avatar colors, not the link color. -
Group Layout
A size class on the group sets the size of every avatar in it, and the gap grows with the size.
-
High Contrast Ring
In high contrast mode, every avatar gets a solid ring inside its edge, so a photo stays apart from the page.
-
Broken Photo
When a photo does not load, the avatar shows the person icon in place of the broken-image mark and the
alttext. Screen readers still read thealttext. Safari shows its own small broken-image mark and no text.
Best Practices
- Show a photo when one is available. When there is none, show two letters from the person’s name.
- Use an icon avatar for an anonymous user or a system account, where no one is named.
- Write
alttext that names the person. Usealt=""only when the name shows in text next to the avatar. - Give a link or button avatar a name. A photo names it through its
alt. An avatar with initials or an icon needsaria-label, such asaria-label="Open the user menu". - Pick the size by place: XS or SM in a line of text or a table row, XL in a card, 3XL in a profile header.
- In a List, leave the size class off. The list makes the avatar XL, a small list (
nds-sm) LG, and a large list (nds-lg) 2XL. - In a group, set the size on
nds-avatar-group. The group size replaces a size class on an avatar inside it. - Use a group to show the members of a team or the people on a task in little space.
- End a long group with an initials avatar that counts the people it does not show, such as
+9.
API
CSS Custom Properties
| Property | Default | Controls |
|---|---|---|
--avatar-size |
40px |
Width and height. It wins over the size classes. Set it on the avatar, or on a parent to size every avatar inside |
--avatar-gap |
6px |
Space between the avatars of a group. A stacked group overlaps them by twice this value. Set it on the group |
Tokens
Set a token at :root, or on a wrapper to reach every avatar inside it. Give a token with a Dark mode value a dark override too: see Tokens. The border is the ring of a 3XL avatar and of each avatar in a stacked group.
Source: the avatar group in _sass/tokens/_components.scss.
| Token | Preview | Value |
|---|---|---|
--avatar-background | var(--colors-neutral-100)Dark mode var(--colors-neutral-700) | |
--avatar-color | var(--icon-default)Dark mode var(--colors-neutral-300) | |
--avatar-border | var(--colors-base-white)Dark mode var(--colors-neutral-800) |