Created
July 28, 2018 14:51
-
-
Save jeremyHixon/1103a78649270ae19a0739796875f73d to your computer and use it in GitHub Desktop.
Sass functions to calculate the line height based off the font size, the content's width in px, and the golden ratio
This file contains 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
/** | |
* Calculates the line height based on font size and the content's width in px. Based | |
* off the research underlying Pearsonified's Golden Ratio Typography Calculator | |
* https://pearsonified.com/typography/ | |
* Ex: $font-size: 16px; | |
* $content-width: 1336px; | |
* $line-height: calculate-line-height( $font-size, $content-width ); | |
* | |
* @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 ) ) ) ) ); | |
} | |
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment