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.

CSS overriding, className vs StyledComponent

See original GitHub issue

Passing a className prop to a StyledComponent works fine, but if a specific CSS property (e.g. background-color) is passed in via a className prop and also defined in the StyledComponent, the StyledComponent will win.

In the example below <div> will have a green background. Should the CSS properties sent via the className prop override any matching CSS properties in the StyledComponent? Either way, it would be great to have this documented, happy to submit a PR.

// StyledUpYo.js
const StyledUpYo = styled.div`background-color: green;`;
// styles.css
.styledUpYo {
  background-color: red;
}
import StyledUpYo from './StyledUpYo';
import styles from './styles.css';

const Contrived = (props) => (
  <StyledUpYo className={styles.styledUpYo}>
    {props.children}
  </StyledUpYo>
);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
geelencommented, Oct 15, 2016

Both of them simply inject CSS into the head, so if you have conflicting rules you have the same CSS cascade rules that always apply — for the same specificity, the rules that are defined latest will win.

I haven’t looked into webpack’s style-loader (which I’m assuming you’re using) as to where it injects the styles, but it wouldn’t surprise me if will always inject CSS ahead of Styled Components (since one is build time and one is run time). If that’s the case, there’s not much you can do beyond escalating specificity. Or, you know, try to avoid clashes where possible!

But if you want to bump specificity the easiest way is to do this:

// styles.css
.styledUpYo.styledUpYo {
  background-color: red;
}

Or, if for some reason the order switches and you need to boost specificity in SC, you can do this:

// StyledUpYo.js
const StyledUpYo = styled.div`
  && {
    background-color: green;
  }
`;

Hope that helps explain it. Maybe you could try out the specificity increase and just confirm it works for your problem, and we could document that?

2reactions
geelencommented, Oct 15, 2016

Happy to help!

I think it could be its own doc. “Using Styled Components with Existing CSS”. There may well be a few more edge cases we come across. If you’re happy to have a go at writing that that would be great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to override a css class in style component - Stack Overflow
It works just like css. import * as Toolkit from "@some_repo/toolkit"; export const SearchInput = styled(Toolkit.Input)` .
Read more >
Advanced Usage - styled-components
Existing CSS​​ styled-components generates an actual stylesheet with classes, and attaches those classes to the DOM nodes of styled components via the className...
Read more >
Overriding Material UI styles with Styled Components.md
1 - Add the property classes in the AppBar component: <AppBar classes={{root: 'my-root-class'}}. 2 - Override the styles with the styled components:.
Read more >
Style library interoperability - Material UI - MUI
Plain CSS; Global CSS; Styled Components; CSS Modules; Emotion; Tailwind CSS ... The following examples override the slider's thumb style in addition to...
Read more >
Is classname prop compulsory to override styling of ... - Reddit
Yes, if you want to overload a styled component's css you must add className as a prop.
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