Last active
January 4, 2016 04:49
-
-
Save pospi/8570877 to your computer and use it in GitHub Desktop.
Calculate sizes in EMs easily by converting from other units
This file contains hidden or 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
@BASE_FONT_SIZE : 16px; | |
// translate units by parent element ratio | |
.emsize(@property, @desired, @base : @BASE_FONT_SIZE) { | |
@{property}: 1em * (unit(@desired) / unit(@base)); | |
} | |
// a wrapper for setting font size | |
.emfz(@desired, @base : @BASE_FONT_SIZE) { | |
.emsize(font-size, @desired, @base); | |
} | |
// another helper which can be used inline. eg- padding: 0 ~`@{toem}(20, 16)`; | |
@toem: ~`fn = function(a, b) { return (parseFloat(a) / parseFloat(b)) + 'em'; }`; |
This file contains hidden or 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
$BASE_FONT_SIZE : 16px; | |
@function strip-units($value) { | |
@return $value / ($value * 0 + 1); | |
} | |
// translate units by parent element ratio | |
@function emsize($desired, $base : $BASE_FONT_SIZE) { | |
@return 1em * (strip-units($desired) / strip-units($base)); | |
} | |
// a wrapper for setting font size | |
@mixin emfz($desired, $base : $BASE_FONT_SIZE) { | |
font-size: emsize($desired, $base); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment