How to create avatar in react js?
April 03, 2023Hi Friends π,
Welcome To aGuideHub!
To create an avatar in React js, you have to use the npm
or yarn
package manager that is how you can install the avatar
library into your project.
Today, I am going to show you, how to create avatar in react js.
Table of contents
- Set up the React project.
- Import necessary components.
- Define the Avatar component.
Letβs start with the first step.
Step 1: Set up the React project.
First you have to install the React project. You should use create-react-app
command to create a new React project.
npx create-react-app my-app
cd my-app
npm start
Step 2: Import necessary components.
In this line, you have to import the styles.css
file that contains CSS styles for the Avatar component.
import React from "react";
import "./styles.css";
Step 3: Define the Avatar component.
In this code, you have to define a functional
component called Avatar that returns a div element with the class avatar
and the letter "R"
inside it.
const Avatar = () => {
return (
<div className="avatar">R</div>
);
};
ReactJS create avatar example.
This code below is an example, you must use the CSS class Avatar
can be applied to any HTML
element with the class attribute
set to avatar
.
App.js
import React from "react";
import "./styles.css";
const Avatar = () => {
return (
<div className="avatar">R</div>
);
};
export default Avatar;
App.css
.avatar {
position: relative;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
width: 40px;
height: 40px;
font-family: "Roboto", "Helvetica", "Arial", sans-serif;
font-size: 1.25rem;
line-height: 1;
border-radius: 50%;
overflow: hidden;
user-select: none;
color: #fff;
background-color: #bdbdbd;
}
Check the output of the above code example.
Here, we are provided code sandbox links for the above program create avatar in React js. Then you can use whenever you went and do the changes as per your requirements.
All the best π