How to set max width of mui table in react js?

Hi Friends πŸ‘‹,

Welcome To aGuideHub!

To set max width of mui table in React js, you can use maxWidth: 650 in Table. It will set max width of mui table in react js.

Today, I am going to show you, how to set max width of 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 set max width of 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 set max width of 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.

<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>

MUI material set max width of 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 maxWidth: 650 to set max width of mui table in react js.

App.js

import React from "react";
import { makeStyles } from '@mui/styles';
import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow } from '@mui/material';

const useStyles = makeStyles({
  table: {
    maxWidth: 650,
    "& .MuiTableCell-root": {
       border: "1px solid black",
       color: "blue"
    } 
  }
});

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 (
    <TableContainer  className={classes.table}>
      <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>
  );
}

In the above code example, I have used the @mui/material component and set max width of mui table in react js.

Check the output of the above code example.

React, max, width, of, mui, table

All the best πŸ‘

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts