HelpBot Assistant

How can I help you?

Getting started with React Avatar component

6 Feb 20265 minutes to read

This section explains how to create a simple Avatar using styles in the React framework.

Dependencies

The Avatar component is a pure CSS-based component and does not require any JavaScript dependencies for rendering.

Installation and configuration

To easily set up a React application, use create-vite-app, which provides a faster development environment, smaller bundle sizes, and optimized builds compared to traditional tools such as create-react-app. For detailed steps, refer to the Vite installation instructions. Vite configures the project using JavaScript or TypeScript and streamlines the build process for production environments.

Note: To create a React application using create-react-app, refer to this documentation for more details.

To create a new React application, run the following command:

npm create vite@latest my-app

To set up a React application in a TypeScript environment, run the following commands:

npm create vite@latest my-app -- --template react-ts
cd my-app
npm run dev

To set up a React application in a JavaScript environment, run the following commands:

npm create vite@latest my-app -- --template react
cd my-app
npm run dev

Adding Syncfusion® packages

All the available Essential® JS 2 packages are published in npmjs.com public registry.

To install Avatar component, use the following command

npm install @syncfusion/ej2-layouts --save

Adding CSS Reference

The Avatar CSS files include base styles from @syncfusion/ej2-base and component styles from @syncfusion/ej2-layouts. Add the following CSS references to the src/App.css file to apply the required styles:

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

Adding a simple Avatar

Add a div element with the e-avatar class in the App.tsx file:

<div className="e-avatar">AJ</div>

Run the application

Run the npm run dev command to start the development server. This command compiles the application and serves it locally in the browser.

npm run dev

Output will be as follows:

import * as React from "react";
import * as ReactDom from "react-dom";
import './index.css';
function ReactApp() {
    return (<div>
      <span className="e-avatar e-avatar-xlarge"></span>
      <span className="e-avatar e-avatar-large"></span>
      <span className="e-avatar"></span>
      <span className="e-avatar e-avatar-small"></span>
      <span className="e-avatar e-avatar-xsmall"></span>
  </div>);
}
export default ReactApp;
ReactDom.render(<ReactApp />, document.getElementById("element"));
import * as React from "react";
import * as ReactDom from "react-dom";

function ReactApp() {
  return (
  <div>
      <span className="e-avatar e-avatar-xlarge"></span>
      <span className="e-avatar e-avatar-large"></span>
      <span className="e-avatar"></span>
      <span className="e-avatar e-avatar-small"></span>
      <span className="e-avatar e-avatar-xsmall"></span>
  </div>
  );
}
export default ReactApp;
ReactDom.render(<ReactApp />, document.getElementById('element'));
#container {
    visibility: hidden;
}

#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;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Syncfusion EJ2 React Avatar Sample</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Essential JS 2 for React Components" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-base/styles/tailwind3.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/32.2.3/ej2-layouts/styles/tailwind3.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
    <link rel="stylesheet" href="index.css">
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
    <div id='element'>
        <div id='loader'>Loading....</div>
    </div>
</body>
</html>

See Also

Types of Avatar