1

我有以下for循环:

@for $i from 1 through $columns {
    .tile#{$i}.noMargin {  
    margin:0;
     }
}

输出为:

 .threeColForm .tile1.noMargin {
  margin: 0; }
.threeColForm .tile2.noMargin {
  margin: 0; }
.threeColForm .tile3.noMargin {
  margin: 0; }
.threeColForm .tile4.noMargin {
  margin: 0; }
.threeColForm .tile5.noMargin {
  margin: 0; }
.threeColForm .tile6.noMargin {
  margin: 0; }
.threeColForm .tile7.noMargin {
  margin: 0; }
.threeColForm .tile8.noMargin {
  margin: 0; }
.threeColForm .tile9.noMargin {
  margin: 0; }
.threeColForm .tile10.noMargin {
  margin: 0; }
.threeColForm .tile11.noMargin {
  margin: 0; }
.threeColForm .tile12.noMargin {
  margin: 0; }

有没有办法写这个所以它会像这样输出:

.threeColForm .tile1.noMargin,
.threeColForm .tile2.noMargin,
.threeColForm .tile3.noMargin,
.threeColForm .tile4.noMargin,
.threeColForm .tile5.noMargin,
.threeColForm .tile6.noMargin,
.threeColForm .tile7.noMargin,
.threeColForm .tile8.noMargin,
.threeColForm .tile9.noMargin,
.threeColForm .tile10.noMargin,
.threeColForm .tile11.noMargin,
.threeColForm .tile12.noMargin {
  margin: 0; 
}

任何帮助表示赞赏。谢谢

我的CSS(仅供参考):

.threeColForm {
    //cols, gutter
    @include grid(12, 1);
}
4

1 回答 1

6

利用@extend

$columns: 12;

%no-margin {
    margin: 0;
}

@for $i from 1 through $columns {
    .tile#{$i}.noMargin {  
        @extend %no-margin;
    }
}

输出:

.tile1.noMargin, .tile2.noMargin, .tile3.noMargin, .tile4.noMargin, .tile5.noMargin, .tile6.noMargin, .tile7.noMargin, .tile8.noMargin, .tile9.noMargin, .tile10.noMargin, .tile11.noMargin, .tile12.noMargin {
  margin: 0;
}
于 2013-05-16T18:19:27.173 回答