Friday, September 5, 2025
HomeLanguagesJavascriptHow to use Grid Component in Material UI ?

How to use Grid Component in Material UI ?

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS