Skip to content

Instantly share code, notes, and snippets.

@jeremyHixon
Created August 24, 2016 18:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jeremyHixon/021e7e938f06abfc307a875371cc57e0 to your computer and use it in GitHub Desktop.
Save jeremyHixon/021e7e938f06abfc307a875371cc57e0 to your computer and use it in GitHub Desktop.
/**
* Calculates numbers to the mathmatical power (exponent)
*
* @since 1.0.0
*
* @param int $number The number to increase
* @param int $exponent The power to increase the number by
*
* @return int The new number
*/
@function pow( $number, $exponent ) {
$value: 1;
@if $exponent > 0 {
@for $i from 1 through $exponent {
$value: $value * $number;
}
}
@return $value;
}
/**
* Removes units from values for easier modification
*
* @since 1.0.0
*
* @param int $number The number to strip units from
*
* @return int The number without the unit
*/
@function strip-unit( $number ) {
@if type-of( $number ) == 'number' and not unitless( $number ) {
@return $number / ( $number * 0 + 1 );
}
@return $number;
}
/**
* Calculates the line height based on multiple parameters
*
* @since 1.0.0
*
* @param int $font-size The font size
* @param int $content-width The width of the content area
* @param int $ratio The ratio. Default 1.61803398875 (golden ratio)
*
* @return int The calculated line height
*/
@function calculate-line-height( $font-size, $content-width, $ratio: 1.61803398875 ) {
$font-size: strip-unit( $font-size );
$content-width: strip-unit( $content-width );
@return $ratio - ( ( 1 / ( 2 * $ratio ) ) * ( 1 - ( $content-width / ( pow( ( $font-size * $ratio ), 2 ) ) ) ) );
}
@brokerUA
Copy link

Thanks! I will try to use your code in current project.
I'm using Bootstrap 4 and extended it mixin with native media-query values:

@mixin font-size($size){
  font-size: $size + px;

  @media (max-width: 575px) {
    line-height: calculate-line-height( $size, 575px );
  }
  @media (max-width: 767px) {
    line-height: calculate-line-height( $size, 767px );
  }
  @media (max-width: 991px) {
    line-height: calculate-line-height( $size, 991px );
  }
  @media (max-width: 1199px) {
    line-height: calculate-line-height( $size, 1199px );
  }

};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment