Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 36 ยท Layout and Grid

Box and Container

Use Material UI Box as a utility wrapper and Container to center content with maxWidth and gutters. Learn spacing, display, flex props and page shells.

Layout in Material UI starts with two humble components: Box and Container. Box is a div that understands sx, so you can add spacing, color and flexbox without writing CSS. Container centers your content and limits its width so text does not stretch across a wide monitor. Together they build the skeleton of almost every page.

Box

Box renders a <div> by default. It has no styles of its own; everything comes from sx (and from the component prop if you want another tag). Real MUI code (read-only):

JSX
import Box from '@mui/material/Box';

<Box
  component="section"
  sx={{
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'space-between',
    gap: 2,
    p: 2,
    bgcolor: 'background.paper',
    border: 1,
    borderColor: 'divider',
    borderRadius: 2,
  }}
>
  <Typography>Left</Typography>
  <Button>Right</Button>
</Box>

A few conveniences beyond plain CSS:

  • border: 1 means 1px solid and borderColor: 'divider' uses the theme's divider color.
  • gap: 2 is 16px because gap uses the theme spacing.
  • You can pass system props directly (<Box p={2} display="flex">), but this older style is being phased out; prefer sx.

Think of Box as "a div with theme-aware CSS". When a plain element is all you need and you want theme values, reach for Box. When you need structure with defaults (a card, a stack of items), prefer the specific component.

Flexbox with Box

Because sx accepts any CSS property, Box is a convenient way to use flexbox and CSS grid:

JSX
<Box sx={{ display: 'flex', flexDirection: { xs: 'column', md: 'row' }, gap: 3 }}>
  <Box sx={{ flex: 1 }}>Main</Box>
  <Box sx={{ width: { md: 280 } }}>Sidebar</Box>
</Box>

<Box sx={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 2 }}>
  <Item /> <Item /> <Item />
</Box>

Later in this chapter you will meet Stack and Grid, which wrap the most common flex and grid patterns with shorter props.

Stand-in: Box as a utility wrapper

This runnable example is a stand-in Box with a small sx subset (display, flex, gap, padding, bgcolor, responsive direction). Change values to see the effect.

Stand-in: Box with flex and spacing

Container

On a wide screen, a paragraph that spans 1900 pixels is unreadable. Container centers its children horizontally and sets a maximum width that depends on the breakpoint. It also adds horizontal padding (gutters) on small screens.

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

<Container maxWidth="md">
  <Typography variant="h4" component="h1">Article</Typography>
  <Typography>Readable line length, centered on the page.</Typography>
</Container>

maxWidth accepts xs (444px), sm (600px), md (900px), lg (1200px, the default), xl (1536px) or false (no maximum). Add fixed to use the exact breakpoint widths instead of fluid scaling, and disableGutters to remove the side padding (for full-bleed children like images).

A typical page structure:

JSX
<Box sx={{ minHeight: '100vh', display: 'flex', flexDirection: 'column' }}>
  <AppBar position="static">...</AppBar>
  <Container component="main" maxWidth="lg" sx={{ flex: 1, py: 4 }}>
    {children}
  </Container>
  <Box component="footer" sx={{ py: 2, textAlign: 'center' }}>ยฉ My company</Box>
</Box>

The flex: 1 on the main container pushes the footer to the bottom even when content is short. This "sticky footer" pattern is a classic use of Box plus Container.

A page skeleton

AppBar Top navigation
Container Centered main content
Footer Box Sticks to the bottom

Box with minHeight 100vh and flex column ties them together.

1 / 2

Stand-in: a Container with maxWidth

This stand-in shows how a container behaves: choose a maxWidth and resize the preview. The content stays centered and never grows past the limit.

Stand-in: Container maxWidth and gutters

Common mistakes

  • Nesting Container inside Container; it doubles the gutters. Use one per page section.
  • Using Box for everything; a Stack or Grid states the intent better and needs less CSS.
  • Setting fixed pixel widths on Box instead of percentages or breakpoint objects, which breaks on phones.
  • Forgetting component="main" or component="section" so the page has no landmarks for assistive technology.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Box render by default?

    Box renders a div unless you change it with the component prop.
  2. 2. What is the default maxWidth of Container?

    The default is lg (1200px). Set maxWidth to another breakpoint or false to change it.
  3. 3. How do you remove the horizontal padding of a Container?

    disableGutters removes the left and right padding so children can be full-bleed.
  4. 4. In sx, what does gap: 2 produce?

    Like margin and padding, gap uses the theme spacing scale (8px by default).

Summary

  • Box is a theme-aware div; style it with sx, change its tag with component.
  • It is a quick way to use flexbox and CSS grid with spacing and palette shortcuts.
  • Container centers content and limits width via maxWidth; disableGutters and fixed adjust it.
  • Combine Box (flex column, minHeight) and Container for a page skeleton with a sticky footer.

Next: Stack and Spacing