Avatar头像

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

设计师专属

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

代码演示

Avatar头像 - 图1

基本

头像有三种尺寸,两种形状可选。

TypeScript

JavaScript

Avatar头像 - 图2

  1. import { Avatar } from 'antd';
  2. import { UserOutlined } from '@ant-design/icons';
  3. ReactDOM.render(
  4. <>
  5. <div>
  6. <Avatar size={64} icon={<UserOutlined />} />
  7. <Avatar size="large" icon={<UserOutlined />} />
  8. <Avatar icon={<UserOutlined />} />
  9. <Avatar size="small" icon={<UserOutlined />} />
  10. </div>
  11. <div>
  12. <Avatar shape="square" size={64} icon={<UserOutlined />} />
  13. <Avatar shape="square" size="large" icon={<UserOutlined />} />
  14. <Avatar shape="square" icon={<UserOutlined />} />
  15. <Avatar shape="square" size="small" icon={<UserOutlined />} />
  16. </div>
  17. </>,
  18. mountNode,
  19. );

Avatar头像 - 图3

自动调整字符大小

对于字符型的头像,当字符串较长时,字体大小可以根据头像宽度自动调整。也可使用 gap 来设置字符距离左右两侧边界单位像素。

TypeScript

JavaScript

Avatar头像 - 图4

  1. import React, { useState } from 'react';
  2. import { Avatar, Button } from 'antd';
  3. const UserList = ['U', 'Lucy', 'Tom', 'Edward'];
  4. const ColorList = ['#f56a00', '#7265e6', '#ffbf00', '#00a2ae'];
  5. const GapList = [4, 3, 2, 1];
  6. const Autoset: React.FC = () => {
  7. const [user, setUser] = useState(UserList[0]);
  8. const [color, setColor] = useState(ColorList[0]);
  9. const [gap, setGap] = useState(GapList[0]);
  10. const changeUser = () => {
  11. const index = UserList.indexOf(user);
  12. setUser(index < UserList.length - 1 ? UserList[index + 1] : UserList[0]);
  13. setColor(index < ColorList.length - 1 ? ColorList[index + 1] : ColorList[0]);
  14. };
  15. const changeGap = () => {
  16. const index = GapList.indexOf(gap);
  17. setGap(index < GapList.length - 1 ? GapList[index + 1] : GapList[0]);
  18. };
  19. return (
  20. <>
  21. <Avatar style={{ backgroundColor: color, verticalAlign: 'middle' }} size="large" gap={gap}>
  22. {user}
  23. </Avatar>
  24. <Button
  25. size="small"
  26. style={{ margin: '0 16px', verticalAlign: 'middle' }}
  27. onClick={changeUser}
  28. >
  29. ChangeUser
  30. </Button>
  31. <Button size="small" style={{ verticalAlign: 'middle' }} onClick={changeGap}>
  32. changeGap
  33. </Button>
  34. </>
  35. );
  36. };
  37. ReactDOM.render(<Autoset />, mountNode);

Avatar头像 - 图5

Avatar.Group

头像组合展现。

TypeScript

JavaScript

