【问题标题】:Align icon inside Material UI Button对齐 Material UI 按钮内的图标
【发布时间】:2021-05-14 17:35:05
【问题描述】:

如何插入一个图标居中的材质ui按钮?

代码片段:

 <Button variant="outlined" startIcon={<Add color='primary'/>} style={{ maxWidth: "36px" }} />

预期行为:

当前行为:

【问题讨论】:

  • 我不知道材料,但这也可能完全是图标的错,以及视图框是如何设置的。
  • 我认为它需要一个标签文本,但我无法将图标居中

标签: css reactjs material-ui google-material-icons


【解决方案1】:

这个边距来自 material-ui 本身的startIcon 类。要删除这个,将一个类传入classes 属性中的startIcon

<Button
    style={{ maxWidth: "36px", minWidth: "36px" }}
    classes={{ startIcon: classes.startICon }}
    variant="outlined"
    startIcon={<Add />}
></Button>

并在 useStyles 中添加类以去除边距。

const useStyles = makeStyles((theme) => ({
  startICon: {
    margin: 0
  }
}));

这是工作演示:

【讨论】:

  • 我希望按钮轮廓为正方形而不是矩形
  • 为此添加一个与maxWidth 相同值的minWidth.. 我已经更新了答案和演示....检查一下
猜你喜欢
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2018-08-01
  • 2017-06-25
  • 2017-08-28
  • 2019-03-11
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多