Skip to content
Likable Aurora @likable-hair/svelte 5

Avatar

Round picture of a person or a thing. With src it shows the image; until the image arrives, and when it cannot load, it shows the initials of name (first letter of the first and of the last word: "Mario Rossi" → "MR"), or text when given, or an icon when there is neither. The background is one of six gradients picked from name (or colorKey), so the same person always gets the same color; color fixes one. name is also the accessible name (alt changes it, alt="" makes the avatar decorative). src can also be a Blob or File, for images downloaded with authentication: the component creates the object URL and revokes it. status adds a dot in the bottom-right corner, ring a highlight ring. Its state is exposed as data-size, data-shape, data-color, data-status and data-image (loading, success, error).

import { Avatar } from '@likable-hair/svelte';

src/lib/components/simple/media/Avatar.svelte · Markdown for LLMs

Initials, images and icon

With name the avatar shows the initials of the first and last word and picks a color from the name, so the same person always has the same color. src shows an image over the initials, which stay visible while it loads or when it fails. text replaces the initials; with neither the avatar shows an icon.

MR GM SB L EC ACM
Code
<script lang="ts">
	import { Avatar } from '@likable-hair/svelte';

	const photo = `data:image/svg+xml,${encodeURIComponent(
		'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><defs><linearGradient id="g" x2="1" y2="1"><stop offset="0" stop-color="#ffb547"/><stop offset="1" stop-color="#ff4d6d"/></linearGradient></defs><rect width="64" height="64" fill="url(#g)"/><circle cx="32" cy="26" r="11" fill="#fff" opacity=".85"/><ellipse cx="32" cy="56" rx="20" ry="14" fill="#fff" opacity=".85"/></svg>'
	)}`;
</script>

<div class="row">
	<Avatar name="Mario Rossi" />
	<Avatar name="Giulia Marini" />
	<Avatar name="Sara Bianchi" />
	<Avatar name="Luca" />
	<Avatar name="Elena Conti" src={photo} />
	<Avatar name="Acme S.p.A." text="ACM" />
	<Avatar />
</div>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 12px;
	}
</style>

Sizes

size goes from xs (24px) to xl (80px); the initials and the icon scale with it. Any other size with --avatar-size.

GM GM GM GM GM GM
Code
<script lang="ts">
	import { Avatar } from '@likable-hair/svelte';

	const sizes = ['xs', 'sm', 'md', 'lg', 'xl'] as const;
</script>

