Avatar
A round picture of someone, over their initials.
CN
ERavatar-demo.tsx
import { Avatar, AvatarFallback, AvatarImage } from './ui/avatar.tsx'
export default () => (
<>
<Avatar>
<AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" />
<AvatarFallback>CN</AvatarFallback>
</Avatar>
<Avatar>
<AvatarImage src="/missing.png" alt="" />
<AvatarFallback>ER</AvatarFallback>
</Avatar>
</>
)
Installation
dui add avatarOr copy ui/avatar.tsx and ui/avatar.css into your project, with lib/jsx/jsx-runtime.ts, lib/jsx/jsx-dev-runtime.ts, lib/css.d.ts, and lib/theme.css.
Examples
Group
SH
LE
EVavatar-group.tsx
import { Avatar, AvatarFallback, AvatarImage } from './ui/avatar.tsx'
export default () => (
<div style="display: flex">
{['shadcn', 'leerob', 'evilrabbit'].map((user, i) => (
<Avatar style={`margin-left: ${i ? '-0.5rem' : '0'}; outline: 2px solid var(--background)`}>
<AvatarImage src={`https://github.com/${user}.png`} alt={`@${user}`} />
<AvatarFallback>{user.slice(0, 2).toUpperCase()}</AvatarFallback>
</Avatar>
))}
</div>
)
Sizes
SMMDLG
avatar-sizes.tsx
import { Avatar, AvatarFallback } from './ui/avatar.tsx'
export default () => (
<>
<Avatar style="width: 1.5rem; height: 1.5rem">
<AvatarFallback style="font-size: 0.625rem">SM</AvatarFallback>
</Avatar>
<Avatar>
<AvatarFallback>MD</AvatarFallback>
</Avatar>
<Avatar style="width: 3rem; height: 3rem">
<AvatarFallback>LG</AvatarFallback>
</Avatar>
</>
)
Compared to shadcn/ui
- Radix shows the fallback until the image loads, with JavaScript. dui stacks the image on top of the fallback in one grid cell, and hides a broken image's alt text, so the fallback shows through.
- No
delayMs: the fallback is there from the start, under the image. - Resize it with
styleor a class, as with shadcn'ssize-*.