Skip to content

Instantly share code, notes, and snippets.

@imranomari
Last active August 29, 2015 14:02
Show Gist options
  • Save imranomari/2ae0a7b76c3715c69998 to your computer and use it in GitHub Desktop.
Save imranomari/2ae0a7b76c3715c69998 to your computer and use it in GitHub Desktop.
normalize margin and padding values (e.g: normalize-space(13px 0px 13px 0px). returns 13px 0 )
@function strip-unit($_value) {
@return $_value / ($_value * 0 + 1);
}
// normalize margin and padding values (e.g: normalize-space(13px 5px 13px 5px). returns 13px 5px).
@function normalize-space($_values, $_flip-if-rtl: false, $_dir: ltr) {
$len: length($_values);
$a: false;// all
$h: false;// horizontal
$v: false;// vertical
$t: 0;// top
$e: 0;// end (right in ltr)
$b: 0;// bottom
$s: 0;// start (left in ltr)
@if ($len == 1) {
$t: nth($_values, 1);
$e: $t;
$b: $t;
$s: $t;
}
@else if ($len == 2) {
$t: nth($_values, 1);
$e: nth($_values, 2);
$b: $t;
$s: $e;
}
@else if ($len == 3) {
$t: nth($_values, 1);
$e: nth($_values, 2);
$b: nth($_values, 3);
$s: $e;
}
@else if ($len == 4) {
$t: nth($_values, 1);
$e: nth($_values, 2);
$b: nth($_values, 3);
$s: nth($_values, 4);
}
@if ($_flip-if-rtl) and ($_dir == rtl) {
$temp: $e;
$e: $s;
$s: $temp;
}
$v: if($t == $b, $b, false);
$h: if($e == $s, $s, false);
$a: if($v == $h, $h, false);
$t: if( $t and (strip-unit($t) == 0), 0, $t);
$e: if( $e and (strip-unit($e) == 0), 0, $e);
$b: if( $b and (strip-unit($b) == 0), 0, $b);
$s: if( $s and (strip-unit($s) == 0), 0, $s);
$v: if( $v and (strip-unit($v) == 0), 0, $v);
$h: if( $h and (strip-unit($h) == 0), 0, $h);
$a: if( $a and (strip-unit($a) == 0), 0, $a);
$normalized-space: false;
@if ($a) {
$normalized-space: $a;
}
@else {
@if ($h) {
@if ($v) {
$normalized-space: $v $h;
}
@else {
$normalized-space: $t $h $b;
}
}
@else {
$normalized-space: $t $e $b $s;
}
}
@return $normalized-space;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment