Avatar
A face, initials, or a fallback when the image is missing.
OS
Source
<script lang="ts">
import 'portal-bits/styles.css';
import 'portal-bits/tokens.css';
import type { Snippet } from 'svelte';
let { children }: { children: Snippet } = $props();
</script>
{@render children()}
<script lang="ts">
import AvatarExample from './AvatarExample.svelte';
</script>
<AvatarExample size={40} media={"image"} />
<script lang="ts">
import { Avatar } from 'portal-bits';
let { size = 40, media = 'image' }: { size?: 32 | 40 | 56; media?: 'image' | 'broken' | 'none' } =
$props();
// A synthetic poster as inline SVG data. Pass your own image URL as `src` instead.
const poster = `data:image/svg+xml,${encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" width="160" height="160"><rect width="160" height="160" fill="#c5d4f6"/><circle cx="80" cy="80" r="44" fill="none" stroke="#263864" stroke-width="4"/></svg>',
)}`;
const sources = {
image: poster,
// A path your app does not serve, so the fallback initials appear.
broken: '/missing-avatar.png',
none: undefined,
};
</script>
{#key media}
<Avatar alt="Orbit Studio" src={sources[media]} {size} />
{/key}
API reference
These tables list the props and snippets that Avatar declares. Attributes passed through to an element or primitive are listed under Forwarded attributes, not here.
Props
| Name | Type | Default | Required | Binding | Description |
|---|---|---|---|---|---|
src | string | None | No | One-way | The image URL. When it is missing or fails, the fallback shows. |
alt | string | None | Yes | One-way | The accessible name. Also the source of the default initials. |
fallback | string | None | No | One-way | Text for the fallback. Without it, the first letters of the first two words of `alt`. |
size | number | 40 | No | One-way | Width and height in pixels. |
decorative | boolean | false | No | One-way | Removes the avatar from the accessibility tree. Use it when nearby text names the same thing. |
Forwarded attributes
None. Avatar forwards no attributes.
The primitive underneath is a Bits UI peer dependency. Its own options are documented in the Bits UI Avatar documentation. Only the props listed on this page are part of the Avatar contract. No data attributes are documented as stable.
Limits
- The component forwards no attributes, so it takes no `class`. Size it with `size`.
- Initials come from `alt` unless you supply `fallback`. Names that begin with a symbol give odd initials.