CSS specificity issue with :global()
See original GitHub issueDescribe the bug usually, the CSS specificity is id > class > tag. but when using global style syntax, the class is not taking president over the Tag.
To Reproduce svelte REPL
Expected behavior I would expect that in the case of
<p class="class">secret stuff</p>
the following style
<style>
:global(.class){
/* some style */
}
</style>
will override styled Tag with
<style>
p{
/* some style */
}
</style>
Information about your Svelte project:
-
browser version: any
-
operating system: OS X 10
-
Svelte version: v3.29.0
Severity I think this is kind of severe, but I may be missing some background on this specific use-case
Additional context working on a table component and want to override styles from the outside
<style>
:global(.td){
color: red;
}
}
</style>
<MySuperTableComp data={somedata} />
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Specificity - CSS: Cascading Style Sheets - MDN Web Docs
Specificity is the algorithm used by browsers to determine the CSS declaration that is the most relevant to an element, which in turn, ......
Read more >Strategies for Keeping CSS Specificity Low
Keeping CSS specificity low across all the selectors in your project is a worthy goal. It's generally a sign that things are in...
Read more >The End of Global CSS - Medium
We've worked around the issues of global scope with a series of naming conventions like OOCSS, SMACSS, BEM and SUIT, each providing a...
Read more >CSS Specificity: Things You Should Know - Smashing Magazine
Let's take a look at some important issues related to CSS Specificity as well as examples, rules, principles, common solutions, ...
Read more >Issue with :global() css-module selectors not being pure in ...
[1]: This method doesn't make the styles truly global as the styles are still scoped. The class foo will work only when some...
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
@mosheduminer, Amazing, thank you.
I think I’m more inclined to say that this is not a bug, but a gotcha. Scoped styles on
p
are compiling top.svelte-123xyz
, which has a higher specificity than.class
. One way to increase the specificity of.class
is to use the selector.:global(.class.class)
instead. I don’t think we want Svelte to be doing anything to change the selectors that appear inside:global()
s.