2020-07-15 21:30:37 +00:00
# Avatar
[component-header:sl-avatar]
Avatars are used to represent a person or object.
2021-11-12 22:40:26 +00:00
Like [images ](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img ), you should always provide `alt` text for avatars as alternate text for assistive devices.
2020-07-15 21:30:37 +00:00
```html preview
2021-12-31 16:18:44 +00:00
< sl-avatar label = "User avatar" > < / sl-avatar >
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlAvatar } from '@shoelace-style/shoelace/dist/react';
2022-03-02 15:10:41 +00:00
const App = () => < SlAvatar label = "User avatar" / > ;
2021-11-04 22:12:47 +00:00
```
2020-07-15 21:30:37 +00:00
## Examples
### Images
To use an image for the avatar, set the `image` and `alt` attributes. This will take priority and be shown over initials and icons.
```html preview
< sl-avatar
image="https://images.unsplash.com/photo-1529778873920-4da4926a72c2?ixlib=rb-1.2.1& auto=format& fit=crop& w=300& q=80"
2021-12-31 16:18:44 +00:00
label="Avatar of a gray tabby kitten looking down"
2020-07-15 21:30:37 +00:00
>< / sl-avatar >
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlAvatar } from '@shoelace-style/shoelace/dist/react';
const App = () => (
< SlAvatar
image="https://images.unsplash.com/photo-1529778873920-4da4926a72c2?ixlib=rb-1.2.1& auto=format& fit=crop& w=300& q=80"
2021-12-31 16:18:44 +00:00
label="Avatar of a gray tabby kitten looking down"
2021-11-04 22:12:47 +00:00
/>
);
```
2020-07-15 21:30:37 +00:00
### Initials
When you don't have an image to use, you can set the `initials` attribute to show something more personalized than an icon.
```html preview
2021-12-31 16:18:44 +00:00
< sl-avatar initials = "SL" label = "Avatar with initials: SL" > < / sl-avatar >
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlAvatar } from '@shoelace-style/shoelace/dist/react';
2022-03-02 15:10:41 +00:00
const App = () => < SlAvatar initials = "SL" label = "Avatar with initials: SL" / > ;
2021-11-04 22:12:47 +00:00
```
2020-07-15 21:30:37 +00:00
### Custom Icons
When no image or initials are set, an icon will be shown. The default avatar shows a generic "user" icon, but you can customize this with the `icon` slot.
```html preview
2021-12-31 16:18:44 +00:00
< sl-avatar label = "Avatar with an image icon" >
2020-07-15 21:30:37 +00:00
< sl-icon slot = "icon" name = "image" > < / sl-icon >
< / sl-avatar >
2021-12-31 16:18:44 +00:00
< sl-avatar label = "Avatar with an archive icon" >
2020-07-15 21:30:37 +00:00
< sl-icon slot = "icon" name = "archive" > < / sl-icon >
< / sl-avatar >
2021-12-31 16:18:44 +00:00
< sl-avatar label = "Avatar with a briefcase icon" >
2020-07-15 21:30:37 +00:00
< sl-icon slot = "icon" name = "briefcase" > < / sl-icon >
< / sl-avatar >
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlAvatar, SlIcon } from '@shoelace-style/shoelace/dist/react';
const App = () => (
< >
2021-12-31 16:18:44 +00:00
< SlAvatar label = "Avatar with an image icon" >
2021-11-04 22:12:47 +00:00
< SlIcon slot = "icon" name = "image" / >
< / SlAvatar >
2021-12-31 16:18:44 +00:00
< SlAvatar label = "Avatar with an archive icon" >
2021-11-04 22:12:47 +00:00
< SlIcon slot = "icon" name = "archive" / >
< / SlAvatar >
2021-12-31 16:18:44 +00:00
< SlAvatar label = "Avatar with a briefcase icon" >
2021-11-04 22:12:47 +00:00
< SlIcon slot = "icon" name = "briefcase" / >
< / SlAvatar >
< />
);
```
2020-07-15 21:30:37 +00:00
### Shapes
Avatars can be shaped using the `shape` attribute.
```html preview
2021-12-31 16:18:44 +00:00
< sl-avatar shape = "square" label = "Square avatar" > < / sl-avatar >
< sl-avatar shape = "rounded" label = "Rounded avatar" > < / sl-avatar >
< sl-avatar shape = "circle" label = "Circle avatar" > < / sl-avatar >
2020-07-15 21:30:37 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlAvatar, SlIcon } from '@shoelace-style/shoelace/dist/react';
const App = () => (
< >
2021-12-31 16:18:44 +00:00
< SlAvatar shape = "square" label = "Square avatar" / >
< SlAvatar shape = "rounded" label = "Rounded avatar" / >
< SlAvatar shape = "circle" label = "Circle avatar" / >
2021-11-04 22:12:47 +00:00
< />
);
```
2021-01-06 12:57:46 +00:00
### Avatar Groups
You can group avatars with a few lines of CSS.
```html preview
< div class = "avatar-group" >
2022-03-02 15:10:41 +00:00
< sl-avatar
2021-11-12 22:40:26 +00:00
image="https://images.unsplash.com/photo-1490150028299-bf57d78394e0?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D& ixlib=rb-1.2.1& auto=format& fit=crop& w=256& h=256& q=80& crop=right"
2021-12-31 16:18:44 +00:00
label="Avatar 1 of 4"
2021-11-12 22:40:26 +00:00
>< / sl-avatar >
2022-03-02 15:10:41 +00:00
< sl-avatar
2021-11-12 22:40:26 +00:00
image="https://images.unsplash.com/photo-1503454537195-1dcabb73ffb9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D& ixlib=rb-1.2.1& auto=format& fit=crop& w=256& h=256& crop=left& q=80"
2021-12-31 16:18:44 +00:00
label="Avatar 2 of 4"
2021-11-12 22:40:26 +00:00
>< / sl-avatar >
2022-03-02 15:10:41 +00:00
< sl-avatar
2021-11-12 22:40:26 +00:00
image="https://images.unsplash.com/photo-1456439663599-95b042d50252?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D& ixlib=rb-1.2.1& auto=format& fit=crop& w=256& h=256& crop=left& q=80"
2021-12-31 16:18:44 +00:00
label="Avatar 3 of 4"
2021-11-12 22:40:26 +00:00
>< / sl-avatar >
2022-03-02 15:10:41 +00:00
< sl-avatar
2021-11-12 22:40:26 +00:00
image="https://images.unsplash.com/flagged/photo-1554078875-e37cb8b0e27d?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D& ixlib=rb-1.2.1& auto=format& fit=crop& w=256& h=256& crop=top& q=80"
2021-12-31 16:18:44 +00:00
label="Avatar 4 of 4"
2021-11-12 22:40:26 +00:00
>< / sl-avatar >
2021-01-06 12:57:46 +00:00
< / div >
< style >
.avatar-group sl-avatar:not(:first-of-type) {
margin-left: -1rem;
}
.avatar-group sl-avatar::part(base) {
2021-11-18 22:41:03 +00:00
border: solid 2px var(--sl-color-neutral-0);
2021-01-06 12:57:46 +00:00
}
< / style >
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlAvatar, SlIcon } from '@shoelace-style/shoelace/dist/react';
const css = `
.avatar-group sl-avatar:not(:first-of-type) {
margin-left: -1rem;
}
.avatar-group sl-avatar::part(base) {
2021-11-18 22:41:03 +00:00
border: solid 2px var(--sl-color-neutral-0);
2021-11-04 22:12:47 +00:00
}
`;
const App = () => (
< >
2021-11-05 14:06:06 +00:00
< div className = "avatar-group" >
2022-03-02 15:10:41 +00:00
< SlAvatar
image="https://images.unsplash.com/photo-1490150028299-bf57d78394e0?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D& ixlib=rb-1.2.1& auto=format& fit=crop& w=256& h=256& q=80& crop=right"
label="Avatar 1 of 4"
2021-11-12 22:40:26 +00:00
/>
2022-03-02 15:10:41 +00:00
< SlAvatar
image="https://images.unsplash.com/photo-1503454537195-1dcabb73ffb9?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D& ixlib=rb-1.2.1& auto=format& fit=crop& w=256& h=256& crop=left& q=80"
label="Avatar 2 of 4"
2021-11-12 22:40:26 +00:00
/>
2022-03-02 15:10:41 +00:00
< SlAvatar
image="https://images.unsplash.com/photo-1456439663599-95b042d50252?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D& ixlib=rb-1.2.1& auto=format& fit=crop& w=256& h=256& crop=left& q=80"
label="Avatar 3 of 4"
2021-11-12 22:40:26 +00:00
/>
2022-03-02 15:10:41 +00:00
< SlAvatar
image="https://images.unsplash.com/flagged/photo-1554078875-e37cb8b0e27d?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D& ixlib=rb-1.2.1& auto=format& fit=crop& w=256& h=256& crop=top& q=80"
label="Avatar 4 of 4"
2021-11-12 22:40:26 +00:00
/>
2021-11-04 22:12:47 +00:00
< / div >
2022-03-02 15:10:41 +00:00
< style > { c s s } < / style >
2021-11-04 22:12:47 +00:00
< />
);
```
2020-07-15 21:30:37 +00:00
[component-metadata:sl-avatar]