gpt4 book ai didi

reactjs - 如何在 React with Material-UI 中垂直和水平居中?

转载 作者:行者123 更新时间:2023-12-05 05:00:47 24 4
gpt4 key购买 nike

我想将位于屏幕中央的 Card 内的 Button 居中。到目前为止,我还不能。

这是我目前的情况:

import React from 'react'
import Button from '@material-ui/core/Button'
import { makeStyles } from '@material-ui/core/styles'
import { Card } from '@material-ui/core'
import Grid from '@material-ui/core/Grid'

const useStyles = makeStyles({
root: {
background: 'linear-gradient(45deg, #9013FE 15%, #50E3C2 90%)',
minWidth: '100%',
minHeight: '100vh',
display: "flex",
flexDirection: "column",
justifyContent: "center"
},
card: {
maxWidth: '40%',
minHeight: '20vh',
},
})

export default function LoginPage () {
const classes = useStyles()

return (
<Grid className={classes.root} spacing={0} align="center" justify="center">
<Card className={classes.card} align="center" justify="center">
<Button variant="contained" color="primary">
Hello World
</Button>
</Card>
</Grid>
)
}

它是垂直居中而不是水平居中,当我更改其他参数时我会使其水平居中而不是垂直居中。

Screenshot of the result

有什么想法吗?

最好的问候。

最佳答案

您可以使用alignItems

代码沙箱演示:https://codesandbox.io/s/stack-overflow-material-ui-centering-0rgqx

const useStyles = makeStyles({
root: {
background: "linear-gradient(45deg, #9013FE 15%, #50E3C2 90%)",
minWidth: "100%",
minHeight: "100vh",
display: "flex",
flexDirection: "column",
justifyContent: "center"
},
card: {
maxWidth: "40%",
minHeight: "20vh",
display: "flex",
alignItems: "center"
}
});

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

return (
<Grid
className={classes.root}
spacing={0}
alignItems="center"
justify="center"
>
<Card className={classes.card}>
<Button variant="contained" color="primary">
Hello World
</Button>
</Card>
</Grid>
);
}

关于reactjs - 如何在 React with Material-UI 中垂直和水平居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62975294/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com