Last active
January 18, 2024 13:09
-
-
Save AriPerkkio/aa156252c5c34886f2aa928aa1a5d269 to your computer and use it in GitHub Desktop.
SCSS Increase specificity mixin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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