MUI - Styling text inside ListItemText

ReactjsMaterial Ui

Reactjs Problem Overview


I'm trying to apply style to the text inside a ListItemText (MUI):

const text = {
  color: 'red'
}

<ListItem button><ListItemText style={text} primary="MyText" /></ListItem>

But the rendered Typograhy element inside is not styled at all ("MyText" is not red).

Looking at the generated code, it seems that the default CSS rules for Typography > subheading is overriding my CSS.

Thanks for your help

Edit: In the first version of the question, there was a misake ("className" instead of "style" prop on ListItemText, sorry about that).

Reactjs Solutions


Solution 1 - Reactjs

I beleive the only way to achieve this right now is to use the 'disableTypography' prop of the ListItemText element.

 <ListItemText
        disableTypography
        primary={<Typography type="body2" style={{ color: '#FFFFFF' }}>MyTitle</Typography>}
      />

This lets you embed your own text element with whatever styling you want on it.

Solution 2 - Reactjs

this is good one, you can implement without disabling typography

<ListItemText 
   classes={{ primary: this.props.classes.whiteColor }}
   primary="MyTitle"
/>

Solution 3 - Reactjs

Per the documentation, the <ListItemText /> component exposes the prop primaryTypographyProps, which we can use to accomplish what you're attempting in your question:

const text = {
    color: "red"
};

<ListItem button>
    <ListItemText primaryTypographyProps={{ style: text }} primary="MyText" />
</ListItem>

Hope that helps!

Solution 4 - Reactjs

            <ListItem >
                    <Avatar style={{ backgroundColor: "#ff6f00" }}>
                      <LabIcon />
                    </Avatar>
                    <ListItemText 
                     primary={<Typography variant="h6" style={{ color: '#ff6f00' }}>Lab</Typography>}
                     secondary="Experiments" />
                  </ListItem>

enter image description here

Solution 5 - Reactjs

Turns out there's an even better way to do this as such:

const styles = {
  selected: {
    color: 'green',
    background: 'red',
  },
}

const DashboardNagivationItems = props => (
  <ListItemText
    classes={{ text: props.classes.selected }}
    primary="Scheduled Calls"
  />
)

export default withStyles(styles)(DashboardNagivationItems)

You can read more about how this is done here: https://material-ui-next.com/customization/overrides/#overriding-with-classes

Solution 6 - Reactjs

If you are using material-ui 3.x, this is how it is done:

import { withStyles } from '@material-ui/core/styles';

const styles = {
  listItemText: {
    color: 'white',
  },
}

class YourComponent extends Component {
...
render() {
    const { classes } = this.props; // this is magically provided by withStyles HOC.
    return (
          <ListItem button>
            <ListItemIcon>
              <DashboardIcon />
            </ListItemIcon>
            <ListItemText classes={{ primary: classes.listItemText }} primary="My Bookings" />
          </ListItem>
    );
...

}
export default withStyles(styles)(YourComponent);

set all your text related styles on primary property. Sad that it's hidden so deep in the documentation. https://material-ui.com/api/list-item/

Solution 7 - Reactjs

Method 1

const textColor = {
    color: "red"
};

<ListItemText primaryTypographyProps={{ style: textColor }} primary="BlodyLogic" />
For the Secondary Text
const secondaryColor = {
   color: 'green'
}

<ListItemText secondaryTypographyProps={{ style: secondaryColor }} 
     secondary="If you say that you" />

Method 2

<ListItemText
          primary={
            <Typography variant="caption" display="block" gutterBottom>
              caption text
            </Typography>
          }
 />

Custom design:

const useStyles = makeStyles({
      text: {
        color: 'red',
        fontSize: 49
      },
    });
        
/.....// all make classes

<ListItemText
              primary={
                <Typography className={classes.text}>
                  caption text
                </Typography>
              }
     />

Offical Docs

Solution 8 - Reactjs

MUI v5 update

You can leverage system properties in Typography to directly add styling props in the primary and secondary components inside the ListItemText:

<ListItemText
  primary="Photos"
  secondary="Jan 9, 2014"
  primaryTypographyProps={{
    fontSize: 22,
    color: 'primary.main',
  }}
  secondaryTypographyProps={{
    fontSize: 15,
    color: 'green',
  }}
/>

You can also use styled if you want to reuse ListItemText in multiple places:

import MuiListItemText from '@mui/material/ListItemText';
import { styled } from '@mui/material/styles';

const ListItemText = styled(MuiListItemText)({
  '& .MuiListItemText-primary': {
    color: 'orange',
  },
  '& .MuiListItemText-secondary': {
    color: 'gray',
  },
});

Live Demo

Codesandbox Demo

Solution 9 - Reactjs

Material v1.0

I would add something to @SundaramRavi in regards to:

