【问题标题】:Button next to Typography排版旁边的按钮
【发布时间】:2019-06-10 10:24:13
【问题描述】:

如何在纸容器内的排版标签旁边放置一个按钮,与右/尾对齐?

<div className={classes.root}>
    <Grid container spacing={3}>
      <Grid item xs={12} md={6}>
        <Paper className={classes.paper}>
          <Typography inline>CPU</Typography>
          <IconButton className={classes.button} aria-label="Add" size="medium" ><AddIcon /></IconButton>
        </Paper>
      </Grid>
</div>

这是我的风格:

const useStyles = makeStyles(theme => ({
  root: {
    display: 'flex',
    padding: theme.spacing(2),
    flexGrow: 1,
  },
  paper: {
    padding: theme.spacing(2),
    color: theme.palette.text.secondary,
  }
}));

我正在使用 Typography 的 inline 属性,但按钮继续在下一行...非常感谢,我是新手。

【问题讨论】:

    标签: reactjs material-ui material-design typography


    【解决方案1】:

    如果您不介意添加更多 dom 元素。

    <div className={classes.root}>
        <Grid container spacing={3}>
            <Grid item xs={12} md={6}>
                <Paper className={classes.paper}>
                    <Grid container alignContent="space-between" alignItems="center">
                        <Grid item>
                            <Typography inline>CPU</Typography>
                        </Grid>
                        <Grid item>
                            <IconButton className={classes.button} aria-label="Add" size="medium" ><AddIcon /></IconButton>
                        </Grid>
                    </Grid>
                </Paper>
            </Grid>
    </div> 
    

    否则你可以改变 CSS 属性

    paper: {
        padding: theme.spacing(2),
        color: theme.palette.text.secondary,
        display: flex,
        alignContent: 'space-between',
        alignItems: 'center'
    }
    

    【讨论】:

    • 谢谢,现在排版与按钮不垂直对齐,我尝试添加verticalAlign:'middle',但它不起作用......按钮的右对齐呢?
    • 对于垂直对齐,您可以将alignItems="center" 添加到网格容器中。
    • 对于右对齐,我在排版的网格中添加了“flex-grow: 1”。这是一个好的解决方案吗?
    • 以后你可以使用这个互动工具material-ui.com/components/grid/#interactive
    猜你喜欢
    • 2014-04-18
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 2011-09-24
    相关资源
    最近更新 更多