Avatar头像 - 图6

  1. import { Avatar, Divider, Tooltip } from 'antd';
  2. import { UserOutlined, AntDesignOutlined } from '@ant-design/icons';
  3. const Demo = () => (
  4. <>
  5. <Avatar.Group>
  6. <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
  7. <Avatar style={{ backgroundColor: '#f56a00' }}>K</Avatar>
  8. <Tooltip title="Ant User" placement="top">
  9. <Avatar style={{ backgroundColor: '#87d068' }} icon={<UserOutlined />} />
  10. </Tooltip>
  11. <Avatar style={{ backgroundColor: '#1890ff' }} icon={<AntDesignOutlined />} />
  12. </Avatar.Group>
  13. <Divider />
  14. <Avatar.Group maxCount={2} maxStyle={{ color: '#f56a00', backgroundColor: '#fde3cf' }}>
  15. <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
  16. <Avatar style={{ backgroundColor: '#f56a00' }}>K</Avatar>
  17. <Tooltip title="Ant User" placement="top">
  18. <Avatar style={{ backgroundColor: '#87d068' }} icon={<UserOutlined />} />
  19. </Tooltip>
  20. <Avatar style={{ backgroundColor: '#1890ff' }} icon={<AntDesignOutlined />} />
  21. </Avatar.Group>
  22. <Divider />
  23. <Avatar.Group
  24. maxCount={2}
  25. size="large"
  26. maxStyle={{ color: '#f56a00', backgroundColor: '#fde3cf' }}
  27. >
  28. <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
  29. <Avatar style={{ backgroundColor: '#f56a00' }}>K</Avatar>
  30. <Tooltip title="Ant User" placement="top">
  31. <Avatar style={{ backgroundColor: '#87d068' }} icon={<UserOutlined />} />
  32. </Tooltip>
  33. <Avatar style={{ backgroundColor: '#1890ff' }} icon={<AntDesignOutlined />} />
  34. </Avatar.Group>
  35. </>
  36. );
  37. ReactDOM.render(<Demo />, mountNode);

Avatar头像 - 图7

类型

支持三种类型:图片、Icon 以及字符,其中 Icon 和字符型可以自定义图标颜色及背景色。

TypeScript

JavaScript

Avatar头像 - 图8

  1. import { Avatar, Image } from 'antd';
  2. import { UserOutlined } from '@ant-design/icons';
  3. ReactDOM.render(
  4. <>
  5. <Avatar icon={<UserOutlined />} />
  6. <Avatar>U</Avatar>
  7. <Avatar size={40}>USER</Avatar>
  8. <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
  9. <Avatar
  10. src={<Image src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />}
  11. />
  12. <Avatar style={{ color: '#f56a00', backgroundColor: '#fde3cf' }}>U</Avatar>
  13. <Avatar style={{ backgroundColor: '#87d068' }} icon={<UserOutlined />} />
  14. </>,
  15. mountNode,
  16. );

Avatar头像 - 图9

带徽标的头像

通常用于消息提示。

TypeScript

JavaScript

Avatar头像 - 图10

  1. import { Avatar, Badge } from 'antd';
  2. import { UserOutlined } from '@ant-design/icons';
  3. ReactDOM.render(
  4. <>
  5. <span className="avatar-item">
  6. <Badge count={1}>
  7. <Avatar shape="square" icon={<UserOutlined />} />
  8. </Badge>
  9. </span>
  10. <span>
  11. <Badge dot>
  12. <Avatar shape="square" icon={<UserOutlined />} />
  13. </Badge>
  14. </span>
  15. </>,
  16. mountNode,
  17. );
  1. /* tile uploaded pictures */
  2. .avatar-item {
  3. margin-right: 24px;
  4. }
  5. [class*='-col-rtl'] .avatar-item {
  6. margin-right: 0;
  7. margin-left: 24px;
  8. }

Avatar头像 - 图11

响应式尺寸

头像大小可以根据屏幕大小自动调整。

TypeScript

JavaScript

Avatar头像 - 图12

  1. import { Avatar } from 'antd';
  2. import { AntDesignOutlined } from '@ant-design/icons';
  3. ReactDOM.render(
  4. <Avatar
  5. size={{ xs: 24, sm: 32, md: 40, lg: 64, xl: 80, xxl: 100 }}
  6. icon={<AntDesignOutlined />}
  7. />,
  8. mountNode,
  9. );

API

Avatar

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

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

Avatar.Group (4.5.0+)

参数说明类型默认值版本
maxCount显示的最大头像个数number-
maxPopoverPlacement多余头像气泡弹出位置top | bottomtop
maxStyle多余头像样式CSSProperties-
size设置头像的大小number | large | small | default | { xs: number, sm: number, …}default4.8.0