API
Avatar
A user's picture, with their initials shown when there is no picture.
Installation
pnpm dlx zazz-ui add avatarnpx zazz-ui add avataryarn dlx zazz-ui add avatarbunx zazz-ui add avatarRun zazz-ui init first if the project has no zazz/ folder yet.
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Paint the right theme (background, form controls, scrollbars) before CSS loads -->
<meta name="color-scheme" content="light dark">
<!-- Zazz styles: base layers, then avatar in cascade order -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_layers.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_variables.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_shadows.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_breakpoints.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_reset.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_typography.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_view-transitions.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/primitives/avatar/avatar.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-sm.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-md.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-lg.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-xl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-2xl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-disabled.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-active.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-focus-visible.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-focus-within.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-hover.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-checked.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-current.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-open.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-stuck.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_properties-starting.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-sm.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-md.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-lg.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-xl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-2xl.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-disabled.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-active.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-focus-visible.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-focus-within.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-hover.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-checked.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-current.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-open.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-stuck.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-tier-starting.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-flow.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-grid.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-spacing.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-margin.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-sizing.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-typography.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-color.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-effects.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-box.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-pseudo.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_defaults.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/_utilities-sr.css">
<!-- Import map: bare specifiers in Zazz scripts resolve to pinned, SRI-checked files -->
<script type="importmap">
{
"imports": {
"signal-polyfill": "https://cdn.jsdelivr.net/npm/signal-polyfill@0.2.2/dist/index.js"
},
"integrity": {
"https://cdn.jsdelivr.net/npm/signal-polyfill@0.2.2/dist/index.js": "sha384-0OqM7OXGT6Oz1z98u/geiFn/FUyz4iWexkXHCvbIGXRRDXl6bp6IKc0j78AQ6DEK"
}
}
</script>
<!-- Polyfill: Interest Invokers (interestfor) — native in Chromium only -->
<script type="module" src="https://cdn.jsdelivr.net/npm/invokers@2.2.2/dist/esm/production/interest.js" integrity="sha384-hR2BVNtsS7fIIMwOm+f7MY0JZfuByGhQQfevCBB6evFATKzBsTw0JzH/RxD94z2T" crossorigin="anonymous"></script>
<!-- Zazz behavior: side-effect modules by tag; the rest via module imports -->
<script type="module" src="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.16.0/src/base/dialog-lifecycle.js"></script>
<!-- Theme: apply the persisted (or preferred) scheme before first paint -->
<script>
(() => {
try {
// A stored choice pins the scheme with data-theme; with no choice the
// page follows the system scheme.
const storedTheme = localStorage.getItem("theme");
if (storedTheme === "dark" || storedTheme === "light") {
document.documentElement.dataset.theme = storedTheme;
}
} catch {
/* no storage access: follow the system scheme */
}
})();
</script>A <head> that loads the base layers and avatar from jsDelivr, pinned to 0.16.0. To load the whole kit in two requests, see the CDN install.
Examples
John Doe
Founder, ACME Inc.
John Doe
Founder, ACME Inc.
<div
style="
--display: flex;
--items: center;
--gap: 2;
"
>
<span data-ui="avatar" aria-hidden="true">
<img
data-avatar-slot="image"
width="64"
height="64"
loading="lazy"
src="https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?auto=format&fit=facearea&facepad=2&w=64&h=64&q=90"
alt=""
/>
<span data-avatar-slot="fallback">JD</span>
</span>
<div style="--display: flex-col">
<span
style="
--font-size: var(--font-size-sm);
--font-weight: strong;
--leading: var(--leading-sm);
--tracking: var(--tracking-sm);
"
>John Doe</span
>
<span
style="
--font-size: var(--font-size-xs);
--leading: var(--leading-xs);
--tracking: var(--tracking-xs);
--text: var(--color-muted-foreground);
"
>Founder, ACME Inc.</span
>
</div>
</div>
<div
style="
--display: flex;
--items: center;
--gap: 2;
"
>
<span data-ui="avatar" aria-hidden="true">
<span data-avatar-slot="fallback">JD</span>
</span>
<div style="--display: flex-col">
<span
style="
--font-size: var(--font-size-sm);
--font-weight: strong;
--leading: var(--leading-sm);
--tracking: var(--tracking-sm);
"
>John Doe</span
>
<span
style="
--font-size: var(--font-size-xs);
--leading: var(--leading-xs);
--tracking: var(--tracking-xs);
--text: var(--color-muted-foreground);
"
>Founder, ACME Inc.</span
>
</div>
</div>
<span data-ui="avatar" role="img" aria-label="John Doe">
<img
data-avatar-slot="image"
width="64"
height="64"
loading="lazy"
src="https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?auto=format&fit=facearea&facepad=2&w=64&h=64&q=90"
alt=""
/>
<span data-avatar-slot="fallback" aria-hidden="true">JD</span>
</span>/**
* avatar.css: Avatar ([data-ui~="avatar"])
*
* @layer variables, components
* @requires _layers.css, _variables.css, _properties.css (--_*-resolved)
* @uses a one-cell grid: the image and the fallback share the cell with the image on
* top, so the fallback shows where the markup has no image. CSS can't tell an image
* that failed to load, so leave the image out when there is no picture.
* @uses overflow: clip, so the image takes the avatar's corners
* @uses corner-shape — round by default, so circles stay circles. A concave shape at a
* full radius empties a circle, and CSS can't read the number in superellipse(), so
* the hook keeps a convex shape from this list (round, squircle, square,
* superellipse(1 | 1.3 | 2 | infinity)) and draws anything else round; the check is an
* if(). Only Chromium and Safari draw corner-shape, and both support if(); where if()
* is missing the private is invalid and its round fallback draws. The
* --corner-shape utility still sets any shape.
* @tokens --ui-avatar-* (@layer variables; hooks are named after the utilities they back)
*
* Every utility-backed declaration reads one chain, highest first:
* var(--_<utility>-resolved, var(--ui-avatar-<utility>))
* The image takes alt="". Alone, the avatar names the person on the root (role="img" and
* aria-label) and hides the fallback's initials (aria-hidden="true"); beside a visible
* name, the whole avatar takes aria-hidden="true", so the name is read once.
*/
@layer variables {
:root {
/* dual-mode: a scale number or a length */
--ui-avatar-size: calc(var(--spacing) * 10);
--ui-avatar-radius: var(--radius-full);
--ui-avatar-corner-shape: round;
--ui-avatar-bg: var(--color-background);
--ui-avatar-border-width: 1px;
--ui-avatar-border-style: solid;
--ui-avatar-border-color: var(--color-border);
/* the fallback's initials inherit these */
--ui-avatar-font-size: var(--font-size-sm);
--ui-avatar-font-weight: var(--font-weight-strong);
--ui-avatar-fallback-bg: var(--color-muted);
--ui-avatar-fallback-text: var(--color-muted-foreground);
}
}
/* The hook's shape, floored (see the header). Read as var(…, round): WebKit drops a
registered "*" property read with no fallback in corner-shape. */
@property --_avatar-shape {
syntax: "*";
inherits: false;
}
@layer ui.components {
:where([data-ui~="avatar"]) {
display: inline-grid;
flex-shrink: 0;
vertical-align: middle;
overflow: clip;
--_size-len: var(--_size-resolved, var(--ui-avatar-size));
--_size-num: var(--_size-resolved, var(--ui-avatar-size));
inline-size: calc(var(--_size-len) + var(--_size-num) * var(--spacing));
block-size: calc(var(--_size-len) + var(--_size-num) * var(--spacing));
background-color: var(--_bg-resolved, var(--ui-avatar-bg));
border-width: var(--_border-width-resolved, var(--ui-avatar-border-width));
border-style: var(--_border-style-resolved, var(--ui-avatar-border-style));
border-color: var(--_border-color-resolved, var(--ui-avatar-border-color));
--_radius-len: var(--_radius-resolved, var(--ui-avatar-radius));
--_radius-num: var(--_radius-resolved, var(--ui-avatar-radius));
border-radius: calc(
var(--_radius-len) + var(--_radius-num) * var(--spacing) * var(--radius-coefficient)
);
--_avatar-shape: if(
style(--ui-avatar-corner-shape: round) or style(--ui-avatar-corner-shape: squircle) or
style(--ui-avatar-corner-shape: square) or style(--ui-avatar-corner-shape: superellipse(1))
or style(--ui-avatar-corner-shape: superellipse(1.3)) or
style(--ui-avatar-corner-shape: superellipse(2)) or
style(--ui-avatar-corner-shape: superellipse(infinity)): var(--ui-avatar-corner-shape);
else: round
);
corner-shape: var(--_corner-shape-resolved, var(--_avatar-shape, round));
font-size: var(--_font-size-resolved, var(--ui-avatar-font-size));
font-weight: var(--_font-weight-resolved, var(--ui-avatar-font-weight));
line-height: 1;
user-select: none;
}
/* Both parts fill the one cell; the image paints over the fallback */
:where([data-ui~="avatar"]) [data-avatar-slot~="image"] {
grid-area: 1 / 1;
z-index: 1;
inline-size: 100%;
block-size: 100%;
object-fit: cover;
}
:where([data-ui~="avatar"]) [data-avatar-slot~="fallback"] {
grid-area: 1 / 1;
display: grid;
place-items: center;
inline-size: 100%;
block-size: 100%;
background-color: var(--ui-avatar-fallback-bg);
color: var(--ui-avatar-fallback-text);
}
}Attributes
| Attribute | Kind | Values | Notes |
|---|---|---|---|
data-ui="avatar" | Identity | free value | Identity token in data-ui. |
data-avatar-slot | Slot | fallback image | Slot: a part inside the primitive. |
Hooks
Set a hook on :root, on a subtree, or inline on one element. A state hook (--…--hover) themes that state only; the primitive keeps its other states.
| Hook | Default | State |
|---|---|---|
--ui-avatar-bg | var(--color-background) | — |
--ui-avatar-border-color | var(--color-border) | — |
--ui-avatar-border-style | solid | — |
--ui-avatar-border-width | 1px | — |
--ui-avatar-corner-shape | round | — |
--ui-avatar-fallback-bg | var(--color-muted) | — |
--ui-avatar-fallback-text | var(--color-muted-foreground) | — |
--ui-avatar-font-size | var(--font-size-sm) | — |
--ui-avatar-font-weight | var(--font-weight-strong) | — |
--ui-avatar-radius | var(--radius-full) | — |
--ui-avatar-size | calc(var(--spacing) * 10) | — |