-
|
We want to render components in an environment that already has id selectors for style rules. For examples: #container h1 {
// some-rules
}So to override this we need to either add id to the selector, or to use inline style tags. We'd like to avoid inline style tags. Is there a way to prefix selectors in generated style tags with an id? <style>
- .generatedClass {
+ #container .generatedClass {
// some-rules
}
</style> |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
You can use const Title = styled.h1`
#container & {
color: blueviolet;
}
`;Or You can use // Overrides ❌
const Title = styled.h1`
color: gray;
`;
// Overrides ⭕️
const GlobalStyle = createGlobalStyle`
#container ${Title} {
color: gray;
}
`;Please check this sample code :) |
Beta Was this translation helpful? Give feedback.
-
|
There are two approaches:
const Title = styled.h1`
#container & {
color: red;
}
`;
<StyleSheetManager stylisPlugins={[myPrefixPlugin]}>
<App />
</StyleSheetManager>Approach 1 is simpler for individual components. Approach 2 is better if you need it globally. |
Beta Was this translation helpful? Give feedback.
There are two approaches:
&nesting to scope rules within a parent selector:stylisPluginsonStyleSheetManagerto write a custom stylis plugin that prepends a selector to all rules:Approach 1 is simpler for individual components. Approach 2 is better if you need it globally.