简体   繁体   English

Material UI - 将图标与排版文本居中对齐

[英]Material UI - Align icon to center with the Typography text

How do I align icon to the same level as the text.如何将图标对齐到与文本相同的水平。 As of now, I see the icon is at little top to the text.截至目前,我看到图标位于文本的顶部。 I tried to use padding-top: 5px and also margin-top: 5px but that does not seem to work as expected.我尝试使用padding-top: 5pxmargin-top: 5px但这似乎没有按预期工作。

  <Box>
    <Typography variant="h2">
      Photography <FaCamera />
    </Typography>
  </Box>

I created a working example using Stackblitz .我使用Stackblitz创建了一个工作示例。 Could anyone please help?有人可以帮忙吗?

I was able to align it correctly using position and top properties of CSS.我能够使用 CSS 的positiontop属性正确对齐它。

<Box>
  <Typography variant="h2">
    Photography <FaCamera style={{position: 'relative', top: '8px'}} />
  </Typography>
</Box>

You can easily use a Grid to achieve the correct alignment without the need for any CSS.您可以轻松地使用 Grid 来实现正确的对齐,而无需任何 CSS。

<Grid container alignItems="center" spacing={2}>
   <Grid item>
      <Typography variant="h2">
         Photography
      </Typography>
   </Grid>
   <Grid item>
      <FaCamera />
   </Grid>
</Grid>

只需在您的内联图标上执行fontSize="inherit" ,例如: <FaCamera fontSize="inherit" />

<Typography variant="h2">
  Photography <FaCamera  style={{verticalAlign:"middle"}}/>
</Typography>

you can try inline style 'verticalAlign', it is works for me .您可以尝试内联样式“verticalAlign”,它对我有用。

I ran into the same issue when importing icons from @mui/icons-material .@mui/icons-material导入图标时,我遇到了同样的问题。

Setting fontSize: 'inherit' makes the icon the same size as the font.设置fontSize: 'inherit'使图标与字体大小相同。 There was still a slight vertical alignment issue which I was able to resolve by using verticalAlign: 'text-top' .仍然存在轻微的垂直 alignment 问题,我可以通过使用verticalAlign: 'text-top'来解决。

Code that worked for me was:对我有用的代码是:

<Icon sx={{ fontSize: 'inherit', verticalAlign: 'text-top' }} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM