Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass extend and parent selector

Tags:

css

sass

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?

like image 217
TimPetricola Avatar asked Sep 12 '13 11:09

TimPetricola


People also ask

Is there a SCSS 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.

What is extend in Sass?

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

Which symbol is used to refer parent selector in Sass?

Sass lets you reference the parent selector of a nested rule using the ampersand (&) symbol –– it's one of Sass' most useful features!

What is the use of & in SCSS?

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.

What is the use of @extend in Sass?

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.

What happens when one Class Selector extends another in Sass?

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.

What is parent and parent selector in Sass?

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.

How do I append text to a selector in Sass?

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.


1 Answers

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 @extending 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):

  • placeholder-selectors decide on the actual selector's order, not the actual selectors
like image 179
André Dion Avatar answered Oct 20 '22 03:10

André Dion