Comments (6)
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?
from styled-components.
Oh man, great response, thank you for taking the time to explain, it makes a lot of sense now.
- Yes, I am using the
style-loader
. - You are 100% right, bumping the specificity fixes the issue in both examples (both directions).
As for documenting this, do you think it should live under the Tip and Tricks section? That is kind of where I would expect an edge case like this to belong.
from styled-components.
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!
from styled-components.
I'm working on a component library that includes a styled component , like this:
const MyDiv =styled.div`
background-color:green;
`;
I want the flexibility for users of my component library to easily override the styles through css and css, particularly the background-color
, when they use my components in their projects.
import MyDiv from 'my-package';
....
<MyDiv className='background-color-red'>Text</MyDiv>
....
As classes with styled component will higher specificity (since styles with styled component will be appended in head) there will no way to override those style at consumer end.
I'm using esbuild for my component library.
What's the best way to ensure that my component styles can be customized by users without being overridden when they use my component library in their projects? Any advice or best practices would be greatly appreciated.
from styled-components.
Just discovered that Overriding a non Styled Component (your own or 3rd party react component) only works if the react component adds the className
prop. This seems to be a caveat more than anything but I am guessing this should be listed in the main README, what do you think?
from styled-components.
Yeah true, that's just inherent to how styled-components and react works. We should definitely note that!
from styled-components.
Related Issues (20)
- Theme prop (not styled) is not being passed through HOT 1
- Feature request for v5 to ease migration to v6 of large ecosystems
- Interplay of `shouldForwardProp` clarifying question
- Export the `toStyleSheet` function HOT 3
- createGlobalStyle not working version >= 5.3.1 HOT 1
- [Docs] Lint for pseudo selectors - Styled Components update from v5 to v6 HOT 1
- Tailwind selectors do not work the expected (from me) way
- React SSR Streaming Bug
- Error while building project after upgrading from 5.3.11 to 6.0.0 HOT 2
- styled-components is ~50% larger than it could be due to IE11 support HOT 1
- Cannot infer the 'as' and 'forwardedAs' props by React.ComponentProps
- Ssr not picking up styled components from an npm package HOT 1
- error TS4023 when compiling a library that uses "styled-components": "^6.1.9" HOT 4
- Required attrs type fixes for 6.1.9 not applied when using prop factory
- Types of `.attrs` are still broken for React Native on 6.1.9 when using `styled()` HOT 4
- Required attrs type fixes for 6.1.9 not applied when using union props HOT 1
- Heap issues in latest 6.1.9 HOT 3
- Regression: `typeof` on a styled component returns incompatible type HOT 1
- toHaveStyleRule assertion fails after upgrading to 6.1.10 HOT 2
- Use changesets
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from styled-components.