Skip to content

Instantly share code, notes, and snippets.

@nickisnoble
Last active December 15, 2016 17:43
Show Gist options
  • Save nickisnoble/bb9643a9ce935bd7ad8a070e3a11702e to your computer and use it in GitHub Desktop.
Save nickisnoble/bb9643a9ce935bd7ad8a070e3a11702e to your computer and use it in GitHub Desktop.
Fluid column, static gutter SCSS Grid Generator with offset, push, & pull
$unit: 24px;
* {
&,
&:before,
&:after {
box-sizing: border-box;
}
& + & {
margin-top: $unit;
}
}
.debug { // add this class to body or container
// Depth mapping
* { background-color: rgba(255,0,0,.2); }
* * { background-color: rgba(0,255,0,.2); }
* * * { background-color: rgba(0,0,255,.2); }
* * * * { background-color: rgba(255,0,255,.2); }
* * * * * { background-color: rgba(0,255,255,.2); }
* * * * * * { background-color: rgba(255,255,0,.2); }
// make all columns very visible
[class*=col]{
box-shadow: inset 0 0 0 1px #000;
min-height: 30px;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<title>Grid Example</title>
</head>
<body class="debug">
<div class="container">
<div class="grid">
<div class="col-1-2 col-1-5">
a
</div>
<div class="col-1-2 col-1-5">
b
</div>
<div class="col-1-3 col-1-5">
c
</div>
<div class="col-1-3 col-1-5">
d
</div>
<div class="col-1-3 col-1-5">
e
</div>
</div>
<div class="grid">
<div class="col-2-5 push-1-5">
f
</div>
<div class="col-1-3 col-1-5 pull-2-5">
g
</div>
<div class="col-1-3 col-1-5">
h
</div>
</div>
<div class="grid">
<div class="col-1-5">
i
</div>
<div class="col-2-5 full">
j
</div>
<div class="col-1-5">
k
</div>
</div>
<div class="grid">
<div class="col-1-2">
l
</div>
<div class="col-1-2">
m
</div>
<div class="col-3-5 offset-1-5">
n
</div>
</div>
<div class="grid">
<div class="col-2-5 offset-1-5">
o
</div>
<div class="col-1-5 offset-1-5">
p
</div>
</div>
</div>
</body>
</html>
$grids: (
// columns: min-screen-width
2: 400px,
3: 500px,
5: 700px
);
.container{
margin: $unit/2 $unit;
}
.grid{
margin-left: -$unit/2;
margin-right: -$unit/2;
margin-top: 0;
@include clearfix;
}
[class*=col]{
float: left;
position: relative; // allow push / pull
width: calc(100% - #{$unit}); // set default width
// counteract negatives on left and right,
// top + bottom = 1 unit (no margin collapse due to float)
margin: $unit/2;
}
// Trigger grid generator
@each $grid, $breakpoint in $grids{
@include make-grid($grid, $breakpoint);
}
@mixin clearfix{
&:before,
&:after {
content: "";
display: block;
height: 0;
overflow: hidden;
}
&:after {
clear: both;
}
}
// Grid generator
@mixin grid-modifier($type, $count, $columns) {
$width: percentage($count/$columns);
@if $type == push {
left: if($width > 0, $width, auto);
} @else if $type == pull {
right: if($width > 0, $width, auto);
} @else if $type == offset {
margin-left: calc(#{$width} + #{$unit/2});
}
}
@mixin make-grid($columns, $min-width){
@media screen and (min-width: $min-width){
// Set columns
@for $i from 1 through $columns{
$ideal-width: percentage($i/$columns);
.col-#{$i}-#{$columns}{
width: calc(#{$ideal-width} - #{$unit});
}
}
// Set modifiers
@each $modifier in (pull, push, offset) {
@for $i from 0 through ($columns - 1) {
.#{$modifier}-#{$i}-#{$columns} {
@include grid-modifier($modifier, $i, $columns)
}
}
}
}
}
@nickisnoble
Copy link
Author

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