How to align horizontal icon and text in Material-UI

前端 未结 8 1983
后悔当初
后悔当初 2021-01-31 07:35

I am a newbie in material-ui, now my icon and text are not aligned :

\"not

My desired results:

相关标签:
8条回答
  • 2021-01-31 08:07

    Same problem here, this is what I did.

    import LinkIcon from '@material-ui/icons/Link';
    import styled from 'styled-components';
    ...
    const Resolve = styled.div`
      display: flex;
      vertical-align: middle,
    `;
    
    <Resolve>
      <LinkIcon style={{ marginRight: '5px' }} />
      <p>resolve</p>
    </Resolve>
    

    If you aren't happy with mUI default link icon you can always DIY:

    {/* this is the same chained icon used in the own material-ui, 
    idk why this ins't avaiable yet */}
    
    function CustomLinkIcon(props) {
      return (
        <SvgIcon {...props}>
          <path d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z" />
        </SvgIcon>
      );
    }
    ...
    <Resolve>
      <CustomLinkIcon 
         {/* adjust margin top if needed */}
         style={{ marginRight: '3px', marginTop: '3px' }} {
      />
      <p>resolve</p>
    </Resolve>
    
    
    0 讨论(0)
  • 2021-01-31 08:09

    alternative simple solution

    <Grid container direction="row" alignItems="center">
         <SearchIcon /> example
    </Grid>
    
    0 讨论(0)
  • 2021-01-31 08:10

    You can also use Material UI's Flexbox component.

    For example:

    // ...
    import { Box } from '@material-ui/core';
    // ...
    
    <Box alignItems="center" display="flex">
     <Box>
        <LinkIcon className={classes.linkIcon}  />
     </Box>
     <Box>
        revolve
     </Box>
    </Box>
    

    The alignItems: center attribute will vertically align the inner items.

    This will add some additional markup. However, if you look at the component's API there's a lot of additional flexibility. Such as for example a method to use margin or padding that's consistent with the rest of your Material UI implementation. Also it's really easy to align the items differently if the use-case should occur.

    0 讨论(0)
  • 2021-01-31 08:13

    You need to use Grid. Something like that should works:

    <Grid container direction="row" alignItems="center">
      <Grid item>
        <LinkIcon className={classes.linkIcon}  />
      </Grid>
      <Grid item>
        revolve
      </Grid>
    </Grid>
    
    0 讨论(0)
  • 2021-01-31 08:27

    Try the below code. You can use variant as per your requirement.

    const useStyles = makeStyles(theme => ({
      wrapIcon: {
        verticalAlign: 'middle',
        display: 'inline-flex'
       }
    }));
    
    <Typography variant="subtitle1" className={classes.wrapIcon}>
        <LinkIcon className={classes.linkIcon}  /> revolve
    </Typography>
    
    0 讨论(0)
  • 2021-01-31 08:29

    This works perfectly!

    <div style={{
        display: 'flex',
        alignItems: 'center',
        flexWrap: 'wrap',
    }}>
        <LinkIcon />
        <span>revolve</span>
    </div>  
    
    0 讨论(0)
提交回复
热议问题