How to align left mui table in react js?
February 29, 2024Hi Friends π,
Welcome To aGuideHub!
To align left mui table in React js, you can use this code style={{ display: 'flex', justifyContent: 'left' }}
. It will align left mui table in react js.
Today, I am going to show you, how to align left mui table in react js.
Table of contents
- Install Material-UI
- Import the required Material-UI components.
- Define the table body.
Letβs start with the first step.
Step 1: Install Material-UI
Install the following packages to use align left mui table in react js.
npm
npm install @mui/material @emotion/react @emotion/styled
yarn
yarn add @mui/material @emotion/react @emotion/styled
Step 2: Import the required Material-UI components.
To align left mui table in React, first, you have to import the table. We have imported the table
, tableBody
, tableCell
, tableContainer
, tableHead
, tableRow
, and Paper
components from the @mui/material
library.
import React from "react";
import { makeStyles } from '@mui/styles';
import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper } from '@mui/material';
Step 3: Define the table body.
Table body is needed after table head, This code creates a table body with one row for each object in the rows array.
function createData(name, calories, fat, carbs, protein, food) {
return { name, calories, fat, carbs, protein, food };
}
const rows = [
createData("Frozen yoghurt", 159, 6.0, 24, 4.0, 0),
createData("Ice cream sandwich", 237, 9.0, 37, 4.3, 0),
createData("Eclair", 262, 16.0, 24, 6.0, 0),
createData("Cupcake", 305, 3.7, 67, 4.3, 0),
createData("Gingerbread", 356, 16.0, 49, 3.9, 0)
];
MUI material align left mui table example.
The below code is an example of a Material UI table. You have to import @mui material
table. In the Material Table function when you create a table component, it consists of a table head and a table body. In this code, we create our TableContainer component with a Paper component as its child and use style={{ display: 'flex', justifyContent: 'left' }}
to align left mui table in React js.
App.js
import React from "react";
import { makeStyles } from '@mui/styles';
import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper } from '@mui/material';
const useStyles = makeStyles({
table: {
minWidth: 650,
"& .MuiTableCell-root": {
borderRadius: "10px 0 0 10px"
}
}
});
function createData(name, calories, fat, carbs, protein, food) {
return { name, calories, fat, carbs, protein, food };
}
const rows = [
createData("Frozen yoghurt", 159, 6.0, 24, 4.0, 0),
createData("Ice cream sandwich", 237, 9.0, 37, 4.3, 0),
createData("Eclair", 262, 16.0, 24, 6.0, 0),
createData("Cupcake", 305, 3.7, 67, 4.3, 0),
createData("Gingerbread", 356, 16.0, 49, 3.9, 0)
];
export default function App() {
const classes = useStyles();
return (
<div style={{ display: 'flex', justifyContent: 'left' }}>
<TableContainer component={Paper} style={{ width: '50%' }}>
<Table className={classes.table} aria-label="simple table" >
<TableHead>
<TableRow>
<TableCell align="center" colSpan={5}>
MRF
</TableCell>
<TableCell align="center" colSpan={1}>
AD
</TableCell>
</TableRow>
<TableRow>
<TableCell>Paper</TableCell>
<TableCell align="center">Plastic</TableCell>
<TableCell align="center">Glass</TableCell>
<TableCell align="center">Metals</TableCell>
<TableCell align="center">Metals</TableCell>
<TableCell align="center">Food</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row) => (
<TableRow key={row.name}>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell align="center">{row.calories}</TableCell>
<TableCell align="center">{row.fat}</TableCell>
<TableCell align="center">{row.carbs}</TableCell>
<TableCell align="center">{row.protein}</TableCell>
<TableCell align="center">{row.food}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
</div>
);
}
In the above code example, I have used the @mui/material
component and align left mui table in react js.
Check the output of the above code example.
All the best π