LESS CSS - accessing classes further up the dom tree from within a nested class

后端 未结 2 941
天命终不由人
天命终不由人 2020-12-19 13:15

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

HTML:


         


        
相关标签:
2条回答
  • 2020-12-19 13:33

    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 */
         }
    }
    
    0 讨论(0)
  • 2020-12-19 13:54

    Yes! (an update)

    When I tested this here, it worked!

        .container { 
            .logo { 
                background:url(/images/logo.gif);
                .svg & {
                    background:url(/images/svg-logo.svg);
                } 
            } 
        }
    
    0 讨论(0)
提交回复
热议问题