Skip to content

Avatar

Display user or thing with icons, images, or characters.

Basic Usage

Display avatar via icon, src, or default slot.

USER
Basic Usage

Sizes

Set size via size prop. Preset values are small (28px), default (40px), large (56px), or a custom pixel number.

Sizes

Shapes

Supports circle and square shapes.

Shapes

Image Fit

Set image fit mode via fit prop, consistent with CSS object-fit.

Image Fit

Custom Color

Customize the avatar background color via background-color prop.

YH
Custom Color

Error Handling

Handle image load error via error event. The default slot serves as fallback content.

Error Handling

Use in Nuxt

The Avatar component fully supports Nuxt 3/4 SSR rendering. The avatar size, shape, and image are correctly rendered on the server, ensuring complete user avatar information is presented on the first screen.

J
Use in Nuxt

SSR Notes:

  • ✅ Avatar size (size), shape (shape), and image fit (fit) render correctly on first load
  • ✅ Custom background color (background-color) and text content supported in SSR
  • ✅ Image src / srcset are included in server-rendered HTML
  • 💡 Image load error fallback handling is completed after client-side activation

Nuxt Auto Import

After installing the @yh-ui/nuxt module, the YhAvatar component is auto-registered, no manual import needed.

API

Props

PropDescriptionTypeDefault
shapeAvatar shape'circle' | 'square''circle'
sizeAvatar size, supports preset values or pixel numbernumber | 'large' | 'default' | 'small''default'
srcImage URLstring
src-setImage srcset for responsive loadingstring
altImage alt textstring
fitImage fit mode'fill' | 'contain' | 'cover' | 'none' | 'scale-down''cover'
iconIcon component, displayed when no imagestring | Component
colorBackground color (legacy API)string
background-colorBackground colorstring
theme-overridesTheme override variablesComponentThemeVars

Events

Event NameDescriptionParameters
errorTriggered when image fails to load(event: Event) => void

Slots

Slot NameDescription
defaultCustom content (text or icon)

Theme Variables

VariableDescriptionDefault
--yh-avatar-bg-colorBackground colorvar(--yh-color-primary-light-8, #d9ecff)
--yh-avatar-text-colorText colorvar(--yh-color-primary, #409eff)
--yh-avatar-radiusSquare avatar border radiusvar(--yh-border-radius-base, 4px)

Released under the MIT License.