Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ampersand (&) at the end, and part of, a selector in SASS

I have a problem I'm struggling with. I have this mixin (which is an attempt to port some less to sass ):

@mixin button-variant($color, $background, $border)
    {
        ...
        .foreverAlone{
            ...
        }

        .iThink .illNeverWork& {

            color: $pinkUnicornRainbow;
            ...
        }
  }

Which obviously is not working :D I would like it to generate something like:

.callerClass .foreverAlone{
    ...
} 

.callerClass .iThink .illNeverWork.callerClass{

    color: #123ABC;
    ...
}

The mixin is called inside various div classes, so it is not possible to make it static (easily).

Is there any workaround from some Sass pro (or not pro, but smarter than I am)? Thank you all for the attention and sharing your solutions.

like image 607
Edoardoo Avatar asked Mar 19 '14 09:03

Edoardoo


People also ask

Why is it called an ampersand?

Etymology. The term ampersand is a corruption of and (&) per se and, which literally means "(the character) & by itself (is the word) and." The symbol & is derived from the ligature of ET or et, which is the Latin word for "and."

What is an ampersand?

What is an ampersand? An ampersand (&) is a typographical symbol that is rarely used in formal writing. It is read aloud as the word and and is used as a substitute for that word in informal writing and in the names of products or businesses.

What is the name of &?

An ampersand is a sign for the word and. It's written or typed as the symbol &. It's a modification of the term “and per se and,” which has Latin origins. The ampersand can indicate that the listed items are grouped together as part of a name.

How do you write an &?

The ampersand represents the word and. It is actually a ligature (two characters combined) an e and t from the Latin word et, meaning “and”. With many variations, there are two main ways of writing the ampersand: the traditional version (&), and the style that looks more like an E or an et.


1 Answers

For Sass versions 3.2 and older, these are all of the valid ways to use the parent selector:

.foo {
    &, &.bar, &#bar, &:after, &[active] {
        color: red;
    }
}

.foo {
    .bar & {
        color: red;
    }
}

As of Sass 3.3, this is valid:

.foo {
    &bar, &-bar {
        color: red;
    }
}

As of Sass 3.4, this is valid:

.foo {
    $foo: &;
    @at-root bar#{&} {
        color: red;
    }
}
like image 186
cimmanon Avatar answered Sep 19 '22 15:09

cimmanon