JGuang

Avatar

Avatar · Ant Design

Preview
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Avatar

Used to represent users or things, supporting the display of images, icons, or characters.

Examples

  • Basic · basic

  • Type · type

  • Autoset Font Size · dynamic

  • With Badge · badge

  • Avatar.Group · group

  • maxCount includes overflow · max-count

  • Responsive Size · responsive

API

Common props ref:Common props

Avatar

PropertyDescriptionTypeDefaultVersionGlobal Config
altThis attribute defines the alternative text describing the imagestring-×
gapLetter type unit distance between left and right sidesnumber44.3.0×
iconCustom icon type for an icon avatarReactNode-×
shapeThe shape of avatarcircle | squarecircle×
sizeThe size of the avatarnumber | large | medium | small | { xs: number, sm: number, ...}medium4.7.0×
srcThe address of the image for an image avatar or image elementstring | ReactNode-ReactNode: 4.8.0×
srcSetA list of sources to use for different screen resolutionsstring-×
draggableWhether the picture is allowed to be draggedboolean | 'true' | 'false'true×
crossOriginCORS settings attributes'anonymous' | 'use-credentials' | ''-4.17.0×
onErrorHandler when img load error, return false to prevent default fallback behavior() => boolean-×

Tip: You can set icon or children as the fallback for image load error, with the priority of icon > children

Avatar.Group <Badge>4.5.0+</Badge>

PropertyDescriptionTypeDefaultVersion
maxSet maximum display related configurations{ count?: number; style?: CSSProperties; popover?: PopoverProps }-5.18.0
maxCountDeprecated, please use max={{ count: number }}number-
maxPopoverPlacementDeprecated, please use max={{ popover: PopoverProps }}top | bottomtop
maxPopoverTriggerDeprecated, please use max={{ popover: PopoverProps }}hover | focus | clickhover
maxStyleDeprecated, please use max={{ style: CSSProperties }}CSSProperties-
sizeThe size of the avatarnumber | large | medium | small | { xs: number, sm: number, ...}medium4.8.0
shapeThe shape of the avatarcircle | squarecircle5.8.0

Design Token

Types, inheritance and declarations retain the pinned native source. Defaults are computed by the native theme and may be overridden by ConfigProvider; this table does not invent static defaults.

TokenTypeNative description
containerSizenumberSize of Avatar
containerSizeLGnumberSize of large Avatar
containerSizeSMnumberSize of small Avatar
textFontSizenumberFont size of Avatar
textFontSizeLGnumberFont size of large Avatar
textFontSizeSMnumberFont size of small Avatar
iconFontSizenumberFont size of Avatar icon
iconFontSizeLGnumber | stringFont size of large Avatar icon
iconFontSizeSMnumberFont size of small Avatar icon
groupSpacenumberSpacing between avatars in a group
groupOverlappingnumberOverlapping of avatars in a group
groupBorderColorstringBorder color of avatars in a group

Declaration source: vendor/components/avatar/style/index.ts

import type { CSSProperties } from 'react';

import { unit } from '@ant-design/cssinjs';

import type { CSSObject } from '@ant-design/cssinjs';

import { resetComponent } from '../../style';

import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';

import { genStyleHooks, mergeToken } from '../../theme/internal';

export interface ComponentToken {
  /**
   * @desc 头像尺寸
   * @descEN Size of Avatar
   */
  containerSize: number;
  /**
   * @desc 大号头像尺寸
   * @descEN Size of large Avatar
   */
  containerSizeLG: number;
  /**
   * @desc 小号头像尺寸
   * @descEN Size of small Avatar
   */
  containerSizeSM: number;
  /**
   * @desc 头像文字大小
   * @descEN Font size of Avatar
   */
  textFontSize: number;
  /**
   * @desc 大号头像文字大小
   * @descEN Font size of large Avatar
   */
  textFontSizeLG: number;
  /**
   * @desc 小号头像文字大小
   * @descEN Font size of small Avatar
   */
  textFontSizeSM: number;
  /**
   * @desc 头像图标大小
   * @descEN Font size of Avatar icon
   */
  iconFontSize: number;
  /**
   * @desc 大号头像图标大小
   * @descEN Font size of large Avatar icon
   */
  iconFontSizeLG: number | string;
  /**
   * @desc 小号头像图标大小
   * @descEN Font size of small Avatar icon
   */
  iconFontSizeSM: number;
  /**
   * @desc 头像组间距
   * @descEN Spacing between avatars in a group
   */
  groupSpace: number;
  /**
   * @desc 头像组重叠宽度
   * @descEN Overlapping of avatars in a group
   */
  groupOverlapping: number;
  /**
   * @desc 头像组边框颜色
   * @descEN Border color of avatars in a group
   */
  groupBorderColor: string;
}

/**
 * @desc Avatar 组件的 Token
 * @descEN Token for Avatar component
 */
type AvatarToken = FullToken<'Avatar'> & {
  avatarBgColor: string;
  avatarBg: string;
  avatarColor: string;
};

Shared native props

Tips: The following generic properties apply to most antd components; those not supported are described separately.

PropertyDescriptionTypeDefault
styleThe additional styleCSSProperties-
classNameThe additional css classstring-
rootClassNameClassName on the root elementstring-
autoFocusAuto focus when component mounted, only effective for focusable elements like forms, links, etc.booleanfalse

Install and reproduce an example

npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Use ConfigProvider and native App; configure dayjs locale separately. Each example lists its complete source files and additional pinned dependencies in View code. Example names select demos, not universal component props. See the resource setup guide for theme, locale and SSR.

Native type references

The source viewer includes unchanged native implementations and the following source declarations. Inherited types remain in their native packages. English source comments are preserved as code reference.

  • vendor/components/avatar/Avatar.tsx
  • vendor/components/avatar/AvatarContext.ts
  • vendor/components/avatar/AvatarGroup.tsx
  • vendor/components/avatar/index.tsx
  • vendor/components/avatar/style/index.ts

Source and license

https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd

Original bilingual prose and executable examples are retained. Website-only embeds and token tables are expanded locally. Ant Design is MIT; separately listed dependency notices also apply. Upload previews block outbound requests and do not provide a backend.