Components
Avatar
Avatar displays an image, initials, or icon for a person, entity, or profile.
Unable to parse html
const Preview = () => ( <Avatar aria-label='Ola Nordmann' variant='circle' /> ); render(<Preview />)
Usage
Avatar uses the class name ds-avatar on a <span> with role="img".Ensure the avatar has an accessible name.
CSS variables and data attributes
Sizes are controlled with data-size and colors with data-color. The component will inherit from the closest parent where these are set.
| Name | Value |
|---|---|
| data-initials | |
| data-variant | square |
| Name | Value |
|---|---|
| --dsc-avatar-background | var(--ds-color-base-default) |
| --dsc-avatar-color | var(--ds-color-base-contrast-default) |
| --dsc-avatar-font-size | 1.3em |
| --dsc-avatar-icon-url | url("data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%221em%22%20height%3D%221em%22%20fill%3D%22none%22%20viewBox%3D%220%200%2024%2024%22%3E%3Cpath%20fill%3D%22currentColor%22%20fill-rule%3D%22evenodd%22%20d%3D%22M8.25%207.5a3.75%203.75%200%201%201%207.5%200%203.75%203.75%200%200%201-7.5%200M12%202.25a5.25%205.25%200%201%200%200%2010.5%205.25%205.25%200%200%200%200-10.5M8.288%2017.288A5.25%205.25%200%200%201%2017.25%2021a.75.75%200%200%200%201.5%200%206.75%206.75%200%200%200-13.5%200%20.75.75%200%200%200%201.5%200%205.25%205.25%200%200%201%201.538-3.712%22%20clip-rule%3D%22evenodd%22%2F%3E%3C%2Fsvg%3E") |
| --dsc-avatar-padding | 0.45em |
| --dsc-avatar-radius | var(--ds-border-radius-full) |
| --dsc-avatar-size | 2.625em |
Example
Variants
Use data-variant to determine the shape of the avatar. You can choose between circle (default) or square.
Unable to parse html
const ShapeVariants = () => ( <> <Avatar variant='circle' aria-label='variant circle' /> <Avatar variant='square' aria-label='variant square' /> <Avatar variant='circle' aria-label='Ola Nordmann'> ON </Avatar> <Avatar variant='square' aria-label='Ola Nordmann'> ON </Avatar> </> ); render(<ShapeVariants />)
With image or icon
Avatar displays a user icon by default, but this can be overridden in CSS using the --dsc-avatar-icon-url variable.
If you add an image or icon as content inside the avatar, make sure to set aria-hidden="true" to avoid duplicate information. You must add an aria-label to the avatar yourself to explain who or what the avatar represents.
Unable to parse html
const WithImageAndIcon = () => ( <> <Avatar aria-label='Ola Nordmann'> <img src='/img/component-docs/cats/cat1.webp' alt='' /> </Avatar> <Avatar aria-label='Ola Nordmann'> <BriefcaseIcon /> </Avatar> </> ); render(<WithImageAndIcon />)
React
Avatar
- aria-label
- Type
string
- data-tooltip
- Type
string
- aria-hidden
- Type
Booleanish
- variant
- Description
The shape of the avatar. @deprecated Please use `style={{ '--dsc-avatar-radius': VALUE }}` instead
- Type
"circle" | "square"- Default
'circle'
- initials
- Description
Initials to display inside the avatar.
- Type
string
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior.
- Type
boolean- Default
false
- children
- Description
Image, icon or initials to display inside the avatar. Gets `aria-hidden="true"`
- Type
ReactNode
| Name | Type | Default | Description |
|---|---|---|---|
| aria-label | string | - | - |
| data-tooltip | string | - | - |
| aria-hidden | Booleanish | - | - |
| variant | "circle" | "square" | 'circle' | The shape of the avatar. @deprecated Please use `style={{ '--dsc-avatar-radius': VALUE }}` instead |
| initials | string | - | Initials to display inside the avatar. |
| asChild | boolean | false | Change the default rendered element for the one passed as a child, merging their props and behavior. |
| children | ReactNode | - | Image, icon or initials to display inside the avatar. Gets `aria-hidden="true"` |
Composition
The component should be able to be composed into other components. Here is an example of using avatar inside a dropdown component.
Unable to parse html
const InDropdown = () => ( <> <Button popovertarget='dropdown' variant='tertiary'> <Avatar aria-hidden='true' data-size='sm'> ON </Avatar> Ola Nordmann </Button> <Dropdown id='dropdown' placement='bottom-end' autoPlacement={false}> <Dropdown.List> <Dropdown.Item> <Dropdown.Button> <Badge.Position overlap='circle'> <Badge data-color='danger' data-size='sm'></Badge> <Avatar aria-hidden='true' data-size='xs'> ON </Avatar> </Badge.Position> Ola Nordmann </Dropdown.Button> </Dropdown.Item> <Dropdown.Item> <Dropdown.Button> <Avatar aria-hidden='true' data-size='xs'> <BriefcaseIcon /> </Avatar> Sogndal kommune </Dropdown.Button> </Dropdown.Item> </Dropdown.List> </Dropdown> </> ); render(<InDropdown />)
Non-text content
If you add anything other than text as a child inside <Avatar> (like an image or icon), it will automatically receive aria-hidden="true" to avoid duplicate information.