我有以下 SCSS mixin,它从.grid1
to生成 CSS 类.grid12
:
$columns: 12;
@mixin repeat($character, $n) {
$c: "";
@for $i from 1 through $n {
$c: $c + $character;
}
-ms-grid-columns: unquote($c);
}
// classes for defining number of equally sized (sub-)grid columns
@mixin grid-x {
@for $i from 1 through $columns {
.grid#{$i} {
@include repeat(" 1fr", $i);
}
}
}
@include grid-x;
使用 libsass 生成的 CSS 类如下所示:
.grid1 {
-ms-grid-columns: 1fr;
}
.grid2 {
-ms-grid-columns: 1fr 1fr;
}
.grid3 {
-ms-grid-columns: 1fr 1fr 1fr;
}
...
由于 IE 11 不支持grid-gap
我想在生成的列之间插入 20px 间隙列。
预期结果如下所示:
// nothing changed for .grid1
.grid1 {
-ms-grid-columns: 1fr;
}
.grid2 {
-ms-grid-columns: 1fr 20px 1fr;
}
.grid3 {
-ms-grid-columns: 1fr 20px 1fr 20px 1fr;
}
...
简单地20px
在重复字符串中添加列会导致不需要的尾随20px
:
@include repeat(" 1fr 20px", $i);
结果是
.grid1 {
-ms-grid-columns: 1fr 20px;
} ^^^^
.grid2 {
-ms-grid-columns: 1fr 20px 1fr 20px;
}^ ^^^^
.grid3 {
-ms-grid-columns: 1fr 20px 1fr 20px 1fr 20px;
} ^^^^
...
任何想法如何使用 SCSS mixins 获得所需的结果?
为了方便测试,只需将 mixins 粘贴到sassmeister上即可。