  • the way he is using style element which is not great as for Material v1.0 (read the very important difference between v0.16+ and v1.0.
  • the way files can be structured for better separation of concern.

Whatever.styles.js

const styles = theme => ({
  white: {
    color: theme.palette.common.white
  }
});

exports.styles = styles;

Whatever.js

const React = require('react');
const PropTypes = require('prop-types');
const {styles} = require('./Whatever.styles');

class Whatever extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    const {classes} = this.props;
    return (
      <div>
        <ListItemText
          disableTypography
          primary={
            <Typography type="body2" style={{body2: classes.white}}>
              MyTitle
            </Typography>
          }
        />
      </div>
    );
  }
}

Whatever.propTypes = {
  classes: PropTypes.object.isRequired,
  theme: PropTypes.object.isRequired
};

exports.Whatever = withStyles(styles, {withTheme: true})(Whatever);

Solution 10 - Reactjs

you can easily style text by using & span

const useStyles = makeStyles(theme => ({
	listItem: {
		"& span": {
			color: "red"
		}

	}
}));
..
..
..

<ListItem button>
	<ListItemIcon>
		<SendIcon />
	</ListItemIcon>
	<ListItemText className={classes.listItem} primary="Sent mail"/>
</ListItem>

Solution 11 - Reactjs

If your are using "@material-ui/core": "^4.11.4" (4.X.X or newer version) then it's simple:

#1st step: Define your styles like this:

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    // Other Styling if you wish to use it
    root: {
      width: '100%',
      maxWidth: '36ch',
      backgroundColor: theme.palette.background.paper
    },
    // Other Styling if you wish to use it
    inline: {
      display: 'inline'
    },
    // Styling that you asked for
    textColor: {
        color: 'red'
    }
  }),
);

#2nd step: Define a constant to use the specific Styles like this:

const AlignItemsList = (props: any) => {
    const classes = useStyles(); // Like this one
    ......
}

#3rd step: In your ListItemText component do like this:

const AlignItemsList = (props: any) => {
    const classes = useStyles();
    ......
    <ListItemText
         primary="Your Text Goes Here"
         classes={{ primary: classes.textColor }} // Like this one
         ...
   />
};

#4th & final step: Just export your component normally without any other stuff, like this:

export default AlignItemsList;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJulien TanayView Question on Stackoverflow
Solution 1 - ReactjsSundaram RaviView Answer on Stackoverflow
Solution 2 - ReactjsSachin srinivasanView Answer on Stackoverflow
Solution 3 - ReactjsjosephemswilerView Answer on Stackoverflow
Solution 4 - ReactjsHitesh SahuView Answer on Stackoverflow
Solution 5 - ReactjsAndre RomanoView Answer on Stackoverflow
Solution 6 - Reactjssaran3hView Answer on Stackoverflow
Solution 7 - ReactjsEricgitView Answer on Stackoverflow
Solution 8 - ReactjsNearHuscarlView Answer on Stackoverflow
Solution 9 - ReactjsMickView Answer on Stackoverflow
Solution 10 - ReactjsChervenView Answer on Stackoverflow
Solution 11 - ReactjsEdzio AuditoreView Answer on Stackoverflow