material-ui and styled override specificity issue
See original GitHub issueI’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:
- Created 6 years ago
- Reactions:18
- Comments:26 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
Each
&
gets replaced with the generated class, so the injected CSS then looks like this:The repeated class bumps the specificity high enough to override the source order without being very tedious to write! 🎉
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.