I have some css rules to be applied for primary button, I did something like this:
.my-btn {
&--primary {
padding: 10px;
}
}
Now I want same rules to be applied for a primary button with "sm" class, I can do something like this:
.my-btn {
&--primary, &--primary.my-btn--sm {
padding: 10px;
}
}
This is working for me. But I want to use "&" for "sm" as well, like this:
.my-btn {
&--primary, &--primary.&--sm {
padding: 10px;
}
}
Is it possible?
Explains the mostly common used ampersand symbol in SCSS In SCSS, the & is used to nest selectors. As one of the most extremely useful symbols, it can be a nice time-saver if you know how to use it.
Multiple classes can be applied to a single element in HTML and they can be styled using CSS.
Nesting is combining of different logic structures. Using SASS, we can combine multiple CSS rules within one another. If you are using multiple selectors, then you can use one selector inside another to create compound selectors.
In this case, a solution could be use a variable:
.my-btn {
$this: &;
&--primary, &--primary#{$this}--sm {
padding: 10px;
}
}
Or use interpolation syntax
.my-btn {
&--primary, &--primary#{&}--sm {
padding: 10px;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With