【问题标题】:Material-UI Paper component does not center the textMaterial-UI Paper 组件不居中文本
【发布时间】:2019-08-28 08:07:18
【问题描述】:

我从 React Material-UI 开始,如果高度较低,纸张组件不会使文本居中。有人可以帮忙吗?

下面有一个沙盒的链接,可以看到文字太靠下,不在中间。

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
import Typography from '@material-ui/core/Typography';

const styles = theme => ({
  root: {
    ...theme.mixins.gutters(),
    height: '8px',
    backgroundColor: 'pink',
    paddingTop: theme.spacing.unit * 2,
    paddingBottom: theme.spacing.unit * 2,
  },
});

function PaperSheet(props) {
  const { classes } = props;

  return (
    <div>
      <Paper className={classes.root} elevation={1}>
        ABC
      </Paper>
    </div>
  );
}

PaperSheet.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(PaperSheet);

您可以在此处查看沙盒中的代码: https://codesandbox.io/s/o9k0392mj5?fontsize=14

【问题讨论】:

  • 您将高度设置为小于文本高度。你想达到什么目的?如果只是去掉高度,高度会自动为文字高度加上内边距的高度。
  • 但我希望它更短 - 并且文本非常适合 - 没有办法让它居中吗?

标签: reactjs material-ui


【解决方案1】:

只要去掉高度,如果你不喜欢有多少内边距,也许你可以改变内边距。

此沙盒显示了几个填充较少或填充为零的示例:https://codesandbox.io/s/jjmxnmr7kw

【讨论】:

  • 您是说文本周围有不可见的填充?如何删除?
  • 高度太大,一定要少。
  • @Sem theme.spacing.unit 默认为 8,因此您通过 theme.spacing.unit * 2 在顶部和底部放置 16px 的填充。如果您想要更少的填充,请删除 * 2 或完全删除填充。
  • @Sem 我更新了 Abdallatif 的代码沙箱以显示几种可能性。
  • 所以基本上你必须添加 paddingTop 和 paddingBottom 直到它看起来正确。我将 paddingTop: 2 添加到第二个 Paper 以使其看起来正确。 codesandbox.io/s/32qrp1vz06?fontsize=14
猜你喜欢
  • 2021-05-26
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多