Skip to content

Instantly share code, notes, and snippets.

@robinloeffel
Last active January 15, 2020 22:36
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save robinloeffel/ef3910b8a897aa88447c8a3809cbc9b0 to your computer and use it in GitHub Desktop.
Save robinloeffel/ef3910b8a897aa88447c8a3809cbc9b0 to your computer and use it in GitHub Desktop.
Dynamic CSS Grid Items in Internet Explorer and Edge - https://grid-ie-calc.surge.sh
/*
This mixin allows us use CSS grid without having to think about
what -ms-grid-row/-ms-grid-column we have to assign to a grid element
for it to properly work on Internet Explorer and Edge.
It takes three arguments, the last one of which is optional. Specify the
maximum amount of items you want to have in your grid, when they should
break to the next line and, if you like, a grid-gap of some sort.
Make sure to use the same amount of arguments in -ms-grid-columns as in
$wrap-on, so it correctly breaks the line, eg. '1fr 2fr 1fr 2fr 1fr' and
'5'.
Usage:
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 10px;
display: -ms-grid;
-ms-grid-columns: 1fr 1fr 1fr;
&-item {
@include grid-ie-calc(15, 3, 10px);
}
}
Tested on:
IE 11, Edge 38, Chrome 58, Firefox 53, Safari 10, Opera 45
*/
@mixin grid-ie-calc($items: 30, $wrap-on: 3, $grid-gap: 0) {
$current-row: 1;
$current-column: 1;
@if $grid-gap > 0 {
& {
margin: $grid-gap / 2;
@supports (grid-gap: $grid-gap) {
margin: 0;
}
}
}
@for $i from 1 through $items {
@if $current-column > $wrap-on {
$current-column: 1;
$current-row: $current-row + 1;
}
&:nth-child(#{$i}) {
-ms-grid-row: $current-row;
-ms-grid-column: $current-column;
}
$current-column: $current-column + 1;
}
}
@windycitymoon
Copy link

Nice one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment