I am rendering some cards in a container row and need to align them in the center with equal spacing around.
I am using UI library material-ui for the layout. Even though I have added the justifyContent: center
property, the cards have uneven spacing around them.
This is what the current UI looks like:
Notice the extra space on the right side of the last card. On inspecting, the spacing scale looks like this:
Here is the code so far:
const Home = ({ cards }) => {
return (
<Container maxWidth="xl">
<Grid
container
justifyContent="center"
spacing={3}
my={8}
>
{cards.map((card) => {
return (
<Grid item xs={12} sm={6} md={3}>
<Card sx={{ maxWidth: 300 }}>
<CardActionArea>
<CardMedia
component="img"
height="140"
image="../../bg2.png"
alt="green iguana"
/>
<CardContent>
<Typography gutterBottom variant="h5" component="div">
{card.title}
</Typography>
<Typography variant="body2" color="text.secondary">
{card.description}
</Typography>
</CardContent>
</CardActionArea>
<CardActions>
<Button size="small" color="primary">
View More
</Button>
</CardActions>
</Card>
</Grid>
);
})}
</Grid>
</Container>
);
};
If I remove the container wrapper <Container maxWidth="xl">
, then the UI looks like this:
I am not a very good hand at MUI, If anyone can help to rectify the issue and achieve desired results, will be really helpful.