【问题标题】:How to align material-ui Grid items vertically centered?如何对齐material-ui Grid项目垂直居中?
【发布时间】:2020-10-07 08:39:47
【问题描述】:

我有一个网格容器和按钮作为它的子项(网格项)。我想将网格项目垂直居中对齐。

这是我的需求的可视化表示

这是标记

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

谁能告诉我基于material-ui grid API的解决方案?

【问题讨论】:

  • in css align-items/content 应该这样做
  • @aeXuser264 你能告诉我一个基于 React Material-ui 库 API 的解决方案吗?
  • 您是否尝试过 Material UI 文档中的交互式演示?

标签: reactjs material-ui


【解决方案1】:

我发现,这应该可以,

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>
  );
}

工作沙盒,https://codesandbox.io/s/material-demo-forked-m7fyj?file=/demo.js

【讨论】:

  • 感谢您的意见,但我想避免像 style={{height:"100&"}} 这样的内联样式。
【解决方案2】:
<Grid
  container
  direction="row"
  justify="flex-end"
  alignItems="center"
>

我建议你试试Interactive Demo

【讨论】:

  • 好,我试试
  • 感谢您的意见,但它不适用于我的用例。我认为包裹网格的盒子正在制造问题。我必须找到一种方法从父级继承网格的宽度/高度,因为带有克隆的框不起作用
猜你喜欢
  • 2020-08-17
  • 2022-01-11
  • 2019-10-17
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 2017-07-18
相关资源
最近更新 更多