【问题标题】:Putting Card in Center of The Page in React在 React 中将卡片放在页面的中心
【发布时间】:2020-10-29 04:16:14
【问题描述】:

我有一个简单的问题,就是将卡片放在页面中心。我已经输入了alignItems: centerjustifyContent: center,但它仍然没有显示在中心页面上。

检查此代码框链接CLICK HERE

代码

const useStyles = makeStyles(() => ({
  root: {
    minWidth: 275,
    justifyContent: "center",
    alignItems: "center"
  },
  cardHeader: {
    backgroundColor: "blue",
    color: "white",
    display: "flex",
    justifyContent: "center",
    alignItems: "center"
  }
}));

【问题讨论】:

    标签: css reactjs flexbox jss


    【解决方案1】:

    您可以在父级上使用 flex,注意您需要设置 height 以便它会相应地居中。

    const useStyles = makeStyles(() => ({
      root: {
        display: "flex",
        justifyContent: "center",
        alignItems: "center",
        height: "100vh"
      },
      cardHeader: {/*...*/}
    }));
    

    【讨论】:

    • 你能解释一下为什么高度是100vh吗?这是什么意思?可以是100+...。谢谢
    • developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/…, 1vh = 视口高度的 1%。它只是意味着“占据所有屏幕”,你可以使用任何你喜欢的height,它只是一个例子,你必须添加高度否则将采用默认值:auto
    • 我在我的应用程序中尝试过,当我将高度设置为100vh 时出现滚动条。它的理想高度是多少?谢谢
    • 没有理想的东西,这取决于,100% 可能有用...
    猜你喜欢
    • 2015-03-01
    • 1970-01-01
    • 2020-01-27
    • 2014-11-18
    • 2017-02-15
    • 2011-02-25
    • 1970-01-01
    • 2022-01-09
    • 2012-03-13
    相关资源
    最近更新 更多