Skip to content

Instantly share code, notes, and snippets.

@amiut
Created June 14, 2018 20:12
Show Gist options
  • Save amiut/3e3e88a4937692cc9822eb66a44c08e2 to your computer and use it in GitHub Desktop.
Save amiut/3e3e88a4937692cc9822eb66a44c08e2 to your computer and use it in GitHub Desktop.
Convert px values to rem in SASS
$html-base-font-size = 12px;
@function rem( $px ){
@if( unit( $px ) == 'rem' ){
@return $px;
}
@else if( unit($px) == 'px' ) {
@return ( $px / $html-base-font-size ) + 0rem;
}
@else if( unit( $px ) == '' ) {
@return ( ( $px + 0px ) / $html-base-font-size ) + 0rem;
}
@else {
@return $px;
}
}
.example{
font-size: rem(14px);
font-size: rem(16); // unitless values are considered to be px
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment