Avatar

A person's picture, or their initials, in a square box at a control's height.

Example

View Code
<p>
    <span class="avatar"><img src="yeti.svg" alt="Yeti"></span>
    <span class="avatar" role="img" aria-label="Joe Workman">JW</span>
    <span class="avatar" data-variant="success" role="img" aria-label="Sam Lee">SL</span>
</p>

When to use it

A person's picture, or their initials when there is no picture: beside a comment, in a list of people, in a header's account menu.

How it works

A square box as tall as a control, with a round corner by default. It uses the button's height formula, so an avatar and a button of the same data-size line up in a row. A picture inside it fills the box and is cropped, not stretched.

View Code
<p>
    <span class="avatar"><img src="yeti.svg" alt="Yeti"></span>
    <span class="avatar" role="img" aria-label="Joe Workman">JW</span>
</p>

Initials sit one step smaller than the size's text, in strong weight, on the variant's tint with the variant's text color, as a medium badge is. Keep them to two or three letters.

View Code
<p>
    <span class="avatar" data-size="sm" role="img" aria-label="Ana Ruiz">AR</span>
    <span class="avatar" role="img" aria-label="Joe Workman">JW</span>
    <span class="avatar" data-size="lg" data-variant="secondary" role="img" aria-label="Sam Lee">SL</span>
</p>

--yeti-avatar-radius sets the corner. It starts as --yeti-radius-full, so a theme that zeroes the full radius gets square avatars with no change. Set the token for a rounded square.

An account menu

An avatar beside a word in a trigger. The name is visible, so the avatar is decoration. Inside a button the avatar sizes to the line of text, not the control, so the button is as tall as one without it.

View Code
<div class="dropdown">
    <button class="button" type="button" popovertarget="avatar-menu" data-emphasis="low">
        <span class="avatar" aria-hidden="true">JW</span>
        Account
    </button>
    <ul id="avatar-menu" popover role="list">
        <li><a href="#">Profile</a></li>
        <li><button type="button">Sign out</button></li>
    </ul>
</div>

Accessibility

A picture's alt is the person's name. Initials need role="img" and an aria-label with the name, or a screen reader spells out the letters. Next to the visible name an avatar is decoration: use alt="", or aria-hidden="true" on initials. Initials meet AA in both schemes. In forced colors the avatar gets a border in the text color, so it stays a shape.

Attributes

Attribute Type Values Default Description
data-variant enum primary, secondary, success, warning, alert, danger, neutral, black, white primary Which hue tints the initials.
data-size enum sm, md, lg md The control height the avatar matches, so it lines up with a button or a field of the same size.

Children

  • > img: 0 to 1. The picture; it fills the box.

Tokens

Token Description
--yeti-avatar-radius Corner radius; full makes a circle.
--yeti-control-size The control height an avatar of the default size matches.
--yeti-weight-strong Weight of initials.
--yeti-border-width Border width, drawn in forced colors.
--yeti-color-primary The default variant's color, when data-variant is absent.
--yeti-color-primary-subtle The default variant's tint.
--yeti-color-primary-soft The default variant's soft stop.
--yeti-color-primary-strong The default variant's strong stop.
--yeti-color-primary-text The default variant's text color.
--yeti-on-primary Text on the default variant's color.
--yeti-text-md Text size when data-size is absent.
--yeti-space-sm The space step when data-size is absent.
--yeti-leading-tight The button's line height, which an avatar inside a button keeps as its own line.
Internal tokens (may change between minor versions) - `--_yeti-variant` - `--_yeti-variant-subtle` - `--_yeti-variant-text` - `--_yeti-on-variant` - `--_yeti-size-text` - `--_yeti-variant-soft` - `--_yeti-variant-strong` - `--_yeti-size-space` - `--_yeti-avatar-size`

Accessibility

  • A picture's alt is the person's name. Initials need role="img" and an aria-label with the name, or a screen reader spells out the letters. Beside the visible name an avatar is decoration: alt="", or aria-hidden="true" on initials.

Browser support

  • Used without guards: inline flexbox, aspect-ratio, overflow: clip, lh unit
  • Behind @supports: nothing

JavaScript

None. This component is CSS only.

Available since 7.0.0.