Skip to content
Related Articles

Related Articles

How to use Grid Component in Material UI ?

Improve Article
Save Article
  • Last Updated : 08 Nov, 2022
Improve Article
Save Article

The Material Design responsive layout grid adapts to screen size and orientation, ensuring consistency across layouts. Material UI for React has this component available for us and it is very easy to integrate. We can the Grid component in ReactJS using the following approach.

Creating React Application And Installing Module:

Step 1: Create a React application using the following command:

npx create-react-app foldername

Step 2: After creating your project folder i.e. foldername, move to it using the following command:

cd foldername

Step 3: After creating the ReactJS application, Install the material-ui modules using the following command:

npm install @mui/material

Project Structure: It will look like the following.

Project Structure

Example 1: In this example, we will create a simple application that uses the Grid component to display Paper components in different sizes Please update the file App.js like below.

Filename: App.js

Javascript




import { createStyles, Grid, makeStyles, Paper } 
    from '@mui/material';
import React from 'react';
  
const useStyles = makeStyles((theme) =>
    createStyles({
        paper: {
            padding: theme.spacing(2),
            textAlign: 'center',
            color: theme.palette.text.secondary,
        },
        root: {
            flexGrow: 1,
        },
    }),
);
  
export default function FullWidthGrid() {
    const classes = useStyles();
  
    return (
        <div style={{
            width: '90%', backgroundColor: 'green',
            padding: '10px'
        }}>
            <Grid container spacing={3}>
                <Grid item xs={12}>
                    <Paper className={classes.paper}>
              GEEKSFORGEEKS ---> GRID COMPONENT DEMO
                    </Paper>
                </Grid>
                <Grid item xs={6} sm={3}>
                    <Paper className={classes.paper}>
                        1/4 Size
                    </Paper>
                </Grid>
                <Grid item xs={6} sm={3}>
                    <Paper className={classes.paper}>
                        1/4 Size
                    </Paper>
                </Grid>
                <Grid item xs={6} sm={3}>
                    <Paper className={classes.paper}>
                        1/4 Size
                    </Paper>
                </Grid>
                <Grid item xs={6} sm={3}>
                    <Paper className={classes.paper}>
                        1/4 Size
                    </Paper>
                </Grid>
                <Grid item xs={12} sm={6}>
                    <Paper className={classes.paper}>
                        1/2 Size
                    </Paper>
                </Grid>
                <Grid item xs={12} sm={6}>
                    <Paper className={classes.paper}>
                        1/2 Size
                    </Paper>
                </Grid>
                <Grid item xs={12}>
                    <Paper className={classes.paper}>
                        Full Size
                    </Paper>
                </Grid>
            </Grid>
        </div>
    );
}

Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Example 2: In this example, we will create a simple application that has 2 Grid items, and their spacing is controlled by a Switch component. Please update the file App.js like below.

Filename: App.js

Javascript




import { Grid, Paper, Switch } from '@mui/material';
import React, { useState } from 'react';
  
export default function FullWidthGrid() {
    const [spacing, setSpacing] = useState(2)
  
    return (
        <div>
            <Switch onChange=
            {() => setSpacing(spacing => spacing === 2 ? 4 : 2)} />
            Spacing {spacing}px
            <div>
                <Grid container spacing={spacing}>
                    <Grid item>
                        <Paper sx={{ 
                            height: 140, 
                            width: 100, 
                            border: '2px solid black' 
                        }}></Paper>
                    </Grid>
                    <Grid item>
                        <Paper sx={{ 
                            height: 140, 
                            width: 100,
                             border: '2px solid black' 
                        }}></Paper>
                    </Grid>
                </Grid>
            </div>
        </div>
    );
}

Steps to run the application:

npm start

Output:

 

Reference: https://mui.com/material-ui/react-grid/#main-content


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!