reactjs 使用Material UI Paper垂直对齐

new9mtju  于 2023-05-22  发布在  React
关注(0)|答案(2)|浏览(169)

我想垂直对齐Material UI Paper组件中的一些文本。
代码是here

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
import Typography from '@material-ui/core/Typography';

const useStyles = makeStyles(theme => ({
  root: {
    padding: theme.spacing(3, 2),
    height: 200,
    verticalAlign: 'middle'
  },
}));

function PaperSheet() {
  const classes = useStyles();

  return (
    <div>
      <Paper className={classes.root}>
        <Typography variant="h5" component="h3">
          This is a sheet of paper.
        </Typography>
        <Typography component="p">
          Paper can be used to build surface or other elements for your application.
        </Typography>
      </Paper>
    </div>
  );
}

export default PaperSheet;
nwlqm0z1

nwlqm0z11#

vertical-align CSS属性仅适用于display: block元素。
你可以选择使用flexbox声明你的root类:

const useStyles = makeStyles(theme => ({
  root: {
    padding: theme.spacing(3, 2),
    height: 200,
    display: "flex",
    flexDirection: "column",
    justifyContent: "center"
  },
}));
rqdpfwrv

rqdpfwrv2#

Stack可以在MUI v5中使用。将方向设置为columnjustifyContent,使Card内的内容居中对齐:

<Paper component={Stack} direction="column" justifyContent="center">
  <div>
    This content is vertically aligned
  </div>
</Paper>

现场演示

相关问题