Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize material-ui Grid into rows?

I'm using material-ui to do a form, using the Grid system I'd like to do the following:

<Grid container>     <Grid item xs={4} />     <Grid item xs={4} />     <Grid item xs={4} /> </Grid> 

And be able to put the first 2 items, on the first row and the third on a second row, the only way I found to do it is:

<Grid container>     <Grid item xs={4} />     <Grid item xs={4} /> </Grid> <Grid container>     <Grid item xs={4} /> </Grid> 

What is the better way to stack material-ui Grid into rows (like the row class concept in Bootstrap grid)?

I also thought about these options:

  • filling the first row with empty Grid item?

  • putting vertical container?

like image 672
Gregory Arouche Avatar asked May 30 '18 17:05

Gregory Arouche


People also ask

How do you align grid items in material UI?

Material-UI Grid Center Align and Vertical Align However, if you wanted to achieve vertical alignment using CSS, you would want to use align items with one of the following values: flex-start: vertically aligns top. center: vertically aligns center. flex-end: vertically aligns bottom.

How do you use offset in material UI grid?

Just use a box with margin (mx) or padding px where x is horizontal. In my opinion this is better than introducing more css classes and properties that need maintenance. px gives padding in pixels, whereas offset is used in terms of grid columns.

Can material UI grid be container and an item?

Material Design's grid system is implemented in MUI using the <Grid /> component. Under the hood, the <Grid /> component uses Flexbox properties for greater flexibility. There are two types of grid components: containers and items.


2 Answers

You are close with the second block of code.

I found that you could simply create 2 distinct Grid sections such as:

<div>   <Grid id="top-row" container spacing={24}>     <Grid item xs={4}>       <Paper className={classes.paper}>Grid cell 1, 1</Paper>     </Grid>     <Grid item xs={4}>       <Paper className={classes.paper}>Grid cell 2, 1</Paper>     </Grid>   </Grid>   <Grid id="bottom-row" container spacing={24}>     <Grid item xs={4}>       <Paper className={classes.paper}>Grid cell 1, 2</Paper>     </Grid>     <Grid item xs={4}>       <Paper className={classes.paper}>Grid cell 2, 2</Paper>     </Grid>   </Grid> </div> 

You can play with it in my sandbox

It might also be work checking out the official documentation for Grid, as it shows a few ways to use it and also links to each exapmle hosted on codesandbox.io so you can play with it yourself.

From the docs, it seems the best way to have multi-layered grid systems is to define the width of the overall grid and then to define the width of each cell, as this will push cells later in the series onto the other rows.

like image 67
physicsboy Avatar answered Oct 04 '22 07:10

physicsboy


Here's another approach you could take which does not require two grid containers. In Material UI you can organise a grid into rows by using the fact that a row has 12 columns and making use of empty grid items (as Material UI does not support offset grid items) e.g.

<Grid container spacing={24}>     <Grid item xs={4}>       <Paper className={classes.paper}>Grid cell 1, 1</Paper>     </Grid>     <Grid item xs={4}>       <Paper className={classes.paper}>Grid cell 2, 1</Paper>     </Grid>     <Grid item xs={4} />     {/* 12 Columns used, so next grid items will be the next row  */}     {/* This works because Material UI uses Flex Box and flex-wrap by default */}     <Grid item xs={4}>       <Paper className={classes.paper}>Grid cell 1, 2</Paper>     </Grid>     <Grid item xs={8} />   </Grid> 

You can see this working here.

Indeed this is actually demonstrated on the Material UI websites (though without the offsets) here.

I admit though that I'd prefer to see semantic Row and Column elements in Material UI (e.g. like in Bootstrap), but this is not how it works.

like image 44
Ben Smith Avatar answered Oct 04 '22 06:10

Ben Smith