How to align horizontal icon and text in Material-UI

前端 未结 8 1986
后悔当初
后悔当初 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:33

    Having ListItemIcon and ListItemText wrapped inside a ListItem will keep it in one line and prevent breaking:

    import ListItem from '@material-ui/core/ListItem';
    import ListItemIcon from '@material-ui/core/ListItemIcon';
    import ListItemText from '@material-ui/core/ListItemText';
        
    <ListItem >
      <ListItemIcon><AccessAlarmIcon color="secondary" /></ListItemIcon>
      <ListItemText>Updated 1 hour ago</ListItemText>
    </ListItem>
    

    Demo image:

    Demo image

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

    styles

    const styles = theme => ({
        icon: {
            position: "relative",
            top: theme.spacing.unit,
            width: theme.typography.display1.fontSize,
            height: theme.typography.display1.fontSize
        }
    });
    

    JSX

    <Typography variant="display1">
        <Icon className={this.props.classes.icon}/>Your&nbsp;Text
    </Typography>
    

    you could replace display1 with display3 or another typography variant in all 3 places to choose your text size. The &nbsp; ensures that your text doesn't break between words when it wraps.

    For me this can render to look like this

    with display3 and a few other styles added for color.

    0 讨论(0)
提交回复
热议问题