I have the following Sass placeholder and I want to extend it.
%btn
// ...button styling...
&[disabled], &.btn--disabled
color: red
.btn-primary
@extend %btn
The CSS output is the following:
[disabled].btn-primary, .btn--disabled.btn-primary{ color: red; }
When I'd like to get the following:
.btn-primary[disabled], .btn-primary.btn--disabled { color: red; }
I don't get why the output order is not the same as the specified one. How can I change that?
The parent selector, & , is a special selector invented by Sass that's used in nested selectors to refer to the outer selector. It makes it possible to re-use the outer selector in more complex ways, like adding a pseudo-class or adding a selector before the parent.
The @extend directive lets you share a set of CSS properties from one selector to another. The @extend directive is useful if you have almost identically styled elements that only differ in some small details. The following Sass example first creates a basic style for buttons (this style will be used for most buttons).
Sass lets you reference the parent selector of a nested rule using the ampersand (&) symbol –– it's one of Sass' most useful features!
The & is a special selector invented by SCSS which is used in nested selectors to refer to the outer selector . It simply means that it will be replaced with the outer selector when compiling to CSS.
Sass’s @extend rule solves this. It’s written @extend <selector>, and it tells Sass that one selector should inherit the styles of another. When one class extends another, Sass styles all elements that match the extender as though they also match the class being extended.
When one class selector extends another, it works exactly as though you added the extended class to every element in your HTML that already had the extending class. You can just write class="error--serious", and Sass will make sure it’s styled as though it had class="error" as well. Of course, selectors aren’t just used on their own in style rules.
Parent Selector The parent selector, &, is a special selector invented by Sass that’s used in nested selectors to refer to the outer selector. It makes it possible to re-use the outer selector in more complex ways, like adding a pseudo-class or adding a selector before the parent.
As long as the outer selector ends with an alphanumeric name (like class, ID, and element selectors), you can use the parent selector to append additional text. The parent selector can also be used within SassScript.
I think what you're after is the following:
%btn[disabled], %btn.btn--disabled
color: red
.btn-primary
@extend %btn
To help conceptualize what the structure will be for the CSS that's generated, just remember that %placeholder
is a token that's going to be substituted with the selector that's @extend
ing it.
Edit
Actually, this still outputs
[disabled].btn-primary, .btn--disabled.btn-primary {
color: red; }
which I wouldn't expect... Syntactically, [disabled].btn-primary
is the same as .btn-primary[disabled]
, but it's annoying that the source ordering isn't being respected.
Here's a bug report I found that describes this behaviour (apparently, it's just how @extend
works):
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