How to make mui table alternate row color in react js?
March 01, 2023Hi Friends 👋,
Welcome To aGuideHub!
To make mui table alternate row color in React js, you can use mui select TableRowStyled
variable. you have to put css styled background-color: #DDDDDD;
in &:nth-of-type(odd)
. It will make mui table alternate row color.
Today, I am going to show you, how to make mui table alternate row color in react js.
Installation
Install the following packages to use make mui table alternate row color in react js.
npm
npm install @mui/material @emotion/react @emotion/styled
yarn
yarn add @mui/material @emotion/react @emotion/styled
MUI table alternate row color example.
The below code is an example of a Material UI table. You have to import the @mui material
table and A table of tables consists of a head table and a table body
, each row being a table cell
, alternating
the background colors of the table body
used styled
to create.
App.js
import Table from "@mui/material/Table";
import TableHead from "@mui/material/TableHead";
import TableBody from "@mui/material/TableBody";
import TableRow from "@mui/material/TableRow";
import TableCell from "@mui/material/TableCell";
import styled from "@emotion/styled";
const TableRowStyled = styled(TableRow)`
&:nth-of-type(odd) {
background-color: #DDDDDD;
}
&:nth-of-type(even) {
background-color: #999999;
}
& > td {
color: white;
}
`;
export default function App() {
return (
<div className="App">
<Table>
<TableHead>
<TableRow>
<TableCell>First Name</TableCell>
<TableCell>Last Name</TableCell>
<TableCell>Age</TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableRowStyled>
<TableCell>John</TableCell>
<TableCell>Smith</TableCell>
<TableCell>32</TableCell>
</TableRowStyled>
<TableRowStyled>
<TableCell>Paul</TableCell>
<TableCell>Row</TableCell>
<TableCell>27</TableCell>
</TableRowStyled>
<TableRowStyled>
<TableCell>Doe</TableCell>
<TableCell>Boe</TableCell>
<TableCell>64</TableCell>
</TableRowStyled>
<TableRowStyled>
<TableCell>Frank</TableCell>
<TableCell>Done</TableCell>
<TableCell>53</TableCell>
</TableRowStyled>
<TableRowStyled>
<TableCell>Lucy</TableCell>
<TableCell>Juuuy</TableCell>
<TableCell>37</TableCell>
</TableRowStyled>
</TableBody>
</Table>
</div>
);
}
In the above code example, I have used the @mui/material
component and made mui table alternate row color.
Check the output of the above code example.
Here, we are provided code sandbox links for the above program to make mui table alternate row color. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