Skip to content

Instantly share code, notes, and snippets.

@kevindees
Last active July 31, 2019 17:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevindees/6303d71844846e12661d7d1e260036f6 to your computer and use it in GitHub Desktop.
Save kevindees/6303d71844846e12661d7d1e260036f6 to your computer and use it in GitHub Desktop.
Simple grid css with flex fallback
$cols: 12;
$gutter: 20px;
// flex fallback
.grid-columns {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
width: 100%;
[class*=col-] { box-sizing: border-box; }
}
@for $col from 1 through $cols {
.col-#{$col} {
$pw: percentage($col/$cols);
width: calc(#{$pw} - #{$gutter - $gutter / $col });
}
}
// grid
@supports (display: grid) {
.grid-columns {
display: grid;
grid-template-columns: repeat($cols, 1fr);
grid-gap: $gutter;
> [class*=col-] { width: 100%; }
}
@for $col from 1 through $cols {
.col-#{$col} { grid-column: span $col; }
}
}
// multi row helpers for flex only
// place with .grid-columns where $col is number of cells in top row
// example - 4 cells - .grid-top-4
@for $col from 1 through $cols {
.grid-top-#{$col} > [class*=col-]:nth-child(1n+#{$col + 1}) {
margin-top: $gutter;
}
}
// responsive - example - specify what you need following the naming pattern
@media screen and (max-width: 500px) {
.col-max500-6 {
grid-column: span 6;
width: calc(50% - #{$gutter - $gutter / 2 });
}
}
@media screen and (min-width: 500px) {
.col-min500-6 {
grid-column: span 6;
width: calc(50% - #{$gutter - $gutter / 2 });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment