Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ampersand (parent selector) inside nested selectors [duplicate]

Tags:

Is this not documented or just not possible?

#parent {
  #child {
    width: 75%;

    .additional_parent_class & {
      width: 50%;
    }
  }
}

This will basically turn into:

.additional_parent_class #parent #child {
  width: 50%;
}

While this makes sense because of the implementation of the ampersand and how it's used. What if I'm trying to get it to achieve this:

#parent.additional_parent_class #child {
  width: 50%;
}

The only way I have been able to achieve this is by writing another rule outside of the child declarations:

#parent{
  #child {
    width: 75%;
  }

  &.additional_parent_class #child {
    width: 50%;
  }
}

While this isn't necessarily a 'pain in the butt' in this implementation, it seems counter productive if #child has children of its own that will now need to be duplicated in both rules.

Anyway, maybe I'm just being picky, but it would be great if there were more ways to traverse through the selectors.

like image 691
jarodtaylor Avatar asked Jul 30 '12 15:07

jarodtaylor


People also ask

What is parent/child selector and nested 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 does ampersand mean in less?

One of the less-documented features of the LESS language is the ampersand selector, which refers to the parent selector inside a nested selector. The ampersand selector is most commonly used when applying a modifying class or pseudo-class to an existing selector: a { color: blue; &:hover { color: green; } }

What is selector in scss?

In CSS, selectors are used to target the HTML elements on our web pages that we want to style.


2 Answers

Although it is not currently possible, this and many similar improvements to the & syntax are slated for release in Sass 3.3. You can follow the discussion about the feature on the Sass issue here.

like image 132
hopper Avatar answered Sep 21 '22 15:09

hopper


I agree it would be very helpful. Unfortunately, it's not currently possible in SASS (or any other CSS preprocessor I know of).

like image 29
Russell Davis Avatar answered Sep 21 '22 15:09

Russell Davis