I have the following SCSS mixin which generates CSS classes from .grid1
to .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;
The generated CSS classes look like this using libsass:
.grid1 {
-ms-grid-columns: 1fr;
}
.grid2 {
-ms-grid-columns: 1fr 1fr;
}
.grid3 {
-ms-grid-columns: 1fr 1fr 1fr;
}
...
Since IE 11 doesn't support grid-gap
I want to insert 20px gap columns between columns in the generation.
The expected result would look like this:
// 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;
}
...
Simply adding the 20px
columns in the repeat string leads to an unwanted trailing 20px
:
@include repeat(" 1fr 20px", $i);
results in
.grid1 {
-ms-grid-columns: 1fr 20px;
} ^^^^
.grid2 {
-ms-grid-columns: 1fr 20px 1fr 20px;
}^ ^^^^
.grid3 {
-ms-grid-columns: 1fr 20px 1fr 20px 1fr 20px;
} ^^^^
...
For easy testing, just paste the mixins on sassmeister.
You can include another variable for the gap and do it like this.
$columns: 12;
@mixin repeat($character, $gap, $n) {
$c: "";
@if ($n != 1) {
@for $i from 1 through ($n - 1) {
$c: $c+" "+$character+" "+$gap ;
}
}
$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","20px", $i);
}
}
}
@include grid-x;
This will also give you the initial result if you specify and empty string in the gap: