How do I center elements inside a MUI Grid
item? Here is a snippet from my React application:
<Grid container> <Grid item xs={4}> // Unrelated stuff here </Grid> <Grid item xs={4}> // How do I centre these items? <IconButton className={classes.menuButton} color="inherit"> <EditIcon/> </IconButton> <IconButton className={classes.menuButton} color="inherit"> <CheckBoxIcon/> </IconButton> </Grid> <Grid item xs={4}> // Unrelated stuff here </Grid> </Grid>
I've tried applying alignContent
, justify
, alignItems
(to the parent <Grid item>
) with no success.
I thought this would be quite simple, but I've failed to find any information on centering items inside of grid items.
Solution with the CSS text-align propertyUse the text-align property with the "center" value to center the content of grid items horizontally. Apply it to the grid container.
Material-UI Grid Align Right If you need to horizontally align using only CSS, remember that justify-content horizontally aligns using the following values: flex-start: horizontally aligns left. center: horizontally aligns center. flex-end: horizontally aligns right.
Use margin: auto to vertically and horizontally center grid items. To center the content of grid items you need to make the item into a grid (or flex) container, wrap anonymous items in their own elements (since they cannot be directly targeted by CSS), and apply the margins to the new elements.
There are two styling methods in Material-UI. The first is using “className”, the second is using <Box> components. In my opinion, “className” is a good when using container-based components like <Grid> <Paper> <Card> <Container>.
Two seconds later... I solved this through some simple CSS:
<Grid item xs={4} style={{textAlign: "center"}}> </Grid>
If there is another approach that is somehow more "correct", please feel free to post another answer.
<Grid container className = {classes.root} align = "center" justify = "center" alignItems = "center" > <CssBaseline/> <Grid item xs = {false} sm = {4} md = {6}> </Grid> <Grid item xs = {12} sm = {4} md = {6}> </Grid> </Grid>`enter code here`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With