Components
Avatar
Avatar displays an image, initials, or icon for a person, entity, or profile.
<span aria-label="Ola Nordmann" class="ds-avatar" role="img"></span>const Preview = () => <Avatar aria-label='Ola Nordmann' />; 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.
No relevant data attributes found.
| Name | Value |
|---|---|
| --dsc-avatar-background | var(--ds-color-base-default) |
| --dsc-avatar-color | var(--ds-color-base-contrast-default) |
| --dsc-avatar-font-size | calc(var(--dsc-avatar-size) * .4) |
| --dsc-avatar-icon-url | url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' fill='none' viewBox='0 0 24 24' focusable='false' role='img'%3E%3Cpath fill='currentColor' fill-rule='evenodd' d='M8.25 7.5a3.75 3.75 0 1 1 7.5 0 3.75 3.75 0 0 1-7.5 0M12 2.25a5.25 5.25 0 1 0 0 10.5 5.25 5.25 0 0 0 0-10.5M8.288 17.288A5.25 5.25 0 0 1 17.25 21a.75.75 0 0 0 1.5 0 6.75 6.75 0 0 0-13.5 0 .75.75 0 0 0 1.5 0 5.25 5.25 0 0 1 1.538-3.712' clip-rule='evenodd'%3E%3C/path%3E%3C/svg%3E") |
| --dsc-avatar-padding | var(--ds-size-2) |
| --dsc-avatar-radius | var(--ds-border-radius-full) |
| --dsc-avatar-size | var(--ds-size-12) |
Example
With initials
Place text inside avatar to show initials.
<span aria-label="Ola Nordmann" class="ds-avatar" role="img">ON</span>const Initials = () => <Avatar aria-label='Ola Nordmann'>ON</Avatar>; render(<Initials />)
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.
<link
rel="preload"
as="image"
href="/img/component-docs/cats/cat1.webp"
/>
<span aria-label="Ola Nordmann" class="ds-avatar" role="img">
<img src="/img/component-docs/cats/cat1.webp" alt=""/>
</span>
<span aria-label="Ola Nordmann" class="ds-avatar" role="img">
<svg role="img"></svg>
</span>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 />)
Shape and radius
Use --dsc-avatar-radius CSS custom property to adjust the radius of the avatar.
<span aria-label="radius default" class="ds-avatar" role="img"></span>
<span
aria-label="radius 1rem"
class="ds-avatar"
role="img"
style="--dsc-avatar-radius:1rem"
></span>
<span
aria-label="radius lg"
class="ds-avatar"
role="img"
style="--dsc-avatar-radius:var(--ds-border-radius-lg)"
>ON</span>
<span
aria-label="radius none"
class="ds-avatar"
role="img"
style="--dsc-avatar-radius:0"
>ON</span>const Radius = () => ( <> <Avatar aria-label='radius default' /> <Avatar aria-label='radius 1rem' style={ { '--dsc-avatar-radius': '1rem', } as React.CSSProperties } /> <Avatar aria-label='radius lg' style={ { '--dsc-avatar-radius': 'var(--ds-border-radius-lg)', } as React.CSSProperties } > ON </Avatar> <Avatar aria-label='radius none' style={ { '--dsc-avatar-radius': '0', } as React.CSSProperties } > ON </Avatar> </> ); render(<Radius />)
React
/* With default user icon */
<Avatar aria-label="Ola Nordmann" />
/* With initials */
<Avatar aria-label="Ola Nordmann">ON</Avatar>
/* With image */
<Avatar aria-label="Ola Nordmann">
<img src="..." />
</Avatar>
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. @deprecated Please use text content as children instead
- Type
string
- data-initials
- Description
Initials to display inside the avatar. @deprecated Please use text content as children instead
- 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.
- 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. @deprecated Please use text content as children instead |
| data-initials | string | - | Initials to display inside the avatar. @deprecated Please use text content as children instead |
| 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. |
Composition
The component should be able to be composed into other components. Here is an example of using avatar inside a dropdown component.
<button
class="ds-button"
data-variant="tertiary"
type="button"
popoverTarget="dropdown"
>
<span
class="ds-avatar"
role="img"
aria-hidden="true"
data-size="sm"
>ON</span>
Ola Nordmann
</button>
<div
class="ds-dropdown"
id="dropdown"
popover="manual"
data-placement="bottom-end"
>
<ul>
<li>
<button
class="ds-button"
data-variant="tertiary"
type="button"
>
<span
class="ds-badge--position"
data-overlap="circle"
data-placement="top-right"
>
<span class="ds-badge" data-color="danger" data-size="sm"></span>
<span
class="ds-avatar"
role="img"
aria-hidden="true"
data-size="xs"
>ON</span>
</span>
Ola Nordmann
</button>
</li>
<li>
<button
class="ds-button"
data-variant="tertiary"
type="button"
>
<span
class="ds-avatar"
role="img"
aria-hidden="true"
data-size="xs"
>
<svg role="img"></svg>
</span>
Sogndal kommune
</button>
</li>
</ul>
</div>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 />)