1

I am using inline styling to override the styling of material-UI components in React. Recently, I realized that using CSS is a more organized and better way to style components. I have referred all the documents on the material-ui user guides. But couldn't find the solution.

Working Code:

const useStyles = theme => ({
   input: {
     color: '#000',
     fontSize: "14px",
   },
});

Inside render method of Class.

const { classes } = this.props;

<TextField
    InputProps={{
      className: classes.input
    }}
    InputLabelProps={{
      className: classes.input
    }}

 />

export default withStyles(useStyles)(MyClass);

I want to override style using CSS like below:

CSS File:

     .input {
        color: #ffffff;
         font-size: 18px;
      }
InputProps={{
          className: "input"
        }}

How can I achieve that? Thanks.

1 Answer 1

2

Using a CSS file and normal className can override the styling of material-UI component, but the CSS specificity is important. According to the Material-UI docs:

By default, the style tags are injected last in the <head> element of the page. They gain more specificity than any other style tags on your page e.g. CSS modules, styled components.

The StylesProvider component has an injectFirst prop to inject the style tags first in the head(less priority).

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

<StylesProvider injectFirst>
  {/* Your component tree.
      Styled components can override Material-UI's styles. */}
</StylesProvider>
Sign up to request clarification or add additional context in comments.

1 Comment

Could you please give an example of where you write the css/scss in this declaration?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.