<div class="demo">
	<div class="row">
		{#each sizes as size (size)}
			<Avatar name="Giulia Marini" {size} />
		{/each}
		<Avatar name="Giulia Marini" --avatar-size="100px" />
	</div>
	<div class="row">
		{#each sizes as size (size)}
			<Avatar {size} />
		{/each}
	</div>
</div>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 12px;
	}

	.demo {
		display: flex;
		flex-direction: column;
		gap: 16px;
	}
</style>

Colors

By default the color is one of six gradients picked from the name; colorKey picks it from another value, such as an id, so it does not change with the name. color fixes a gradient (1 to 6), the brand one (primary) or grey (neutral). The gradients come from the theme's --global-color-data-* tokens, and --avatar-background and --avatar-color change a single avatar.

MR GM SB LF AG PR CC MG
MR MR MR MR MR MR MR MR MR
Code
<script lang="ts">
	import { Avatar } from '@likable-hair/svelte';

	const people = ['Mario Rossi', 'Giulia Marini', 'Sara Bianchi', 'Luca Ferri', 'Anna Greco', 'Paolo Russo', 'Chiara Costa', 'Marco Gallo'];
</script>

<div class="demo">
	<div class="row">
		{#each people as name (name)}
			<Avatar {name} />
		{/each}
	</div>
	<div class="row">
		{#each [1, 2, 3, 4, 5, 6] as const as color (color)}
			<Avatar name="Mario Rossi" {color} />
		{/each}
		<Avatar name="Mario Rossi" color="primary" />
		<Avatar name="Mario Rossi" color="neutral" />
		<Avatar name="Mario Rossi" --avatar-background="#111827" --avatar-color="#facc15" />
	</div>
</div>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 12px;
	}

	.demo {
		display: flex;
		flex-direction: column;
		gap: 16px;
	}
</style>

Status, ring and shape

status adds a dot in the bottom-right corner (online, away, busy, offline) and its meaning to the accessible name. ring highlights an avatar, for example the current user. shape="square" rounds the corners instead of drawing a circle. The thin gap around the dot and the ring takes --avatar-gap-color: set it to the background behind the avatar.

MR GM SB LF GM AC AC
Code
<script lang="ts">
	import { Avatar } from '@likable-hair/svelte';
</script>

<div class="row">
	<Avatar name="Mario Rossi" status="online" />
	<Avatar name="Giulia Marini" status="away" />
	<Avatar name="Sara Bianchi" status="busy" />
	<Avatar name="Luca Ferri" status="offline" />
	<Avatar name="Giulia Marini" size="lg" ring status="online" />
	<Avatar name="Acme S.p.A." text="AC" shape="square" />
	<Avatar name="Acme S.p.A." text="AC" shape="square" size="lg" status="online" />
</div>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 16px;
	}
</style>

Loading and protected images

An image that cannot load leaves the initials in place; bind:imageLoadingStatus tells loading, success or error. Images downloaded with authentication can be passed as a Blob or File: the avatar creates the object URL and revokes it when it is no longer needed.

MR Image status:
SB From a Blob
Code
<script lang="ts">
	import { Avatar } from '@likable-hair/svelte';

	let status = $state<'loading' | 'success' | 'error'>();

	const blob = new Blob(
		[
			'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect width="64" height="64" fill="#2fb6c9"/><circle cx="32" cy="26" r="11" fill="#fff" opacity=".85"/><ellipse cx="32" cy="56" rx="20" ry="14" fill="#fff" opacity=".85"/></svg>'
		],
		{ type: 'image/svg+xml' }
	);
</script>

<div class="demo">
	<div class="row">
		<Avatar name="Mario Rossi" src="/missing-photo.jpg" bind:imageLoadingStatus={status} />
		<span>Image status: {status}</span>
	</div>
	<div class="row">
		<Avatar name="Sara Bianchi" src={blob} />
		<span>From a Blob</span>
	</div>
</div>

<style>
	.row {
		display: flex;
		align-items: center;
		gap: 12px;
	}

	.demo {
		display: flex;
		flex-direction: column;
		gap: 16px;
	}
</style>

Custom content

children replaces the initials inside the circle, for example with an icon, and keeps the color and the image; icon changes the icon shown when there is no name. badgeSnippet puts anything in the bottom-right corner in place of the status dot.

GM
Code
<script lang="ts">
	import { Avatar, Icon } from '@likable-hair/svelte';
	import { mdiCurrencyEur, mdiDomain, mdiCheckDecagram } from '@mdi/js';
</script>

<div class="row">
	<Avatar name="Loan request" color={4}>
		<Icon path={mdiCurrencyEur} />
	</Avatar>
	<Avatar icon={mdiDomain} />
	<Avatar name="Giulia Marini" size="lg">
		{#snippet badgeSnippet()}
			<span class="verified"><Icon path={mdiCheckDecagram} /></span>
		{/snippet}
	</Avatar>
</div>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: 16px;
	}

	.verified {
		display: grid;
		place-items: center;
		border-radius: 50%;
		background: var(--global-color-bg);
		--icon-size: 18px;
		--icon-color: var(--global-color-primary);
		transform: translate(4px, 4px);
	}
</style>

Props

NameTypeDefaultDescription
src string | BlobImage: a URL, or a Blob/File (for images downloaded with authentication), turned into an object URL that is revoked when it changes or the avatar goes away. An empty string counts as no image.
name stringFull name of the person or thing. It gives the initials, the color and the accessible name.
text stringText shown instead of the initials of name, for example a short company name.
alt stringAccessible name of the avatar. Defaults to name; an empty string makes the avatar decorative, as it should be when the name is written next to it.
colorKey string | numberValue that picks the color instead of name, for example the id of the person, so the color does not change when the name does.
color 'auto' | 'primary' | 'neutral' | 1 | 2 | 3 | 4 | 5 | 6'auto'Background: auto one of six gradients picked from colorKey or name (neutral when there is neither), 1 to 6 a fixed one, primary the brand gradient, neutral grey.
size 'xs' | 'sm' | 'md' | 'lg' | 'xl''md'Size: xs 24px, sm 32px, md 40px, lg 56px, xl 80px. Any other size with --avatar-size.
shape 'circle' | 'square''circle'circle or square with rounded corners.
ring booleanfalseDraws a ring around the avatar, for example for the current user or a selected person.
status 'online' | 'away' | 'busy' | 'offline'Shows a status dot in the bottom-right corner; its meaning is added to the accessible name.
statusLabel stringAccessible text of the status. Defaults to "Online", "Away", "Busy" or "Offline", in the language of locale.
icon stringSVG path of the icon shown when there is no image, name or text, for example from @mdi/js. Defaults to a person.
referrerpolicy ReferrerPolicy'no-referrer'Native referrerpolicy of the image. no-referrer lets profile pictures from Google load.
imageLoadingStatus bindable 'loading' | 'success' | 'error'State of the image: loading until it arrives, then success or error; undefined without src. Bind it to react to a broken image.
locale stringBCP 47 locale of the texts (it and en are built in, other languages fall back to English). Defaults to the one set with setDefaultLocale, else en.
avatarElement bindable HTMLSpanElementThe root <span> element.
class string''Extra classes on the root <span> element.

It also accepts the native attributes and events of <span> (for example id, title, aria-label, onkeydown), which are forwarded to the element.

Snippets

NameParametersDescription
childrenReplaces the initials or the icon inside the avatar, under the image.
badgeSnippetReplaces the status dot in the bottom-right corner, for example with a count or a small icon.

CSS variables

For a single instance, pass them as style props: <Avatar --avatar-background="tomato">. The --avatar-default-* defaults apply to the whole app: override them in :root or in a theme.

Per-instance variableDefault
--avatar-gap-width0px
--avatar-size
--avatar-default-size 40px
--avatar-default-xs-size 24px
--avatar-default-sm-size 32px
--avatar-default-lg-size 56px
--avatar-default-xl-size 80px
--avatar-icon-scale
--avatar-default-icon-scale 0.6
--avatar-bordernone
--avatar-border-radius
--avatar-default-border-radius 50%
--avatar-default-square-border-radius 30%
--avatar-background
--avatar-default-primary-background var(--global-fill-gradient)
--avatar-default-neutral-background color-mix(in oklab, var(--global-color-surface-solid), var(--global-color-text) 12%)
--avatar-color
--avatar-default-color light-dark(var(--global-color-on-primary), var(--global-color-bg))
--avatar-default-primary-color var(--global-color-on-primary)
--avatar-default-neutral-color var(--global-color-text-2)
--avatar-gap-color
--avatar-default-gap-color var(--global-color-bg)
--avatar-ring-color
--avatar-default-ring-color var(--global-color-primary)
--avatar-font-size
--avatar-font-weight
--avatar-default-font-weight 600
--avatar-ring-width
--avatar-default-ring-width 2px
--avatar-status-size
--avatar-default-status-size 28%
--avatar-status-border-width
--avatar-default-status-border-width 2px
--avatar-status-color
--avatar-default-online-status-color var(--global-color-success)
--avatar-default-away-status-color var(--global-color-warning)
--avatar-default-busy-status-color var(--global-color-error)
--avatar-default-offline-status-color var(--global-color-text-3)

Other defaults

VariableValue
--avatar-default-font-scale0.38
--avatar-default-background-1linear-gradient(135deg, light-dark(color-mix(in oklab, var(--global-color-data-1), black 14%), color-mix(in oklab, var(--global-color-data-1), white 22%)), light-dark(color-mix(in oklab, color-mix(in oklab, var(--global-color-data-1), var(--global-color-data-3) 55%), black 14%), color-mix(in oklab, color-mix(in oklab, var(--global-color-data-1), var(--global-color-data-3) 55%), white 22%)))
--avatar-default-background-2linear-gradient(135deg, light-dark(color-mix(in oklab, var(--global-color-data-2), black 14%), color-mix(in oklab, var(--global-color-data-2), white 22%)), light-dark(color-mix(in oklab, color-mix(in oklab, var(--global-color-data-2), var(--global-color-data-1) 55%), black 14%), color-mix(in oklab, color-mix(in oklab, var(--global-color-data-2), var(--global-color-data-1) 55%), white 22%)))
--avatar-default-background-3linear-gradient(135deg, light-dark(color-mix(in oklab, var(--global-color-data-3), black 14%), color-mix(in oklab, var(--global-color-data-3), white 22%)), light-dark(color-mix(in oklab, color-mix(in oklab, var(--global-color-data-3), var(--global-color-data-6) 50%), black 14%), color-mix(in oklab, color-mix(in oklab, var(--global-color-data-3), var(--global-color-data-6) 50%), white 22%)))
--avatar-default-background-4linear-gradient(135deg, light-dark(color-mix(in oklab, var(--global-color-data-4), black 14%), color-mix(in oklab, var(--global-color-data-4), white 22%)), light-dark(color-mix(in oklab, color-mix(in oklab, var(--global-color-data-4), var(--global-color-data-2) 55%), black 14%), color-mix(in oklab, color-mix(in oklab, var(--global-color-data-4), var(--global-color-data-2) 55%), white 22%)))
--avatar-default-background-5linear-gradient(135deg, light-dark(color-mix(in oklab, var(--global-color-data-5), black 14%), color-mix(in oklab, var(--global-color-data-5), white 22%)), light-dark(color-mix(in oklab, color-mix(in oklab, var(--global-color-data-5), var(--global-color-data-6) 45%), black 14%), color-mix(in oklab, color-mix(in oklab, var(--global-color-data-5), var(--global-color-data-6) 45%), white 22%)))
--avatar-default-background-6linear-gradient(135deg, light-dark(color-mix(in oklab, var(--global-color-data-6), black 14%), color-mix(in oklab, var(--global-color-data-6), white 22%)), light-dark(color-mix(in oklab, color-mix(in oklab, var(--global-color-data-6), var(--global-color-data-5) 40%), black 14%), color-mix(in oklab, color-mix(in oklab, var(--global-color-data-6), var(--global-color-data-5) 40%), white 22%)))
--avatar-default-ring-gap2px