How to make mui avatar with name in react js?
April 20, 2023Hi Friends 👋,
Welcome To aGuideHub!
To make mui avatar with name in React js, you can use this code <Typography>
. It will make mui avatar with name in react js.
Today, I am going to show you, how to make mui avatar with name in react js.
Installation
Install the following packages to use mui avatar in react js.
npm
npm install @mui/material @emotion/react @emotion/styled
yarn
yarn add @mui/material @emotion/react @emotion/styled
Table of contents
- Install MUI and create a new React app.
- Import the Avatar component from MUI.
- Use the Avatar component in your JS code.
- Add inline styles to adjust the name.
Step 1: Install MUI and create a new React app.
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 the Avatar component from MUI.
After installing MUI
, you have to import your React component. To do this, add the following line to the top of your component file.
import { Avatar, Typography } from '@mui/material';
Step 3: Use the Avatar component in your JS code.
You can use the Avatar
component in your react js. For example, you can have the following code to display an avatar with an image
.
<Avatar src="/path/to/image.jpg" />
Step 4: Add inline styles to adjust the name.
You can add inline styles to the Typography component. For example, you can add the following style to set the ml
to 1
pixels.
<Typography variant="subtitle1" sx={{ ml: 1 }}>
John Doe
</Typography>
MUI material make mui avatar name example.
The below code is an example of making name of mui avatar using.
The Avatar
component is used to display an image of a person. The typography
component has been used to display the person’s name. The margin-left
has been given with the help of sx
prop for the space between avatar and name.
App.js
import React from 'react';
import { Avatar, Typography } from '@mui/material';
const App = () => {
return (
<div style={{ display: 'flex', alignItems: 'center' }}>
<Avatar src="/path/to/image.jpg" />
<Typography variant="subtitle1" sx={{ ml: 1 }}>
John Doe
</Typography>
</div>
);
};
export default App;
In the above code example, I have used the @mui/material
component and made mui avatar with name in react js.
Check the output of the above code example.
Here, we are provided code sandbox links for the above program make mui avatar with name in react js. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