Skip to content

Instantly share code, notes, and snippets.

@AriPerkkio
Last active January 18, 2024 13:09
Show Gist options
  • Save AriPerkkio/aa156252c5c34886f2aa928aa1a5d269 to your computer and use it in GitHub Desktop.
Save AriPerkkio/aa156252c5c34886f2aa928aa1a5d269 to your computer and use it in GitHub Desktop.
SCSS Increase specificity mixin
@mixin increase-specificity($times: 1) {
$selector: "";
@for $index from 1 through $times {
$selector: #{$selector}#{&};
}
&#{$selector} {
@content;
}
}
// Tests
.default {
@include increase-specificity() {
padding: 2;
}
}
.once {
@include increase-specificity(1) {
padding: 2;
}
}
.twice {
@include increase-specificity(2) {
padding: 2;
}
}
.thrice {
@include increase-specificity(3) {
padding: 2;
}
}
// Output
.default.default {
padding: 2;
}
.once.once {
padding: 2;
}
.twice.twice.twice {
padding: 2;
}
.thrice.thrice.thrice.thrice {
padding: 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment