Skip to content

头像 Avatar

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

基本使用

三种尺寸,两种形状可选


Show Code
vue
<script setup lang="ts">
import { UserOutlined } from '@ant-design/icons-vue'
</script>
<template>
  <Space vertical>
    <Space align="center">
      <Avatar :size="64">
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
      <Avatar size="large">
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
      <Avatar>
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
      <Avatar size="small">
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
    </Space>
    <Space align="center">
      <Avatar shape="square" :size="64">
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
      <Avatar shape="square" size="large">
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
      <Avatar shape="square">
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
      <Avatar shape="square" size="small">
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
    </Space>
  </Space>
</template>

自定义背景色

Show Code
vue
<script setup lang="ts">
import { UserOutlined } from '@ant-design/icons-vue'
</script>
<template>
  <Space>
    <Avatar color="#1677ff">
      <template #icon>
        <UserOutlined />
      </template>
    </Avatar>
    <Avatar color="#ff6900" shape="square">
      <template #icon>
        <UserOutlined />
      </template>
    </Avatar>
    <Avatar color="#87d068">
      <template #icon>
        <UserOutlined />
      </template>
    </Avatar>
  </Space>
</template>

自定义图标类型

Show Code
vue
<script setup lang="ts">
import { h } from 'vue'
import { UserOutlined, TeamOutlined } from '@ant-design/icons-vue'
</script>
<template>
  <Space align="center">
    <Avatar :icon="() => h(TeamOutlined)" />
    <Avatar>
      <template #icon>
        <UserOutlined />
      </template>
    </Avatar>
    <Avatar>U</Avatar>
    <Avatar :size="40">USER</Avatar>
    <Avatar :size="40" src="https://cdn.jsdelivr.net/gh/themusecatcher/resources@0.0.5/1.jpg" />
    <Avatar style="color: #f56a00; background-color: #fde3cf">U</Avatar>
  </Space>
</template>

带徽标的头像

1
Show Code
vue
<script setup lang="ts">
import { UserOutlined } from '@ant-design/icons-vue'
</script>
<template>
  <Space>
    <Badge :value="1">
      <Avatar shape="square">
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
    </Badge>
    <Badge dot>
      <Avatar shape="square">
        <template #icon>
          <UserOutlined />
        </template>
      </Avatar>
    </Badge>
  </Space>
</template>

响应式尺寸

Show Code
vue
<script setup lang="ts">
import { SketchOutlined } from '@ant-design/icons-vue'
</script>
<template>
  <Avatar :size="{ xs: 24, sm: 32, md: 40, lg: 64, xl: 80, xxl: 100 }">
    <template #icon>
      <SketchOutlined />
    </template>
  </Avatar>
</template>

链接跳转

Show Code
vue
<template>
  <Tooltip tooltip="themusecatcher@github">
    <Avatar
      :size="36"
      src="https://github.com/themusecatcher.png"
      href="https://github.com/themusecatcher"
      target="_blank"
    />
  </Tooltip>
</template>

APIs

Avatar

参数说明类型默认值
color头像的背景色string'rgba(0, 0, 0, 0.25)'
shape指定头像的形状'circle' | 'square''circle'
size设置头像的大小,number类型时单位 pxnumber | 'small' | 'middle' | 'large' | Responsive'middle'
src图片类头像资源地址stringundefined
alt图片无法显示时的替代文本stringundefined
icon设置头像的图标VNode | slotundefined
href点击跳转的地址,指定此属性按钮的行为和 a 链接一致stringundefined
target相当于 a 标签的 target 属性,href 存在时生效'self' | '_blank''self'

Responsive Type

名称说明类型默认值
xs<576px 响应式栅格numberundefined
sm≥576px 响应式栅格numberundefined
md≥768px 响应式栅格numberundefined
lg≥992px 响应式栅格numberundefined
xl≥1200px 响应式栅格numberundefined
xxl≥1600px 响应式栅格numberundefined

Released under the MIT License.