【问题标题】:Grouping Sass Selectors When Using @for使用 @for 时对 Sass 选择器进行分组
【发布时间】:2019-05-18 09:21:26
【问题描述】:

我有一组标题级别(1 到 6)。我正在使用@for 循环,但我似乎无法弄清楚如何将它们组合为一个而不是单独。

这是我正在使用的@for 循环:

@for $i from 1 through 6 {
   h#{$i},
   .h#{$i} {
      margin-bottom: $headings-margin-bottom;
      line-height: $headings-line-height;
      font-weight: $headings-font-weight;
      font-family: $headings-font-family;
   }
}

这是我所期待的:

h1,
h2,
h3,
h4,
h5,
h6 {
   /* styles */
}

这是正在编译的内容:

h1 {
   /* styles */
}

h2 {
   /* styles */
}

h3 {
   /* styles */
}

...

【问题讨论】:

    标签: css web sass


    【解决方案1】:

    你可以使用@extend

    %myStyle {
      margin-bottom: $headings-margin-bottom;
      line-height: $headings-line-height;
      font-weight: $headings-font-weight;
      font-family: $headings-font-family;
    }
    
    @for $i from 1 through 6 {
       h#{$i},
       .h#{$i} {
          @extend %myStyle;
       }
    }
    

    @extend 将允许您加入选择器。符号%placeholder selector,您可以使用它,这样myStyle 就不会出现在编译后的CSS 中。

    More info here

    【讨论】:

    • 完美,非常感谢@Arkellys,也感谢您提供的链接:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2016-12-02
    • 2011-11-12
    相关资源
    最近更新 更多