Skip to Content
CodeUI

Avatar

@codeui/styles v0.3.0

A person in plain HTML: their picture, or their initials on a colour from their id.

Load @codeui/styles/avatar.css, or all.css. The Solid component is documented in Avatar.

Demo

avatar/basic.html
<div class="cui-stack" data-direction="row" data-gap="2" data-align="center">  <span class="cui-avatar" data-cui="avatar" data-size="sm" role="img" aria-label="Grace Hopper" style="--cui-avatar-hue: 25">    <span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">GH</span>  </span>  <span class="cui-avatar" data-cui="avatar" data-size="sm" role="img" aria-label="Alan Turing" style="--cui-avatar-hue: 142">    <span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">AT</span>  </span>  <span class="cui-avatar" data-cui="avatar" data-size="sm" role="img" aria-label="Guest" style="--cui-avatar-chroma: 0">    <span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">G</span>  </span>  <span class="cui-avatar" data-cui="avatar" data-size="sm" data-unassigned role="img" aria-label="Unassigned"></span></div>

Markup

PartElementClassdata-cui
Rootspan with role="img" and the name as aria-labelcui-avataravatar
Initialsspan with aria-hidden="true"cui-avatar-fallbackavatar-fallback
Pictureimg with alt="", after the initialscui-avatar-imageavatar-image
AttributeValuesDefault
data-sizexs, sm, md, lgxs
data-unassignedPresenceOff
--cui-avatar-hue (inline style)A hue in degrees0
--cui-avatar-chroma (inline style)0 for a neutral fillSet per theme

Variants

Size

avatar/sizes.html
<div class="cui-stack" data-direction="row" data-gap="3" data-align="center">  <span class="cui-avatar" data-cui="avatar" data-size="xs" role="img" aria-label="Grace Hopper" style="--cui-avatar-hue: 25"><span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">GH</span></span>  <span class="cui-avatar" data-cui="avatar" data-size="sm" role="img" aria-label="Grace Hopper" style="--cui-avatar-hue: 25"><span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">GH</span></span>  <span class="cui-avatar" data-cui="avatar" data-size="md" role="img" aria-label="Grace Hopper" style="--cui-avatar-hue: 25"><span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">GH</span></span>  <span class="cui-avatar" data-cui="avatar" data-size="lg" role="img" aria-label="Grace Hopper" style="--cui-avatar-hue: 25"><span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">GH</span></span></div>

The sizes are the shared control heights, --cui-control-height-xs to --cui-control-height-lg, so they follow data-cui-density. Comfortable density gives 28, 36, 44 and 52px.

Hue

Set --cui-avatar-hue from a stable id, never from the name. avatarHue(id) from @codeui/styles/avatar returns the hue the Solid component uses. Without an id, set --cui-avatar-chroma: 0 for a neutral fill.

States

Picture

Put the img after the initials. It covers them while it loads. When it fails, remove it and the initials show.

Unassigned

Set data-unassigned and leave the root empty. It draws a dashed circle with no fill. Name it "Unassigned".

What you supply

  • The hue from a stable id, and the initials from the name. avatarHue and avatarInitials from @codeui/styles/avatar do both.
  • Removing the picture when it fails to load.
  • A tooltip with the name, such as the Tooltip markup.

Helper

import "@codeui/styles/avatar.css";
import { applyStyleProps } from "@codeui/styles";
import { avatarHue, avatarInitials, avatarStyleProps } from "@codeui/styles/avatar";

const avatar = document.createElement("span");
applyStyleProps(avatar, avatarStyleProps({ size: "sm" }));
avatar.role = "img";
avatar.ariaLabel = "Grace Hopper";
avatar.style.setProperty("--cui-avatar-hue", String(avatarHue("user_grace")));
avatar.innerHTML = `<span class="cui-avatar-fallback" aria-hidden="true">${avatarInitials("Grace Hopper")}</span>`;
// class="cui-avatar" data-cui="avatar" data-size="sm"

On this page