Skip to content

Instantly share code, notes, and snippets.

@LoicGoyet
Last active March 8, 2016 09:31
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 LoicGoyet/623612833c3405560792 to your computer and use it in GitHub Desktop.
Save LoicGoyet/623612833c3405560792 to your computer and use it in GitHub Desktop.
A sass function that returns a value to apply to a margin in order to vertically or horizontally align two elements.
/// Contexts
/// @author Loïc Goyet
/// @param {Unit} $element1
/// @param {Unit} $element2
/// @param {Bool} $invert
/// @warn the params $big-element & $small-element must share the same type of
/// unit
@function get-diff-to-align($element1, $element2, $invert: true) {
$big-element: if($element1 > $element2, $element1, $element2);
$small-element: if($element1 > $element2, $element2, $element1);
$diff: ($big-element - $small-element); // get the strict difference
$diff: round($diff / 2); // divide the diff by two for each side
$diff: if($invert, $diff * -1, $diff);
@return $diff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment