Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 36 ยท Layout and Grid

The Grid System

Build responsive page layouts with the Material UI Grid: container and item sizes on a 12-column system, breakpoint sizes, spacing, offsets, auto and grow, and nested grids.

Stack is for one row or column. When you want cards in three columns on desktop, two on tablets and one on phones, you need a two-dimensional layout. Material UI's Grid divides the available width into 12 columns and lets each child span as many as it needs at each breakpoint.

MUI changed Grid across versions. In v7 the standard Grid (imported from @mui/material/Grid) uses a size prop and CSS variables. In v6 the same new component was called Grid2 (@mui/material/Grid2), and v5's Grid used item xs={6}; the latter is now GridLegacy. This lesson teaches the current API and shows the old one in a note below so you can read older code.

The 12-column idea

A grid has a container and children. The container declares itself with the container prop; each child says how many of the 12 columns it takes with size:

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

<Grid container spacing={2}>
  <Grid size={8}>Wide (8 of 12)</Grid>
  <Grid size={4}>Narrow (4 of 12)</Grid>
  <Grid size={6}>Half</Grid>
  <Grid size={6}>Half</Grid>
</Grid>

Children that do not fit on the first row wrap to the next. 8 + 4 fills row one; 6 + 6 fills row two. spacing={2} puts 16px of gap between cells, both horizontally and vertically. There is no item prop in the new Grid: every direct child of a container is an item, and any Grid can be both container and item if it has both props.

Responsive sizes

size accepts a number or a breakpoint object. Sizes are mobile-first: a value applies from its breakpoint upward until another value takes over.

JSX
<Grid container spacing={{ xs: 2, md: 3 }}>
  {products.map((p) => (
    <Grid key={p.id} size={{ xs: 12, sm: 6, md: 4, lg: 3 }}>
      <ProductCard product={p} />
    </Grid>
  ))}
</Grid>

That reads: 1 card per row on phones (12/12), 2 per row from 600px, 3 from 900px, 4 from 1200px. This single pattern covers most card layouts.

The breakpoints are the same everywhere in MUI:

Key Starts at
xs 0px
sm 600px
md 900px
lg 1200px
xl 1536px

Stand-in: a 12-column grid

This stand-in Grid uses CSS grid with 12 columns and breakpoint sizes. Resize the preview (or the editor panel) and watch the cards re-flow. Real MUI does the same with its own CSS.

Stand-in: responsive 12-column Grid

Auto, grow and offsets

Besides numbers, size accepts two special values:

  • size="auto": the column is as wide as its content.
  • size="grow": the column takes the remaining space equally with other grow columns.
JSX
<Grid container spacing={2} alignItems="center">
  <Grid size="auto"><Avatar>A</Avatar></Grid>
  <Grid size="grow"><Typography noWrap>Takes all the remaining width</Typography></Grid>
  <Grid size="auto"><Button>Follow</Button></Grid>
</Grid>

To leave empty columns before an item, use offset:

JSX
<Grid container>
  <Grid size={6} offset={3}>Centered: 3 empty columns on each side</Grid>
  <Grid size={{ md: 4 }} offset={{ md: 'auto' }}>Pushed to the right on md+</Grid>
</Grid>

Alignment, order and columns

A Grid container accepts flex-style props that control items within rows: alignItems, justifyContent, direction, wrap. Use rowSpacing and columnSpacing when horizontal and vertical gaps differ, and columns to change the 12-column system (for example columns={5} for five equal tiles).

JSX
<Grid container columns={16} rowSpacing={1} columnSpacing={{ xs: 1, sm: 2, md: 3 }}>
  <Grid size={8}>half of 16 columns</Grid>
</Grid>

To reorder items at a breakpoint, use CSS order through sx:

JSX
<Grid size={{ xs: 12, md: 4 }} sx={{ order: { xs: 2, md: 1 } }}>Sidebar after content on phones</Grid>

Nested grids

A Grid can be an item and a container at once, so you can nest layouts. The nested grid's 12 columns refer to the width of its parent cell, not the page:

JSX
<Grid container spacing={2}>
  <Grid size={{ xs: 12, md: 8 }}>
    <Grid container spacing={2}>
      <Grid size={6}>Nested A</Grid>
      <Grid size={6}>Nested B</Grid>
    </Grid>
  </Grid>
  <Grid size={{ xs: 12, md: 4 }}>Side</Grid>
</Grid>

Nesting is fine for a level or two; deeper structures usually mean the layout should be simplified.

A dashboard layout

The second stand-in builds a dashboard row with auto, grow and sized cells, using CSS grid underneath. It shows how the 12-column vocabulary maps to a real screen.

Stand-in: dashboard layout with sizes and grow

Reading older Grid code

You will meet Grid code from older tutorials. Translate it like this:

Old (v5, now GridLegacy) Current (v7 Grid)
<Grid item xs={12} md={6}> <Grid size={{ xs: 12, md: 6 }}>
<Grid item xs> <Grid size="grow">
<Grid item xs="auto"> <Grid size="auto">
import Grid2 from '@mui/material/Grid2' import Grid from '@mui/material/Grid'

Do not mix the two in one file; they have different props and CSS.

When not to use Grid

  • Simple column or row with equal gaps: Stack.
  • Auto-fitting card galleries where you do not care about 12 columns: CSS grid with gridTemplateColumns: 'repeat(auto-fill, minmax(240px, 1fr))' in a Box is often simpler and needs no breakpoints.
  • Tabular data: use Table or the Data Grid component, not Grid.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the current MUI Grid, how many columns does the layout use by default?

    The default grid has 12 columns; the columns prop can change it.
  2. 2. What does size={{ xs: 12, md: 6 }} mean?

    Sizes are mobile-first: 12 columns from xs, then 6 columns from md (900px).
  3. 3. Which value makes a column take the remaining space?

    size='grow' shares the leftover space; size='auto' fits the content.
  4. 4. How do you center a column of size 6 in a 12-column container?

    offset={3} leaves three empty columns before it, so six columns remain centered.

Summary

  • Grid splits a container into 12 columns; children choose their span with size, per breakpoint if needed.
  • spacing, rowSpacing and columnSpacing control gaps; offset, auto and grow refine placement.
  • Grids can nest: a Grid may be both container and item.
  • v7 Grid replaces the old item xs md props with size; old code is GridLegacy.
  • For simple rows/columns use Stack; for auto-fit galleries consider CSS grid via Box.

Next: Responsive Design and Breakpoints