I am just learning Material UI with react. Starting with V5. I have a basic 12 column grid just to learn this. The spacing is just not working properly. It is just creating a weird padding on the grid items where the items themselves are getting internal padding and I don't see the gutters.
I tested it on the most basic example to ensure its as simple to see:
Here is the code:
//MuiGrid.js
import React from 'react'
import {Grid, Typography, Box} from '@mui/material';
export default function MuiGrid() {
return (
<Box>
<Typography variant='h2'>MUI Grid!</Typography>
{/* Testing Grid Spacing */}
<Box component='section'>
<Typography variant='h5'>Testing Spacing</Typography>
<Box>
<Grid container spacing={2}>
<Grid item sx={{backgroundColor: 'primary.dark'}}>Item 1</Grid>
<Grid item sx={{backgroundColor: 'primary.main'}}>Item 2</Grid>
<Grid item sx={{backgroundColor: 'primary.light'}}>Item 3</Grid>
</Grid>
</Box>
</Box>
</Box>
)
}
I have managed to reproduce this with my first sandbox. Here is the link: https://codesandbox.io/s/quirky-lake-50pqf5?file=/src/Demo.tsx
Here are the screenshots before & after spacing is applied, it shows just odd padding:
All the documentation is very vague and only shows basic information & I can't see what I have done wrong here
Edit 2:
I tried to make spacing 0 and add padding as suggested by the answer, that works well to provide padding but I can't get gutters
See code:
<Box component='section'>
<Typography variant='h5'>Testing Spacing</Typography>
<Box>
<Grid container spacing={0}>
<Grid p={1} item sx={{backgroundColor: 'primary.dark'}}> <Box>Item 1</Box></Grid>
<Grid p={1} item sx={{backgroundColor: 'primary.main'}}><Box>Item 2</Box></Grid>
<Grid p={1} item sx={{backgroundColor: 'primary.light'}}><Box>Item 3</Box></Grid>
</Grid>
</Box>
</Box>
Result:
Also here is the link to GitHub issue I have created: https://github.com/mui/material-ui/issues/31244
I have added background to the container and coloured the box inside the items, this provides a better idea about what is happening:
<Box component='section'>
<Typography variant='h5'>Testing Spacing</Typography>
<Box>
<Grid container spacing={2}
sx={{backgroundColor: 'secondary.main'}}
>
<Grid item> <Box sx={{backgroundColor: 'primary.dark'}}>Item 1</Box></Grid>
<Grid item><Box sx={{backgroundColor: 'primary.main'}}>Item 2</Box></Grid>
<Grid item><Box sx={{backgroundColor: 'primary.light'}}>Item 3</Box></Grid>
</Grid>
</Box>
</Box>
Result:



