multiple nested selectors with variables in Less

后端 未结 3 1384
醉梦人生
醉梦人生 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:06

    Use a Ruleset

    If you define your heading group as a ruleset with a mixin call to set properties with, then you can do this:

    @headings: {h1,h2,h3,h4,h5,h6 {.setProps()}};
    
    
    & {
        .setProps() {
            & {
                some: rule;
            }
            a {
                color: inherit;
            }
            span {
                another: rule;
            }
        }
        @headings();
    }
    

    I've isolated the whole thing inside & just so the .setProps() can be localized (it would work without it, but it would be setting the .setProps() globally. Also, the nested & {} bracketing is not necessary, but I find that it helps show what the "default" for the @headings is going to be.

    This can be used sequentially, if desired, like so:

    & {
        .setProps() {  some: rule; }
        @headings();
    }
    & {
        .setProps() { a {color: inherit;}}
        @headings();
    }
    & {
        .setProps() { span {another: rule;}}
        @headings();
    }
    

    Both will output like so:

    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 span,
    h6 span {
      another: rule;
    }
    

提交回复
热议问题