Avatar

lightning-avatar

A visual representation of an object.

For Use In

Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline

A lightning-avatar component is an image that represents an object, such as an account or user. By default, the image renders in medium sizing with a rounded rectangle, which is also known as the square variant.

Here’s a basic avatar example.

1<lightning-avatar src="/images/codey.jpg" alternative-text="Codey Bear">
2</lightning-avatar>

To use an image in your org as an avatar, upload the image as a static resource in the Static Resources setup page. Import the image from the @salesforce/resourceUrl module and use it in the src attribute.

Design 

lightning-avatar implements the avatar blueprint in the Salesforce Lightning Design System (SLDS). The avatar adapts to SLDS 1 or SLDS 2 styling based on the org’s theme or the container app that you use.

SLDS 1SLDS 2
DesignAvatarAvatar
For Use InLightning Experience, Experience Builder sites, Salesforce mobile app, Lightning Out (Beta), Standalone Lightning app, Mobile OfflineLightning Experience

Handle Invalid Image Paths 

The src attribute resolves the relative path to an image, but sometimes the image path doesn’t resolve correctly because the user is offline or the image has been deleted. To handle an invalid image path, you can provide fallback initials by using the initials attribute or a fallback icon with the fallback-icon-name attribute. These attributes work together if both are specified.

If initials and fallback icon name are provided, the initials are shown and the background color of the fallback icon is used as the background color for the initials. The fallback icon is shown only when the image path is invalid and initials aren’t provided.

This example shows the initials “Sa” if the image path is invalid. The fallback icon “standard:account” provides the background color for the initials. The icon is one of the standard icons in SLDS.

1<template>
2    <lightning-avatar
3        src="/bad/image/url.jpg"
4        initials="Sa"
5        fallback-icon-name="standard:account"
6        alternative-text="Salesforce"
7    >
8    </lightning-avatar>
9</template>

Component Styling 

Use a combination of icons, variants, and utility classes to style your avatars.

Icons 

Although SLDS provides several categories of icons, only icons from the standard and custom categories can be used in lightning-avatar.

When applying SLDS classes or icons, check that they’re available in the SLDS release tied to your org. The latest SLDS resources become available only when the new release is available in your org.

Note

Variants 

Use the variant attribute with one of these values to apply styling.

  • square is the default variant, displaying a rectangle avatar with rounded corners
  • circle displays a round avatar

This example creates a round avatar.

1<template>
2    <lightning-avatar
3        variant="circle"
4        src="https://v1.lightningdesignsystem.com/assets/images/avatar2.jpg"
5        fallback-icon-name="standard:person_account"
6        alternative-text="Amy Smith"
7    ></lightning-avatar>
8</template>

Utility Classes 

To apply additional styling, use the SLDS utility classes with the class attribute.

This example adds a margin around the avatar using an SLDS class. The fallback icon is displayed if the image path is invalid.

1<lightning-avatar
2    variant="circle"
3    src="https://v1.lightningdesignsystem.com/assets/images/avatar2.jpg"
4    fallback-icon-name="standard:person_account"
5    alternative-text="Account Name"
6    class="slds-m-around_small"
7>
8</lightning-avatar>

Styling Hooks 

Component styling hooks provide CSS custom properties that use the --slds-c-* prefix and they change styling for specific elements or properties of a component. Component styling hooks are supported for SLDS 1 only. See the SLDS 1 component blueprints for available component styling hooks.

For more information, see Style Components Using Lightning Design System Styling Hooks in the Lightning Web Components Developer Guide.

Accessibility 

Use the alternative-text attribute to describe the avatar, such as a user’s initials or name. This description provides the value for the alt attribute in the img HTML tag.

Attributes 

NameDescriptionTypeDefaultRequired
alternative-textThe alternative text used to describe the avatar, which is displayed as hover text on the image.string
fallback-icon-nameThe Lightning Design System name of the icon used as a fallback when the image fails to load. The initials fallback relies on this for its background color. Names are written in the format 'standard:account' where 'standard' is the category, and 'account' is the specific icon to be displayed. Only icons from the standard and custom categories are allowed.string
initialsIf the record name contains two words, like first and last name, use the first capitalized letter of each. For records that only have a single word name, use the first two letters of that word using one capital and one lower case letter.string
sizeThe size of the avatar. Valid values are x-small, small, medium, and large. This value defaults to medium.stringmedium
srcThe URL for the image.string
variantThe variant changes the shape of the avatar. Valid values are empty, circle, and square. This value defaults to square.stringsquare