less.js icon indicating copy to clipboard operation
less.js copied to clipboard

Cascade from "root" within nested style

Open anyonecancode opened this issue 15 years ago • 4 comments

Let's say that I have a navigation element. In Less, I might write it thus:

nav {
  li {
    background:red;
  }
}

Now, let's say that when the BODY gets a class applied to it, the navigation element should change its background color. Unless I'm missing something, there's no way to write this the LESS way. Ideally, if LESS supported it I think it would look something like this:

nav {
  li {
    background:red;

    /.newstate {
        background:blue
   }
  }

Which would compile to:

nav li {background:red}
.newstate nav li {background:blue}

anyonecancode avatar Aug 31 '11 21:08 anyonecancode

This can be done using the & operator:

nav {
  li {
    background:red;
    .newstate & {
      background:blue
    }
  }
}

compiles to:

nav li {
  background: red;
}
.newstate nav li {
  background: blue;
}

It would be nice if this usage was added to the documentation.

mmantel avatar Jan 13 '12 01:01 mmantel

This should definitely go in the documentation. I've been waiting for a while now for a function like this just to find out that it was already built-in. Now I feel dumb.

Anahkiasen avatar Jan 13 '12 14:01 Anahkiasen

It is documented and always has been, but the usage is not illustrated as inserting the ampersand & AFTER the nested selector, although I've seen a few other people talk about it.

matthew-dean avatar Jan 13 '12 21:01 matthew-dean

moved to bug on website

lukeapage avatar Oct 18 '12 11:10 lukeapage