JGuang

头像

Avatar · Ant Design

交互预览
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

头像

用来代表用户或事物,支持图片、图标或字符展示。

设计师专属

安装 Kitchen Sketch 插件 💎,一键填充高逼格头像和文本。

代码演示

  • 基本 · basic

  • 类型 · type

  • 自动调整字符大小 · dynamic

  • 带徽标的头像 · badge

  • Avatar.Group · group

  • maxCount 包含溢出元素 · max-count

  • 响应式尺寸 · responsive

API

通用属性参考:通用属性

Avatar

参数说明类型默认值版本全局配置
alt图像无法显示时的替代文本string-×
gap字符类型距离左右两侧边界单位像素number44.3.0×
icon设置头像的自定义图标ReactNode-×
shape指定头像的形状circle | squarecircle×
size设置头像的大小number | large | medium | small | { xs: number, sm: number, ...}medium4.7.0×
src图片类头像的资源地址或者图片元素string | ReactNode-ReactNode: 4.8.0×
srcSet设置图片类头像响应式资源地址string-×
draggable图片是否允许拖动boolean | 'true' | 'false'true×
crossOriginCORS 属性设置'anonymous' | 'use-credentials' | ''-4.17.0×
onError图片加载失败的事件,返回 false 会关闭组件默认的 fallback 行为() => boolean-×

Tip:你可以设置 icon 或 children 作为图片加载失败的默认 fallback 行为,优先级为 icon > children

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

参数说明类型默认值版本
max设置最多显示相关配置{ count?: number; style?: CSSProperties; popover?: PopoverProps }-5.18.0
maxCount已废弃,请使用 max={{ count: number }}number-
maxPopoverPlacement已废弃,请使用 max={{ popover: PopoverProps }}top | bottomtop
maxPopoverTrigger已废弃,请使用 max={{ popover: PopoverProps }}hover | focus | clickhover
maxStyle已废弃,请使用 max={{ style: CSSProperties }}CSSProperties-
size设置头像的大小number | large | medium | small | { xs: number, sm: number, ...}medium4.8.0
shape设置头像的形状circle | squarecircle5.8.0

主题变量(Design Token)

类型、继承关系和原生令牌声明保持固定源码原文;默认值由原生主题计算,并可能受 ConfigProvider 覆盖。本表不伪造静态默认值。

令牌类型原生说明
containerSizenumber头像尺寸
containerSizeLGnumber大号头像尺寸
containerSizeSMnumber小号头像尺寸
textFontSizenumber头像文字大小
textFontSizeLGnumber大号头像文字大小
textFontSizeSMnumber小号头像文字大小
iconFontSizenumber头像图标大小
iconFontSizeLGnumber | string大号头像图标大小
iconFontSizeSMnumber小号头像图标大小
groupSpacenumber头像组间距
groupOverlappingnumber头像组重叠宽度
groupBorderColorstring头像组边框颜色

声明来源: 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;
};

原生通用属性

Tips: 以下通用属性适用于 antd 大部分组件,不支持的组件会单独说明。

参数说明类型默认值
style自定义样式CSSProperties-
className自定义类名string-
rootClassName添加在组件最外层的 classNamestring-
autoFocus自动获取焦点,仅对表单类、链接、交互容器等可聚焦元素生效booleanfalse

安装并复现示例

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

使用 ConfigProvider 和原生 App,并单独配置 dayjs 语言。每个示例在查看代码中列出完整源码文件和额外固定依赖。示例名称用于选择演示,不是组件通用属性。主题、语言与 SSR 初始化见资源安装指南。

原生类型参考

源码查看器包含未修改的原生实现和以下声明文件,继承类型仍位于原生包中。代码中的英文注释保留为源码参考。

  • 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

来源与许可

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

保留原生中英文正文与可执行示例;官网专用的嵌入章节和令牌表格在本地展开。Ant Design 使用 MIT 许可,另列依赖的许可证也需保留。上传预览禁止向外发起请求,不提供后端服务。