LESS CSS nesting classes

后端 未结 3 1679
终归单人心
终归单人心 2020-12-02 10:31

I\'m using LESS to improve my CSS and am trying to nest a class within a class. There\'s a fairly complicated hierarchy but for some reason my nesting doesn\'t work. I have

相关标签:
3条回答
  • 2020-12-02 10:38

    The & character has the function of a this keyword, actually (a thing I did not know at the moment of writing the answer). It is possible to write:

    .class1 {
        &.class2 {}
    }
    

    and the CSS that will be generated will look like this:

    .class1.class2 {}
    

    For the record, @grobitto was the first to post this piece of information.


    [ORIGINAL ANSWER]

    LESS doesn't work this way.

    .class1.class2 {} - defines two classes on the same DOM node, but

    .class1 {
        .class2 {}
    }
    

    defines nested nodes. .class2 will only be applied if it is a child of a node with the class class1.

    I've been confused with this too and my conclusion is that LESS needs a this keyword :).

    0 讨论(0)
  • 2020-12-02 10:43
    .g {
        &.posted {
        }
    }
    

    you should add "&" before .posted

    0 讨论(0)
  • 2020-12-02 10:43

    If the ampersand is located right next to the child element in nesting, it is compiled into a double class selector. If there is space between & and selector it will be compiled into child selector. Read more about nesting in Less here.

    0 讨论(0)
提交回复
热议问题