Skip to content

Instantly share code, notes, and snippets.

@jimyuan
Created May 14, 2015 01:42
Show Gist options
  • Save jimyuan/fd87f46e0010206018f7 to your computer and use it in GitHub Desktop.
Save jimyuan/fd87f46e0010206018f7 to your computer and use it in GitHub Desktop.
A unit convert function for scss
/// Convert one unit into another
/// @author Hugo Giraudel
/// @param {Number} $value - Initial value
/// @param {String} $unit - Desired unit
/// @return {Number}
/// @throw Error if `$unit` does not exist or if units are incompatible.
@function convert-unit($value, $unit) {
  $units: (
    'px': 0px,
    'cm': 0cm,
    'mm': 0mm,
    '%': 0%,
    'ch': 0ch,
    'in': 0in,
    'em': 0em,
    'rem': 0rem,
    'pt': 0pt,
    'pc': 0pc,
    'ex': 0ex,
    'vw': 0vw,
    'vh': 0vh,
    'vmin': 0vmin,
    'vmax': 0vmax,
    'deg': 0deg,
    'turn': 0turn,
    'rad': 0rad,
    'grad': 0grad,
    's': 0s,
    'ms': 0ms,
    'Hz': 0Hz,
    'kHz': 0kHz,
    'dppx': 0dppx,
    'dpcm': 0dpcm,
    'dpi': 0dpi,
  );
 
  @if map-has-key($units, $unit) {
    @return map-get($units, $unit) + $value;
  }
 
  @error "Unknown unit `#{$unit}`.";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment