Skip to content

Instantly share code, notes, and snippets.

@lunelson
Created February 3, 2013 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lunelson/4702284 to your computer and use it in GitHub Desktop.
Save lunelson/4702284 to your computer and use it in GitHub Desktop.
GCD and Reduce to chain extension of column width classes
// Sass v3.2.5
@function gcd($n,$d) {
$numerator: if($n < $d,$d,$n);
$denominator: if($n < $d,$n,$d);
$remainder: $numerator;
$last_remainder: $numerator;
@while $remainder != 0 {
$last_remainder: $remainder;
$remainder: $denominator % $numerator;
$denominator: $numerator;
$numerator: $remainder;
}
@if $last_remainder != 0 {
@return $last_remainder;
}
}
@function reduce($n,$d) {
$gcd: gcd($n,$d);
@return ($n/$gcd,$d/$gcd)
}
$columns: 24;
@for $d from 1 through $columns {
@for $n from 1 through $d {
$ratio: reduce($n,$d);
$x: nth($ratio,1); $y: nth($ratio,2);
%span-#{$n}-#{$d} {
@if $x < $n {
@extend %span-#{$x}-#{$y};
} @else {
width: $n/$d*100%;
}
}
}
}
.test {
@extend %span-3-4;
}
.test2 {
@extend %span-9-12;
}
.test2, .test {
width: 75%; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment