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.
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.
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.
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
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.
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
Name
Description
Type
Default
Required
alternative-text
The alternative text used to describe the avatar, which is displayed as hover text on the image.
string
fallback-icon-name
The 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
initials
If 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
size
The size of the avatar. Valid values are x-small, small, medium, and large. This value defaults to medium.
string
medium
src
The URL for the image.
string
variant
The variant changes the shape of the avatar. Valid values are empty, circle, and square. This value defaults to square.