Implement ability to increase a selector specificity by some syntax sugar
See original GitHub issueHey, everyone knows that using of important
in CSS is the biggest evil. But there is one good hack for it — selector repeat, for example:
.link {
color: red;
font-size: 24px;
line-height: 24px;
}
.link { // without hack
color: green !important;
font-size: 26px !important;
line-height: 26px !important;
}
.link.link { // with hack
color: green;
font-size: 26px;
line-height: 26px;
}
it’s really helpful, I have tried to do it with SCSS:
.class1__el {
padding: 10px;
}
@media only screen and (max-width : 700px) {
.class1 {
&__el#{&}__el {
padding: 27px 31px 27px;
}
}
it works but it looks strange, also what will be if I need to increase specificity again and again?
I suggest adding some syntax sugar here, something like:
.class1 {
&__el:set-specificity(4) {
padding: 27px 31px 27px;
}
}
that will be compiled to
.class1__el.class1__el.class1__el.class1__el {
padding: 27px 31px 27px;
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Specificity - CSS: Cascading Style Sheets - MDN Web Docs
The specificity algorithm calculates the weight of a CSS selector to ... This feature can be used to increase a selector's specificity:.
Read more >Selector Specificity with CSS Preprocessors - SitePoint
Self-chaining will increase the specificity of a selector from 0,1,0 to 0,2,0 to 0,3,0 and so on, making it almost infinitely scalable. Final ......
Read more >Scope Proposal & Explainer - OddBird CSS Sandbox
Based on feedback so far, I lean towards applying the scope-root specificity to the overall specificity of each selector. All the examples above ......
Read more >Handling CSS Selector Specificity with Preprocessors
Each time you encounter a specificity problem and are inclined on making a specific selector heavier than the other, you can simply prepend...
Read more >Talking about logical selectors -- parent selectors here it comes!
It does not implement a new function of a selector, it is more like a syntactic sugar, similar to the Class() syntax in...
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 FreeTop 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
Top GitHub Comments
@hardrese7 I guess if anything I’m suggesting that if you find yourself having to do this it probably means you have some inefficiencies elsewhere in your codebase.
If anyone else finds this issue looking for a good way to increase specificity, here’s a mixin that adds
:nth-child(n)
to the parent selector, which increases the specificity while selecting the same elements:To use:
Which emits: