Opaline for Svelte and SvelteKit: seeded avatars from end to end
Most users never upload a photo. This guide shows how to give each of them their own avatar in a Svelte 5 or SvelteKit app, with no hydration warnings.
You will learn how to install Opaline, render your first avatar, pick a style, add photo fallback, build a team group, theme the colors and use it with SvelteKit SSR.
You need: Svelte 5.20 or later. Node 22 or later for development.
1. Why avatars need a seed
The left side is what most apps ship. The right side is one prop: seed.
- Initials collide. Two people are both “JS”.
- Random colors change on every render unless you store them.
- Generated avatars that use
Math.random()make the server render one face and the browser another.
Opaline has one input, the seed. The same seed and options always give the same SVG.
2. Install
npm i @inlaylab/opaline-svelte
The package installs the core (@inlaylab/opaline) for you. It is built with Svelte 5 runes. Styles are separate imports from the core, so you only ship the ones you use.
3. Your first avatar
<script lang="ts">
import { Avatar } from '@inlaylab/opaline-svelte'
import { clay } from '@inlaylab/opaline/clay'
let { user }: { user: { email: string; name: string } } = $props()
</script>
<Avatar seed={user.email} look={clay} alt={user.name} />
Props:
| Prop | What it does |
|---|---|
seed |
The string that decides the avatar. Use a stable ID or email. |
look |
The style object: aura, morph, gradient, clay, plush, pip, initials, critters, cosmos, folk, doodle, moji, inkwell, isometric or contour. |
size |
Width and height in pixels. |
src |
A photo URL. See photo fallback. |
alt |
The accessible name. With alt the avatar is role="img". Without it, the avatar is hidden from assistive tech. |
| other props | Every core option: palette, shape, finish, expression, body, ears, text, accessory, form, and from 1.3 status, animal, coat, object, tone, scheme, from 1.4 hair, outfit, glasses, mouth, prop, topper, pattern, terrain, Plush material and more. |
Use the user ID rather than an email if people can change their email. Changing the seed changes the face. Props are reactive: change seed and the avatar redraws.
4. Choose a style
<script>
import { aura } from '@inlaylab/opaline/aura'
import { morph } from '@inlaylab/opaline/morph'
import { gradient } from '@inlaylab/opaline/gradient'
import { clay } from '@inlaylab/opaline/clay'
import { initials } from '@inlaylab/opaline/initials'
</script>
<Avatar seed={id} look={aura} />
<Avatar seed={id} look={initials} />
| Style | Looks like |
|---|---|
| Aura | A glowing orb of blended color |
| Morph | An organic blob with a face |
| Gradient | Smooth shapes with expressive faces |
| Clay | Small characters with jelly or hood bodies |
| Plush | Gummy critters with a seeded accessory |
| Pip | Geometric shapes with two pill eyes |
| Initials | Letters from the seed on a seeded tile |
| Critters | Soft vinyl-toy animals, nineteen of them |
| Cosmos | Glowing pastel space objects |
| Folk | Human-like people with personality faces |
| Doodle | Face-only line drawings |
| Moji | Bright cartoon people |
| Inkwell | Ink-line people with props |
| Isometric | Little block characters |
| Contour | Contour-map tiles |
5. Customize Clay and Initials
Clay finishes, bodies and ears
<Avatar seed={id} look={clay} body="hood" ears="bunny" finish="gloss" />
body:jelly,hoodorghostears:cat,bear,bunny,foxormouse(hood only)finish:flat,soft,glossorgummy
Any option you set fixes that part. The seed fills in everything else.
Initials and shapes
<Avatar seed="ada.lovelace@inlay.dev" look={initials} shape="squircle" /> <!-- AL -->
<Avatar seed={user.id} look={initials} text="Op" palette="pastel" />
Letters come from the seed: Zoë Nguyễn gives ZN. Set text for your own, up to 3 characters. Every script works, and the letters clear WCAG AA (4.5:1) contrast.
Every style accepts shape: circle, squircle or square. Folk, Doodle, Moji, Inkwell and Isometric also take none, which drops their tile.
New in 1.4: Moji, Inkwell, Isometric and Contour
Moji takes hair, outfit and expression; Inkwell takes hair, glasses, mouth, prop and expression; Isometric takes body, topper, prop, pattern, expression and palette; Contour takes terrain and palette. Moji, Inkwell and Isometric share 26 expressions.
New in 1.3: Critters, Cosmos, Folk, Doodle and status
<script>
import { Avatar } from '@inlaylab/opaline-svelte'
import { clay } from '@inlaylab/opaline/clay'
import { critters } from '@inlaylab/opaline/critters'
import { cosmos } from '@inlaylab/opaline/cosmos'
import { folk } from '@inlaylab/opaline/folk'
</script>
<Avatar seed={id} look={critters} animal="fox" coat="palette" />
<Avatar seed={id} look={cosmos} object="ufo" shape="circle" />
<Avatar seed={id} look={folk} expression="smug" scheme="sunrise" shape="circle" />
<Avatar seed={id} look={clay} finish="gummy" status="online" alt="Ada" />
- Critters:
animal(19) andcoat(naturalorpalette). - Cosmos:
object(15) andtone. Withshape, it sits on a night sky. - Folk and Doodle: their own
expression(18),moodandscheme(11 colour looks). - Plush:
material(gummy,flatorsoft). Plush ignoresfinish. - Every style:
status(online,away,busyoroffline). Withalt, it is read out, e.g.Ada (online). The badge is not drawn below 20px.
Existing avatars do not change: a seed you use in 1.2 draws the same bytes in 1.3.
6. Photo fallback
<Avatar seed={user.email} look={clay} src={user.photo} alt={user.name} />
The generated avatar shows first. When the photo loads, it replaces the avatar. If the photo fails, the generated avatar stays. You never get a broken image icon.
7. Avatar groups
<script>
import { AvatarGroup } from '@inlaylab/opaline-svelte'
import { clay } from '@inlaylab/opaline/clay'
const members = [
'ada@inlay.dev',
{ seed: 'grace@inlay.dev', src: '/grace.jpg', alt: 'Grace' },
'linus@inlay.dev',
'margaret@inlay.dev',
'alan@inlay.dev',
]
</script>
<AvatarGroup look={clay} {members} max={4} alt="Project members" />
Members are seeds, or { seed, src, alt, status }. Past max, a +N chip shows the rest. Avatars default to 40px.
8. Theme with CSS variables
Colors are CSS variables, so a parent element can restyle every avatar below it.
<div class="team">
<AvatarGroup look={clay} {members} max={4} />
</div>
<style>
.team {
--avatar-c1: #7c3aed; /* the body */
--avatar-ring: #0b0b14; /* ring between grouped avatars: your background */
--avatar-more-bg: #1f1f2e; /* the +N chip */
--avatar-more-fg: #e5e5f0;
}
</style>
For Initials, --avatar-fg sets the letter color. For Clay, --avatar-c2 is the arms and ears, and --avatar-c3 is the hood opening.
9. SvelteKit and SSR
The components are SSR-safe. Server and client produce the same markup, so hydration changes nothing.
<!-- src/routes/+page.svelte -->
<script lang="ts">
import { gradient } from '@inlaylab/opaline/gradient'
import { Avatar } from '@inlaylab/opaline-svelte'
</script>
<main>
<Avatar seed="ada@inlay.dev" look={gradient} src="/ada.jpg" alt="Ada" />
<Avatar seed="ada@inlay.dev" look={gradient} />
</main>
The repo’s apps/ssr-sveltekit sample (adapter-node) does exactly this and checks in a browser that hydration changes nothing. The same seed gives the same bytes on the server, in every browser and in every 1.x release.
10. Export a PNG
For profile uploads, social cards or push notifications that refuse SVG:
import { createAvatar } from '@inlaylab/opaline'
import { clay } from '@inlaylab/opaline/clay'
import { toPng } from '@inlaylab/opaline/png'
const blob = await toPng(createAvatar(clay, { seed: user.email }), { size: 512 })
toPng runs in the browser with no dependencies. On the server (for example a SvelteKit +server.ts endpoint), bake the colors in with forRaster and use a rasterizer such as @resvg/resvg-js:
import { forRaster } from '@inlaylab/opaline/png'
const png = new Resvg(forRaster(createAvatar(clay, { seed }), { size: 512 })).render().asPng()
11. Full example: a team list
<script lang="ts">
import { Avatar, AvatarGroup } from '@inlaylab/opaline-svelte'
import { clay } from '@inlaylab/opaline/clay'
type Member = { id: string; name: string; photo?: string }
let { members }: { members: Member[] } = $props()
const group = $derived(members.map((m) => ({ seed: m.id, src: m.photo, alt: m.name })))
</script>
<section>
<AvatarGroup look={clay} members={group} max={4} alt="Team" />
<ul>
{#each members as m (m.id)}
<li>
<Avatar seed={m.id} look={clay} src={m.photo} alt={m.name} size={32} />
{m.name}
</li>
{/each}
</ul>
</section>
Troubleshooting
| Problem | Fix |
|---|---|
| Avatar changed after a user edited their email | Seed with a stable user ID. |
| Photo never shows | Check src loads in the network tab. A failed photo keeps the generated avatar on purpose. |
| Svelte 4 project | The package needs Svelte 5.20 or later. |
| Avatar group ring looks wrong on a dark page | Set --avatar-ring to your page background. |
Next steps
- Try every setting in the playground and copy the Svelte code.
- Read the package README.
- Compare with other libraries on the compare page.
MIT for code and art.