question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

material-ui and styled override specificity issue

See original GitHub issue

I’m using material-ui(https://material-ui-next.com/) components library. In this library there is for example component Typography for rendering text, it supports className to override styles. I’m using styled function from styled-components to override default styles of Typography component like this:

const StyledTypography = styled(Typography)`
	color: palevioletred;
	font-weight: bold;
        any other styles
`;

but some styles are not overridden because of specificity. Maybe because material-ui library adds style tag to the end of head tag after styled-components does the same. I did not find a way except of using !important to override styles. Maybe there is some way to avoid using !important?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:18
  • Comments:26 (5 by maintainers)

github_iconTop GitHub Comments

165reactions
mxstbrcommented, Oct 19, 2017

You hit the nail on the head with your analysis, this in indeed because material-ui injects their styles after the styled-components ones. As you also said, the way to work around this is to increase the specificity, which you could do with !important but that’s a pain the butt.

I’d probably use this hack:

const StyledTypography = styled(Typography)`
  && {
    color: palevioletred;
    font-weight: bold;
  }
`;

Each & gets replaced with the generated class, so the injected CSS then looks like this:

.StyledTypography-asdf123.StyledTypography-asdf123.StyledTypography-asdf123 {
    color: palevioletred;
    font-weight: bold;
}

The repeated class bumps the specificity high enough to override the source order without being very tedious to write! 🎉

5reactions
oliviertassinaricommented, Dec 9, 2017

We have added a documentation section for using styled-components with Material-UI. The demo shows how to configure JSS to avoid the !important issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Material UI Overriding styles with increased specificity
You could use global overrides to change the default margin of the AccordionSummary. However, this will affect all AccordionSummary ...
Read more >
How to customize - Material UI - MUI
To override the styles of a specific part of the component, use the global classes provided by Material UI, as described in the...
Read more >
Style library interoperability - Material UI - MUI
In MUI, all child elements have an increased specificity of 2: .parent ... The following examples override the slider's thumb style in addition...
Read more >
Migrating from JSS (optional) - Material UI - MUI
Note that you may continue to use JSS for adding overrides for the components ... JSS styles to styled API, but this approach...
Read more >
Breaking changes in v5, part one: styles and themes - MUI
If you have a custom cache and are using Emotion to style your app, it will override the cache provided by Material UI....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found