Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to horizontally center an item in MUI Grid item?

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.

like image 483
Duncan Jones Avatar asked Sep 16 '18 19:09

Duncan Jones


People also ask

How do you horizontally center a grid item?

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.

How do you align items horizontally in MUI?

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.

How do you center something within a grid?

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.

How do you center elements in material UI?

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>.


2 Answers

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.

like image 55
Duncan Jones Avatar answered Sep 19 '22 22:09

Duncan Jones


 <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` 
like image 31
Bharath Mb Avatar answered Sep 21 '22 22:09

Bharath Mb