Issue
The MUI grid example below seems to create an extra space to the right of the grid and makes it look like the grid is not occupying the whole space that is available on the page.
I have tried to change/remove the following margin values but it doesn't help:
.MuiGrid-spacing-xs-1 {
width: calc(100% + 8px);
margin: -4px;
}
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Paper from "@material-ui/core/Paper";
import Grid from "@material-ui/core/Grid";
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
backgroundColor: "lightblue"
},
paper: {
padding: theme.spacing(1),
textAlign: "center",
color: theme.palette.text.secondary
}
}));
export default function NestedGrid() {
const classes = useStyles();
function FormRow() {
return (
<React.Fragment>
<Grid item xs={4}>
<Paper className={classes.paper}>item</Paper>
</Grid>
<Grid item xs={4}>
<Paper className={classes.paper}>item</Paper>
</Grid>
<Grid item xs={4}>
<Paper className={classes.paper}>item</Paper>
</Grid>
</React.Fragment>
);
}
return (
<div className={classes.root}>
<Grid container spacing={1}>
<Grid container item xs={12} spacing={3}>
<FormRow />
</Grid>
<Grid container item xs={12} spacing={3}>
<FormRow />
</Grid>
<Grid container item xs={12} spacing={3}>
<FormRow />
</Grid>
</Grid>
</div>
);
}
Solution
The extra space on the right isn't caused by the styles you included in your question, it is caused by the default justify-content
of flex-start
for the outermost Grid. If you specify justify="center"
on the outermost Grid then the contents of the Grid will be centered.
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Paper from "@material-ui/core/Paper";
import Grid from "@material-ui/core/Grid";
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
backgroundColor: "lightblue"
},
paper: {
padding: theme.spacing(1),
textAlign: "center",
color: theme.palette.text.secondary
}
}));
export default function NestedGrid() {
const classes = useStyles();
function FormRow() {
return (
<React.Fragment>
<Grid item xs={4}>
<Paper className={classes.paper}>item</Paper>
</Grid>
<Grid item xs={4}>
<Paper className={classes.paper}>item</Paper>
</Grid>
<Grid item xs={4}>
<Paper className={classes.paper}>item</Paper>
</Grid>
</React.Fragment>
);
}
return (
<div className={classes.root}>
<Grid container spacing={1} justify="center">
<Grid container item xs={12} spacing={3}>
<FormRow />
</Grid>
<Grid container item xs={12} spacing={3}>
<FormRow />
</Grid>
<Grid container item xs={12} spacing={3}>
<FormRow />
</Grid>
</Grid>
</div>
);
}
Related resource: https://css-tricks.com/snippets/css/a-guide-to-flexbox/#justify-content
Answered By - Ryan Cogswell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.