I am quite new to React and Frontend in general. I am trying to set up a MUI Grid, which lists the items in a column until it wraps around to start a new column. So far, so good, I can achieve that by setting the direction and flexWrap props.
Now the issue I am facing is that one of the items is a nested Grid container, and the column in which this item ends up, is wider than the others.
From the MUI docs or anywhere else I can't find a way to adjust column width once direction is set to 'column'.
Does anyone have an idea on how I can achieve all columns to be the same width? I'll include an example:
import React from "react";
import ReactDOM from "react-dom";
import { Box, styled } from "@mui/material";
import Grid from "@mui/material/Grid2";
const FormField = styled(Box)(({ theme }) => ({
marginBottom: theme.spacing(1.5),
borderRadius: 1,
minHeight: "200px",
backgroundColor: "yellowgreen",
}));
const newGrid = (
<div>
<Grid
container
spacing={2}
direction={"column"}
flexWrap={"wrap"}
maxHeight={"75vh"}
>
<Grid>
<FormField>Item 1</FormField>
</Grid>
<Grid>
<FormField>Item 2</FormField>
</Grid>
<Grid>
<FormField>Item 3</FormField>
</Grid>
<Grid>
<FormField>Item 4</FormField>
</Grid>
<Grid>
<FormField>Item 5</FormField>
</Grid>
<Grid container spacing={2}>
<Grid size={2}>
<FormField>Nest 1</FormField>
</Grid>
<Grid size={5}>
<FormField>Nest 2</FormField>
</Grid>
<Grid size={5}>
<FormField>Nest 3</FormField>
</Grid>
</Grid>
<Grid>
<FormField>Item 6</FormField>
</Grid>
<Grid>
<FormField>Item 7</FormField>
</Grid>
</Grid>
</div>
);
function App() {
return newGrid;
}
ReactDOM.render(<App />, document.querySelector("#app"));
This results in something like this depending on window size: grid with uneven columns How can I make the columns the same width?
I tried different props like AlignItems or JustifyContent, but while using direction='column' there was little to no effect. I tried the deprecated Grid (in contrast to Grid 2), but the problem was the same there. I expected same width columns, but if there is a nested Grid container, this column will be wider. It is more noticable on smaller screens.