multiple nested selectors with variables in Less

后端 未结 3 1376
醉梦人生
醉梦人生 2020-12-20 07:23

I want to build some CSS along these lines:

h1,h2,h3,h4,h5,h6 {some rule}
h1 a,h2 a,h3 a,h4 a,h5 a,h6 a {color: inherit;}
h1 span,h2 span,h3 span,h4 span,h5         


        
3条回答
  •  自闭症患者
    2020-12-20 08:12

    #1 Just yet one more solution in addition to @helderdarocha's answer and those given in https://stackoverflow.com/a/23954580/2712740. Maybe be this one could look a bit more clear:

    // define header list as usual just
    // put a mixin call with some predefined name there
    h1, h2, h3, h4, h5, h6 {.headings}
    
    // now to add styles/childs to the header list just add the mixin definitions:
    
    .headings() {
        some: rule;
    }
    
    .headings() {
      a {color: inherit}
    }
    
    .headings() {
      span {another: rule}
    }
    
    // etc.
    

    The limitation of this solution is that h1, h2, h3 ... {} and .headings should be defined at the same level. Additionally, it's important to keep in mind that all these styles will output to CSS at the point of h1, h2, h3 ... {} definition not at the point of .headings definitions, so it may break your cascading overrides if you have some).


    #2 The alt. solution I'm copy-pasting from https://stackoverflow.com/a/23954580/2712740 #3, basicaly it's the same as #1 but w/o its limitations (just having more special scary symbols):

    // the "variable":
    .headings(@-) {
        h1, h2, h3, h4, h5, h6
            {@-();}}
    
    
    // usage:
    
    .headings({
        some: rule;
    });
    
    .headings({
        a {color: inherit}
    });
    
    .headings({
        span {another: rule}
    });
    
    //etc.
    

提交回复
热议问题