【问题标题】:Material-UI Typography doesn't center in AppBarMaterial-UI 排版不在 AppBar 中居中
【发布时间】:2019-04-23 09:44:25
【问题描述】:

我正在尝试将 AppBar 中的文本居中。我尝试使用 align="center"、textAlign="center" 和 style={{ align: "center" }} 等方法将 Typography 元素中的文本居中:

class App extends React.Component {
  render() {
    return (
      <div>
        <AppBar>
          <Toolbar>
            <Typography
              variant="h6"
              color="inherit"
              style={{ align: "center" }}
            >
              Header
            </Typography>
          </Toolbar>
        </AppBar>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("app"));

...但除非我删除工具栏,否则它不起作用。但随后我将不得不手动更改 AppBar 的高度,我也没有找到办法。此外,我看到的每个 AppBar 实例都使用工具栏。我发现可以解决此问题的所有解决方案都是 outdateddon't work(不再有 ToolbarGroup 这样的东西)。

我也尝试过使用 const 样式并使用 Styles() 导出 AppBar:

const styles = {
  root: {
    flexGrow: 1
  },
  typography: {
    align: "center"
  }
};

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

  return (
    <div className={classes.root}>
      <AppBar>
        <Toolbar>
          <Typography
            variant="h6"
            color="inherit"
            className={classes.typography}
          >
            Header
          </Typography>
        </Toolbar>
      </AppBar>
    </div>
  );
}

Header.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(styles)(Header);

但它也不起作用。我想这应该很简单,我不确定我错过了什么。

【问题讨论】:

  • Container Toolbar 附近会起作用

标签: css reactjs material-ui


【解决方案1】:

AppBar component 被设置为将其子项呈现为弹性项目。

text-align 之类的 CSS 属性用于水平对齐显示不同于弹性项目的子项,例如作为表格单元格、块或内联块。

Flex 项目可以使用justify-content or align-self CSS Property or more other ones 水平居中。

const styles = {
  root: {
    flexGrow: 1,
  },
  appbar: {
    alignItems: 'center',
  }
};

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

  return (
    <div className={classes.root}>
      <AppBar
        className={classes.appbar}
        color="default"
        position="static"
      >
        <!--...-->
      </AppBar>
    </div>
  );
}

【讨论】:

    【解决方案2】:
    const styles = {
      root: {
        flexGrow: 1
      },
      typography: {
    flexGrow: 1,
        align: "center"
      }
    };
    
    function Header(props) {
      const { classes } = props;
    
      return (
        <div className={classes.root}>
          <AppBar>
            <Toolbar>
              <Typography
                variant="h6"
                color="inherit"
                className={classes.typography}
              >
                Header
              </Typography>
            </Toolbar>
          </AppBar>
        </div>
      );
    }
    
    Header.propTypes = {
      classes: PropTypes.object.isRequired
    };
    
    export default withStyles(styles)(Header);
    

    使用 flexGrow, 可以解决问题,并且在移动设备中也能响应

    【讨论】:

    • textAlign: 'center' 而不是 align: 'center' 为我工作。在与我的汉堡菜单相同宽度的右侧添加了一个填充。这对我有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2018-08-12
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    相关资源
    最近更新 更多