Open In App

How to use Grid Component in Material UI ?

Last Updated : 08 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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



Similar Reads

How to use grid element using the grid auto placement rules?
In this article, you will learn to use grid elements using the grid auto-placement rules. You can use auto as a property value on the grid to auto-enable the container to customize its size on the basis of the content of the items in the row or columns. Syntax: grid-template-rows : auto grid-template-columns: auto Property Value: auto : The size of
2 min read
How to use Radio Component in Material UI ?
Radio buttons allow the user to select one option from a set. Material UI for React has this component available for us and it is very easy to integrate. We can use Radio 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-rea
2 min read
How to use Fab Component in Material UI ?
Fab stands for Floating Action Button is which appears in front of all screen content, typically as a circular shape with an icon in its center. Material UI for React has this component available for us and it is very easy to integrate. It can be used to turn an option on or off. We can use the Fab Component in ReactJS using the following approach:
2 min read
How to use Button Component in Material UI ?
Buttons allow users to take actions, and make choices, with a single tap. Material UI for React has this component available for us and it is very easy to integrate. We can use the Button component in ReactJS using the following approach. Creating React Application And Installing Module: Step 1: Create a React application using the following comman
2 min read
How to use Slider Component in Material UI ?
Sliders allow users to make selections from a range of values. Material UI for React has this component available for us and it is very easy to integrate. We can use the Slider Component in ReactJS using the following approach: Creating React Application And Installing Module: Step 1: Create a React application using the following command: npx crea
3 min read
How to use Select Component in Material UI ?
Select components are used for collecting user-provided information from a list of options. Material UI for React has this component available for us and it is very easy to integrate. We can use the Select Component in ReactJS using the following approach: Creating React Application And Installing Module: Step 1: Create a React application using th
3 min read
How to use AppBar Component in Material UI ?
Using AppBar component in Material UI provides a flexible and customizable navigation bar for your web applications. The App Bar displays information and actions relating to the current screen. Material UI for React has this component available for us and it is very easy to integrate. PrerequisitesReact JSMaterial UIApproachTo use AppBar Component
3 min read
How to use TextField Component in Material UI ?
TextField component is a complete form control including a label, input, and help text. Material UI for React has this component available for us and it is very easy to integrate. PrerequisitesKnowledge of ReactJSJavaScript (ES6+)MUI BasicsSteps to create React Application And Installing Module:Step 1: Create a React application using the following
2 min read
How to use Breadcrumbs Component in Material UI ?
Breadcrumbs are list of links that display the page location within a website or application. They allow users to make selections from a range of values. Material UI for React has this component available for us, and it is very easy to integrate. In this article we will see how we can integrate and implement it. PrerequisitesKnowledge of ReactMater
2 min read
How to use Box Component in Material UI ?
The Material UI Box component serves as a wrapper component for most of the CSS utility needs. Material UI for React has this component available for us and it is very easy to integrate. We can use the Box component in ReactJS using the following ways. Prerequisites to use MUI Box ComponentReact JSReact Environment SetupMaterial UI Introduction and
2 min read