How to change mui icon background color in react js?
January 24, 2024Hi Friends π,
Welcome To aGuideHub!
To change mui icon background color in react js, you can set background-color: pink;
in .MuiSvgIcon-root
. It will change mui icon background color in React JS.
Today, I am going to show you, How to change mui icon background color in react js
Installation
Install the following packages to use mui icon button 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 Material-UI icon.
- Use the icon Component.
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 Material-UI icon .
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 * as React from "react";
import SvgIcon from "@mui/material/SvgIcon";
import "./styles.css";
Step 3: Use the icon Component.
Each Material icon also has a βthemeβ: Filled (default), Outlined, Rounded, Two-tone, and Sharp. To import the icon component with a theme other than the default, append the theme name to the icon name.
<SvgIcon>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M4.5 12a7.5 7.5 0 0015 0m-15 0a7.5 7.5 0 1115 0m-15 0H3m16.5 0H21m-1.5 0H12m-8.457 3.077l1.41-.513m14.095-5.13l1.41-.513M5.106 17.785l1.15-.964m11.49-9.642l1.149-.964M7.501 19.795l.75-1.3m7.5-12.99l.75-1.3m-6.063 16.658l.26-1.477m2.605-14.772l.26-1.477m0 17.726l-.26-1.477M10.698 4.614l-.26-1.477M16.5 19.794l-.75-1.299M7.5 4.205L12 12m6.894 5.785l-1.149-.964M6.256 7.178l-1.15-.964m15.352 8.864l-1.41-.513M4.954 9.435l-1.41-.514M12.002 12l-3.75 6.495"
/>
</svg>
</SvgIcon>
MUI material change mui icon background color example.
The below code is an example, you need to import icon
Component. Then, you can set background-color: pink;
in .MuiSvgIcon-root
. Then it will change mui icon background color in react js.
App.js
import * as React from "react";
import SvgIcon from "@mui/material/SvgIcon";
import "./styles.css";
export default function SvgIconChildren() {
return (
<SvgIcon>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M4.5 12a7.5 7.5 0 0015 0m-15 0a7.5 7.5 0 1115 0m-15 0H3m16.5 0H21m-1.5 0H12m-8.457 3.077l1.41-.513m14.095-5.13l1.41-.513M5.106 17.785l1.15-.964m11.49-9.642l1.149-.964M7.501 19.795l.75-1.3m7.5-12.99l.75-1.3m-6.063 16.658l.26-1.477m2.605-14.772l.26-1.477m0 17.726l-.26-1.477M10.698 4.614l-.26-1.477M16.5 19.794l-.75-1.299M7.5 4.205L12 12m6.894 5.785l-1.149-.964M6.256 7.178l-1.15-.964m15.352 8.864l-1.41-.513M4.954 9.435l-1.41-.514M12.002 12l-3.75 6.495"
/>
</svg>
</SvgIcon>
);
}
App.css
.MuiSvgIcon-root {
background-color: pink;
}
In the above code example, I have used the @mui/material
component and change mui icon background color in react js.
Here, we are provided code sandbox links for the above program change mui icon background color in react js. Then you can use whenever you want and do the changes as per your requirements.
All the best π