Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 36 ยท Layout and Grid

Stack and Spacing

Arrange items in one dimension with Material UI Stack: direction, spacing, dividers, alignment, responsive direction and how it differs from Box and Grid.

Most layouts are rows or columns of things with a consistent gap between them. Stack is MUI's component for exactly that: a flexbox container that lays out its children in one direction and spaces them evenly. It replaces dozens of margin rules with a single spacing prop.

Basic usage

By default a Stack lays children out in a column. Real MUI code (read-only):

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

<Stack spacing={2}>
  <TextField label="Name" />
  <TextField label="Email" />
  <Button variant="contained">Submit</Button>
</Stack>

spacing={2} puts 16px (2 times the 8px theme spacing) between each pair of children. There is no margin before the first or after the last child, so a stack never adds unwanted outer space. Under the hood, current MUI versions use the CSS gap property (older versions used margins on children).

Switch to a row with direction:

JSX
<Stack direction="row" spacing={1}>
  <Chip label="React" />
  <Chip label="MUI" />
  <Chip label="Vite" />
</Stack>

Values are row, row-reverse, column and column-reverse.

Alignment and distribution

Stack is flexbox, so the usual flex properties are available as system props or via sx:

JSX
<Stack direction="row" spacing={2} alignItems="center" justifyContent="space-between">
  <Typography variant="h6">Settings</Typography>
  <Button>Save</Button>
</Stack>

<Stack direction="row" useFlexGap sx={{ flexWrap: 'wrap', gap: 1 }}>
  {tags.map((t) => <Chip key={t} label={t} />)}
</Stack>
  • alignItems (cross axis): flex-start, center, flex-end, stretch.
  • justifyContent (main axis): flex-start, center, space-between, space-around, and so on.
  • To let a row wrap, set flexWrap: 'wrap' and use useFlexGap (in versions where spacing is margin-based) so the gaps work in wrapped rows too. In recent versions, gap handles wrapping correctly.

Responsive direction and spacing

Both direction and spacing accept breakpoint objects, which makes "column on phones, row on desktops" one line:

JSX
<Stack direction={{ xs: 'column', sm: 'row' }} spacing={{ xs: 1, sm: 2, md: 4 }}>
  <Card />
  <Card />
  <Card />
</Stack>

Stand-in: Stack and its props

The first runnable example is a stand-in Stack that implements direction, spacing, alignItems, justifyContent and divider with CSS gap. Try the controls.

Stand-in: Stack direction, spacing and alignment

Dividers between items

Pass a divider element and Stack inserts it between every pair of children:

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

<Stack direction="row" spacing={2} divider={<Divider orientation="vertical" flexItem />}>
  <Typography>Home</Typography>
  <Typography>About</Typography>
  <Typography>Contact</Typography>
</Stack>

For a row, the divider must be orientation="vertical" and flexItem so it stretches to the row's height.

Stack versus Box and Grid

Need Use
One row or column with even gaps, any alignment Stack
A custom flex or grid layout once Box with sx
Items that must line up in columns at breakpoints (cards in a page grid) Grid
Wrapping tag lists Stack direction="row" with wrap, or Box with flexWrap

Stack is one-dimensional. When items should line up in rows and columns both, move to Grid or CSS grid.

Spacing without extra wrappers

Because Stack adds the gap itself, you can drop margin from children entirely. That makes children reusable: a Card with mb: 2 looks wrong at the end of a list; a Card inside a Stack spacing={2} works anywhere. Prefer "the parent decides spacing".

Stand-in: a settings form with the parent deciding spacing

Common mistakes

  • Putting a Typography margin or mb on children of a Stack; you then get double spacing.
  • Using Stack for a two-dimensional card grid; use Grid.
  • Forgetting that spacing with objects needs breakpoint keys, not arrays of strings.
  • Wrapping a conditional child in a fragment: Stack counts children; a false child is skipped, but a Fragment containing several items counts as one child for the divider logic.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the default direction of Stack?

    Stack is a column by default. Use direction='row' for a horizontal layout.
  2. 2. How do you make a Stack a column on phones and a row from the sm breakpoint?

    direction accepts a breakpoint object, so one prop describes both layouts.
  3. 3. What does the divider prop of Stack do?

    Stack renders your divider element between children, typically a Divider (vertical with flexItem for rows).
  4. 4. When is Grid a better choice than Stack?

    Stack is one-dimensional. Grid handles two-dimensional, breakpoint-aware layouts.

Summary

  • Stack lays children in a column (default) or row with consistent gaps via spacing.
  • It is flexbox: use alignItems, justifyContent, and wrapping with flexWrap.
  • direction and spacing accept breakpoint objects for responsive layouts.
  • divider inserts an element between children.
  • Let the parent decide spacing; keep margins off the children.

Next: The Grid System