Last active
August 29, 2015 14:15
-
-
Save Da-Fecto/861e88fcb2127863ec8b to your computer and use it in GitHub Desktop.
Build a CSS fractural grid.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Build a CSS fractural grid. | |
* | |
* @var (int) $columns, Amount of columns, 10 columns means 10 columns of 10% width | |
* @var (string) $prefix, Classname prefix | |
*/ | |
function grid($columns = 12, $prefix = ".col-", $suffix = "") { | |
$decimals = 3; // Amount of decimals | |
$out = ''; | |
$array = array(); | |
// Group by value | |
foreach (range(1, $columns) as $range) { | |
foreach (range(1, $range) as $not_used => $step) { | |
$width = (string) floorDec( 100 / $range * $step, $decimals); | |
$classname = $prefix . $step . "-" . $range . $suffix; | |
$array[$width][] = $classname; | |
} | |
} | |
ksort($array, SORT_NUMERIC); | |
foreach ($array as $width => $classname) { | |
$out .= implode(", ", $classname) . ''; | |
$out .= " { width: " . $width . "%; }\n"; | |
} | |
return $out; | |
} | |
/** | |
* Floor with decimals by using the round() and substract an half. | |
* - Substract (0.05) for 1 decimal | |
* - Substract (0.0005) for 3 decimals etc. | |
*/ | |
function floorDec($input, $decimals) { | |
return round($input - (5 / pow(10, $decimals + 1)), $decimals); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment