Search code examples
sasscompass-sasscss

Access the parent selector from within a SASS mixin


I have set up a mixin for a button using display:inline-block. I am trying to get to the parent of whatever class that will eventually end up using the mixim, so I can add the font-size: 0px line there to make sure that I don't need to make adjustments to my HTML to avoid unwanted space between each button.

Here's an example... I want the. parent class to receive the font-size: 0px line.

@mixin button() {
    display:inline-block;
    font-size: 1em;
    //other stuff to make a pretty button
    && { font-size: 0px; }
}

.parent{
    .child {
        @include button();
    }
}

Solution

  • No, this is not possible. You could do something like this, though:

    @mixin button($child: '.child') {
        font-size: 0px;
        //other stuff to make a pretty button
    
        #{$child} {
            display:inline-block;
            font-size: 1em;
        }
    }
    
    .parent{
        @include button();
    }
    

    Output:

    .parent {
      font-size: 0px;
    }
    .parent .child {
      display: inline-block;
      font-size: 1em;
    }