5

I want to be able to access classes further up the dom tree from within a nested class using LESS CSS, see example:

HTML:

<html class="svg">
 <body>
  <div class="content">
    <div class="container">
      <div class="logo"></div>
    </div>
 </body>
</html>

LESS:

.container {
  .logo {
      background:url(/images/ws-logo.gif);
  }
}

I want to target the .svg class on the html tag from within the .logo nested rule, to keep things tidy instead of writing another rule like this:

.svg { 
  .container {
    .logo {
        background:url(/images/logo.svg);
    }
  }
}

So, ideally something like this:

 .container {
    .logo {
        background:url(/images/logo.gif);

        (some-symbol).svg {
           background:url(/images/svg-logo.svg);
        }
    }
 }

I'm using modernizr to detect svg support.

Anyone know if this is possible? Or have any recommendations?

2 Answers 2

7

Yes! (an update)

When I tested this here, it worked!

    .container { 
        .logo { 
            background:url(/images/logo.gif);
            .svg & {
                background:url(/images/svg-logo.svg);
            } 
        } 
    }
Sign up to request clarification or add additional context in comments.

13 Comments

imo unnecessary fuzz and makes all your selectors more inefficient. Besides the fact that you actually have to repeat the rule - what the OP wanted to avoid in the first place.
@Christoph--I tend to agree with you (which is why I upvoted your answer).
@Christoph--Actually, there was a more concise way using the & selector (one I did not realize would work until I tested it). I've updated my answer.
Although i would consider this horrible coding (it looks so incredibly hacky and is guaranteed to give the reviewer serious headaches) +1 for this nice solution! ;)
Now let's target .svg .container:hover .logo:before without writing a new rule :-D
|
2

This is not possible because you can't "step back" in the path to add another class to a parent. Instead, just write another rule:

.svg .container .logo,
/* or perhaps even simpler, however be aware of rule specificity*/
.svg .logo{
        background:url(/images/logo.svg);
}

It's not much of a deal, is it?

For the sake of completeness: You can reference to the actual element via the &-symbol. THis makes sense if you want to target pseudo-classes/elements or additional classes of the current element:

.container {
     .logo {
         /* styles for ".container .logo" */
     }
     &:hover .logo{
         /* styles for ".container .logo"
            The hover however is bound to the .container element
            equals the following selector: .container:hover .logo */
     }
}

3 Comments

You can step back to add a class to the parent with the & cominator in LESS, however, I do not believe you can step back another level with the grandparent.
Okay, you are technically correct. Yet it can become essentially a reference to the parent when you follow it with a nested class rule (like in my answer). Personally, your answer is going to be a bit more "concise" in code, the only potential advantage to nesting is to keep it all together in a nesting (which the OP seemed to want).
I agree that it could make sense if the class wasn't on the html tag in the OPs question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.