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.

Implement ability to increase a selector specificity by some syntax sugar

See original GitHub issue

Hey, 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:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
esr360commented, Oct 31, 2018

@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.

0reactions
maxcoopercommented, Jun 30, 2021

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:

@mixin increase-specificity($amount: 1) {
  $suffix: '';
  @for $i from 0 to $amount {
    $suffix: $suffix + ':nth-child(n)';
  }
  &#{$suffix} {
    @content;
  }
}

To use:

.myClass {
  @include increase-specificity(4) {
    content: 'I am specific!';
  }
}

Which emits:

.myClass:nth-child(n):nth-child(n):nth-child(n):nth-child(n) {
  content: 'I am specific!';
}
Read more comments on GitHub >

github_iconTop 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 >

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