Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align material-ui Grid items vertically centered?

I have a Grid container and Buttons as it's children (Grid-items). I want to align Grid-items vertically centered.

Here is the visual representation of my requirement

enter image description here

Here is the markup

<Box height="10vh" mr={4}>
  <Grid container justify="flex-end" spacing={2}>
    <Button variant="contained" color="default" type="reset">
      Reset
    </Button>
    <Button
      type="submit"
      variant="contained"
      color="primary"
      onClick={() => handleSubmit()}
    >
      Search
    </Button>
  </Grid>
</Box>;

Can anybody tell me a solution based on material-ui grid API?

like image 539
DevLoverUmar Avatar asked Mar 10 '26 09:03

DevLoverUmar


2 Answers

I figured out, this should work,

const useStyles = makeStyles({
  grid: {
    height: "100%"
  }
});

export default function Hook() {
  const classes = useStyles();

  return (
    <Box height="10vh" mr={4}>
      <Grid
        className={classes.grid}
        container
        justify="flex-end"
        alignItems="center"
        spacing={2}
      >
        <Button variant="contained" color="default" type="reset">
          Reset
        </Button>
        <Button
          type="submit"
          variant="contained"
          color="primary"
          onClick={console.log}
        >
          Search
        </Button>
      </Grid>
    </Box>
  );
}

Working sandbox, https://codesandbox.io/s/material-demo-forked-m7fyj?file=/demo.js

Working example image

like image 110
aeXuser264 Avatar answered Mar 11 '26 23:03

aeXuser264


<Grid
  container
  direction="row"
  justifyContent="flex-end"
  alignItems="center"
>

vertically centered

I recommend you to try the Interactive Demo

like image 27
Amr Avatar answered Mar 12 '26 00:03

Amr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!