Open In App

React MUI Avatar API

Last Updated : 12 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

:MUI or Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google.

In this article, we are going to discuss the React MUI Avatar API. Avatars are profile pictures that are usually used for logged-in users. Avatars are found usually from tables to dialog menus. The API provides a lot of functionality and we will learn to implement them.

Import Avatar API

import Avatar from '@mui/material/Avatar';
// or
import { Avatar } from '@mui/material';

Props List: Here is the list of different props used with this component. We can access them and modify them according to our needs.

  • children (node): It is a component similar to the table row.
  • classes (Object): Override the existing styles or add new styles to the component.
  • component (elementType): It is the component used for the root node. It can be either an HTML string or a component.
  • sx (Array<func / object/bool> / func / object): The system prop allows defining system overrides as well as additional CSS styles.
  • alt: It is the alternative attribute in place of the image rendered.
  • imgProps: It is the props applied to img element.
  • sizes: The size attribute is used to modify the size of the avatar.
  • src: It is the source of the image that is provided.
  • srcSet: This attribute is used for the responsive display.
  • variant(circular/rounded/square): This attribute is used for changing the shape of the avatar.

Syntax: Create an Avatar as follows:

<Avatar alt="GeeksforGeeks" 
    src="/static/images/avatar/gfgIcon.jpg" />

Installing and Creating React app, and adding the MUI dependencies:

Step 1: Create a react project using the following command.

npx create-react-app gfg_tutorial

Step 2: Get into the project directory

cd gfg_tutorial

Step 3: Install the MUI dependencies as follows:

npm install @mui/material @emotion/react @emotion/styled @mui/lab

Step 4: Run the project as follows:

npm start

Example 1: In the following example, we have a basic Avatar.

App.js




import "./App.css";
import React from "react";
import Avatar from "@mui/material/Avatar";
function App() {
    return (
        <div className="App">
            <div
                className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}
            >
                <h1 style={{
                    color: "green",
                }}>
                    GeeksforGeeks
                </h1>
            </div>
            <div style={{
                width: "fit-content",
                margin: "auto",
            }}>
                <strong>React MUI Avatar API</strong>
            </div>
  
            <center>
                <Avatar
                    alt="GeeksforGeeks"
                    src=
                />
            </center>
        </div>
    );
}
  
export default App;


Output:

 

Example 2: In the following example, we have different shape avatars.

App.js




import "./App.css";
import React from "react";
import Avatar from "@mui/material/Avatar";
function App() {
    return (
        <div className="App">
            <div className="head"
                style={{
                    width: "fit-content",
                    margin: "auto",
                }}>
                <h1 style={{
                    color: "green",
                }}>
                    GeeksforGeeks
                </h1>
            </div>
            <div style={{
                width: "fit-content",
                margin: "auto",
            }}>
                <strong>React MUI Avatar API</strong>
            </div>
            <br />
            <div style={{
                margin: "auto",
                display: "flex",
                justifyContent: "space-evenly",
            }}>
                <Avatar
                    variant="circular"
                    alt="GeeksforGeeks"
                    src=
                />
                <Avatar
                    variant="rounded"
                    alt="GeeksforGeeks"
                    src=
                />
                <Avatar
                    variant="square"
                    alt="GeeksforGeeks"
                    src=
                />
            </div>
        </div>
    );
}
  
export default App;


Output:

 

Reference: https://mui.com/material-ui/api/avatar/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads