Friday, October 24, 2025
HomeLanguagesReact MUI Container API

React MUI Container API

Material-UI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based on top of Material Design by Google. In this article let’s discuss the Container API in the Material-UI library.

The container is used to center content horizontally. It’s the most basic layout element. In Material UI we have a <Container /> component used for several purposes through its props.

Props List:

  • classes: It is used to add or override the styles of the component.
  • component: It is mainly used for the root node.
  • disableGutters: The padding is removed from the left and right if it is set to true.
  • fixed: It tries to set the max-width to the min-width of the current breakpoint.
  • maxWidth: It is used to set the max-width of the container because the container width increases with the size of the screen. You can set it to false to disable maxWidth.
  • sx: It can be used to override the system and additional CSS styles also. 

 

Fluid Container: A fluid container width is bounded by the maxWidth prop value.

Syntax:

<Container>This is container</Container>

Installing React App:

Step1: Create a React app using the following command.

npx create-react-app container-example

Step 2: Now get into the project directory.

cd container-example

Installing Material-UI: Installing Material-UI’s source files via npm/yarn, and they take care of injecting the CSS needed.

npm install @material-ui/core
OR
yarn add @material-ui/core

Project Structure:

project structure

Importing Container:

import Container from '@mui/material/Container';

Example 1: In this example, we will see the container width grows with the size of the screen. 

Javascript




import * as React from 'react';
import CssBaseline from '@mui/material/CssBaseline';
import Box from '@mui/material/Box';
import Container from '@mui/material/Container';
  
export default function SimpleContainer() {
    return (
        <React.Fragment>
            <CssBaseline />
            <Container maxWidth="sm">
                <Box sx={{ bgcolor: '#ffe8dc'
                    height: '100vh' }} >
                    <h1>GFG Container API</h1>
                    <h2>Fluid Container</h2>
                </Box>
            </Container>
        </React.Fragment>
    );
}


Output:

 

Fixed Container: We use this when we want to design for a fixed set of sizes.

Example 2: In this example, we will set the max-width to match the min-width of the current breakpoint. 

Javascript




import * as React from 'react';
import CssBaseline from '@mui/material/CssBaseline';
import Box from '@mui/material/Box';
import Container from '@mui/material/Container';
  
export default function FixedContainer() {
    return (
        <React.Fragment>
            <CssBaseline />
            <Container fixed>
                <Box sx={{ bgcolor: '#cfe8fc', height: '100vh' }} >
                    <h1>GFG Container API</h1>
                    <h2>Fixed Container</h2>
                </Box>
            </Container>
        </React.Fragment>
    );
}


Output:

 

Reference: https://mui.com/material-ui/api/container/ 

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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS