Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MUI: How to prevent Grid items from going on next row?

I am using MUI Grid structure in ReactJs to display 5 columns like this:

enter image description here

In smaller screens the columns are moving in the next row. But In smaller screens I also want all the columns to show in one row only and I can access other columns by scrolling horizontally.

<Grid
  container
  spacing={2}
  style={{
    maxHeight: "100vh",
    overflowY: "auto",
    overflowX: "hidden",
    height: "440px",
    overflow: "auto",
  }}
>
  <Grid item xs={2.1}>
    {cards.map((card) => <Card props={card} /> )}
  </Grid>
</Grid>
like image 381
Ankit Jaishwal Avatar asked Mar 01 '23 16:03

Ankit Jaishwal


1 Answers

Because Grid uses flexbox under the hood. You can simply set the wrap value to nowrap in your Grid container, you can see the full API of Grid component here.

<Grid
  container
  wrap="nowrap" // --> add this line to disable wrap
  sx={{ overflow: "auto" }} // --> add scrollbar when the content inside is overflowed
  spacing={8}
>
  {...}
</Grid>

Live Demo

Edit 66944361/how-to-prevent-columns-to-go-on-next-row-in-material-ui-grid-structure

like image 82
NearHuscarl Avatar answered Mar 12 '23 09:03

NearHuscarl