Getting started with Angular Avatar component

27 Aug 20254 minutes to read

The following section explains the steps required to create a simple Avatar component using styles and its basic usage.

Setting up Angular project

Angular provides an efficient way to create new projects using the Angular CLI tool.

Install the CLI application globally using the following command:

npm install -g @angular/cli

Create a new Angular application:

ng new syncfusion-angular-app

Navigate to the created project folder:

cd syncfusion-angular-app

Refer to the Syncfusion® Angular Getting Started section for detailed information about setting up Angular CLI projects.

Avatar component dependencies

The Avatar component is pure CSS component which doesn’t need specific dependencies to render.

The component requires the following Syncfusion® package:

|-- @syncfusion/ej2-layouts

This package provides the essential styles and utilities needed for the Avatar component.

Installing Syncfusion® Layouts package

Syncfusion® packages are distributed in npm as @syncfusion scoped packages. You can find all Angular Syncfusion® packages on npm.

Syncfusion® provides two types of package structures for Angular components:

  1. Ivy library distribution package format
  2. Angular compatibility compiler(Angular’s legacy compilation and rendering pipeline) package:

Ivy library distribution package

Syncfusion® Angular packages(>=20.2.36) has been moved to the Ivy distribution to support the Angular Ivy rendering engine and the package are compatible with Angular version 12 and above. To download the package use the below command.

Add @syncfusion/ej2-angular-layouts package to the application.

npm install @syncfusion/ej2-angular-layouts --save

Angular compatibility compiled package (ngcc)

For Angular versions below 12, use the legacy (ngcc) package of Syncfusion® Angular components.

Add the @syncfusion/ej2-angular-layouts@ngcc package:

npm install @syncfusion/ej2-angular-layouts@ngcc --save

To specify the ngcc package in the package.json file, add the suffix -ngcc with the package version as below.

@syncfusion/ej2-angular-layouts:"20.2.38-ngcc"

Note: If the ngcc tag is not specified during installation, the Ivy Library Package will be installed and may display warnings for incompatible Angular versions.

Adding style sheet to the application

To render the Avatar component, import the required stylesheets in the [src/styles.css] file:

@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-layouts/styles/material.css';
@import '../node_modules/@syncfusion/ej2-angular-layouts/styles/material.css';

Alternatively, import the styles directly relative to the node_modules folder based on your CSS file’s location, as shown below:

@import 'node_modules/@syncfusion/ej2-base/styles/material.css';
@import 'node_modules/@syncfusion/ej2-layouts/styles/material.css';
@import 'node_modules/@syncfusion/ej2-angular-layouts/styles/material.css';

Note: To use combined component styles, refer to Syncfusion® CRG (Custom Resource Generator) for your application.

Adding Avatar to the application

The Avatar component uses CSS classes to display user representations. Modify the template in the app.component.ts file to render the Avatar component.

[src/app/app.component.ts]

import { Component } from '@angular/core';

@Component({
    imports: [
    ],
    standalone: true,
    selector: 'app-root',
    template: `<div id='element'><span class="e-avatar">GR</span></div>`
})

export class AppComponent {}

Run the application

Run the application in the browser using the following command:

npm start

The following example demonstrates a basic Avatar component with initials:

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'

import { Component } from '@angular/core';

@Component({
    imports: [
    ],
    standalone: true,
    selector: 'my-app',
    template: `
    <div id='element'>
            <span class="e-avatar e-avatar-xlarge"></span>
            <span class="e-avatar e-avatar-large"></span>
            <span class="e-avatar"></span>
            <span class="e-avatar e-avatar-small"></span>
            <span class="e-avatar e-avatar-xsmall"></span>
    </div>
    `
})

export class AppComponent { }
@import 'node_modules/@syncfusion/ej2-base/styles/material.css';
@import 'node_modules/@syncfusion/ej2-layouts/styles/material.css';


#loader {
  color: #008cff;
  height: 40px;
  width: 30%;
  position: absolute;
  font-family: 'Helvetica Neue','calibiri';
  font-size: 14px;
  top: 45%;
  left: 45%;
}

#element {
    display: block;
    width: 300px;
    margin: 130px auto;
    border-radius: 3px;
    justify-content: center;
}

.e-avatar {
    background-image: url(https://ej2.syncfusion.com/demos/src/grid/images/2.png);
    margin: 2px;
}

#container {
  visibility: hidden;
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Refer to the Angular Avatar feature tour page for comprehensive feature demonstrations. Explore the Angular Avatar example to learn data presentation and manipulation techniques.