Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ampersand in selector name with SASS

Tags:

css

sass

Here's what i have in my CSS file ...

.search-doctors-box {
    position: relative;
    z-index: 999;
    margin: 0px;
}

.search-doctors-box--at-map {
    position: absolute;
    margin-bottom: 10px;
    top: 0px;
    left: 415px;
    width: 680px;
}

I want to achieve this in SASS using the & as the parent selector name and to join it with the rest of the string...

.search-doctors-box {
    position: relative;
    z-index: 999;
    margin: 0px;

    &--at-map {
        position: absolute;
        margin-bottom: 10px;
        top: 0px;
        left: 415px;
        width: 680px;
    }
}

Is it possible?

Thanks!

like image 806
Kitze Avatar asked Dec 11 '13 09:12

Kitze


People also ask

How do you use ampersand in sass?

SASS Parent Selector Syntax: All you have to do is use & (ampersand) as a prefix followed by some text and when you will compile the SCSS code into CSS code the ampersand sign will be replaced with the name of the parent selector.

What does the ampersand & symbol do in Sass SCSS?

Ampersand or “&” is a powerful feature of SASS. It enhances the readability of code by using nesting statements, which takes an edge over conventional CSS. We can nest the css class any number of times but most of the times it's not required.

What does ampersand mean in CSS selector?

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. There are some most commonly used cases: there is a space after the ampersand.


1 Answers

Unfortunately there are limitations to what you can use in combination with the ampersand in selectors - it expects a class name (.), an id (#), a pseudo class (:), or attribute selectors ([]).
Other acceptable symbols that can also be combined with & are valid CSS selector combinators , >, +, and ~.


Solution for Sass >= 3.3:

You can use string interpolation on the ampersand #{&} and then you can concatenate it with any string.
However, this way (if you do this in a nested rule) a nested selector still automatically gets the parent selectors attached at the beginning:

.parent {
  #{&}--at-map { ... }
}

would return:

.parent .parent--at-map { ... }

But, you can save the contents of the ampersand in a variable and use it outside the parent rule. So in your case something along these lines could work:

$last-rule: null;
.search-doctors-box {
  position:relative;
  z-index:999;
  margin: 0px;
  $last-rule: &;
}
#{$last-rule}--at-map {
  position: absolute;
  margin-bottom:10px;
  top: 0px;
  left: 415px;
  width:680px;
}

DEMO

or even better, you could use

@at-root

with nested concatenated selectors, like so:

.search-doctors-box {
  position:relative;
  z-index:999;
  margin: 0px;

  @at-root #{&}--at-map {
    position: absolute;
    margin-bottom:10px;
    top: 0px;
    left: 415px;
    width:680px;
  }
}

which will give you the desired output:

DEMO

like image 167
Martin Turjak Avatar answered Oct 08 '22 23:10

Martin Turjak