Skip to content

Instantly share code, notes, and snippets.

@daphotron
Last active December 27, 2015 04:59
Show Gist options
  • Select an option

  • Save daphotron/7271193 to your computer and use it in GitHub Desktop.

Select an option

Save daphotron/7271193 to your computer and use it in GitHub Desktop.
Testing em and rem mixins and functions on http://sassmeister.com/gist/7271193
<h1>Title</h1>
<div class="caption">
caption
</div>
<div class="big">
I'm a big font.
<p>
Paragraph paragraph paragraph paragraph paragraph paragraph paragraph.
</p>
</div>
// ----
// Sass (v3.3.0.rc.1)
// Compass (v0.13.alpha.10)
// ----
@import "compass";
//==============================================
// VARS
$rembase: 16;
$fontpx: 16; // Font size (px) baseline applied to <body> and converted to %.
//==============================================
// MIXINS
@function strip-unit($num) {
@return $num / ($num * 0 + 1);
}
@mixin rel($property, $value, $context: $base-font-size) {
$length: length($value);
@if type-of($value) == "number" {
@if ($property == "line-height") {
$value: strip-unit($value);
#{$property}: ($value/$context);
} @else {
$value: strip-unit($value);
#{$property}: ($value/$context + 0em);
}
} @else {
// Create list to append values to.
$allvalues: ();
@each $singleValue in $value {
@if $singleValue == 0 or type-of($singleValue) != "number" {
// Append 0 as is or strings such as 'auto'.
$allvalues: append($allvalues, $singleValue);
} @else {
$allvalues: append($allvalues, #{strip-unit($singleValue) / $context}em);
}
}
#{$property}: $allvalues;
}
}
// Only rel can be a function, rem returns two values.
@mixin rem($property, $sizeValue: $rembase) {
$length: length($sizeValue);
@if type-of($sizeValue) == "number" {
// If we want line-height to be unitless everywhere, do not use the rem function.
#{$property}: strip-unit($sizeValue) + px;
#{$property}: (strip-unit($sizeValue)/$rembase) + rem;
} @else {
// Create list to append values to.
$allvalues: ();
$allvaluespx: ();
@each $singleValue in $sizeValue {
@if $singleValue == 0 or type-of($singleValue) != "number" {
// Append 0 as is or strings such as 'auto'.
$allvaluespx: append($allvaluespx, $singleValue);
$allvalues: append($allvalues, $singleValue);
} @else {
$allvaluespx: append($allvaluespx, $singleValue + 0px);
$allvalues: append($allvalues, ( strip-unit($singleValue) / $rembase) + 0rem);
}
}
#{$property}: $allvaluespx;
#{$property}: $allvalues;
}
}
// Difference between functions and mixins: http://thesassway.com/advanced/pure-sass-functions
@function relfunc($value, $context: $fontpx) {
$length: length($value);
@if type-of($value) == "number" {
$value: strip-unit($value);
@return #{$value/$context}em;
} @else {
// Create list to append values to.
$allvalues: ();
@each $singleValue in $value {
@if $singleValue == 0 or type-of($singleValue) != "number" {
// Append 0 as is or strings such as 'auto'.
$allvalues: append($allvalues, $singleValue);
} @else {
$allvalues: append($allvalues, #{strip-unit($singleValue) / $context}em);
}
}
@return $allvalues;
}
}
//==============================================
// CSS
.big {
@include rel(font-size, 25)
}
.big p {
@include rel(font-size, 16, 25);
}
h1 {
@include rel(font-size, 26px);
@include rem(margin-bottom, 10px);
}
.caption {
@include rel(margin, 40 auto 40);
padding: relfunc(100);
}
.big {
font-size: 1.5625em;
}
.big p {
font-size: 0.64em;
}
h1 {
font-size: 1.625em;
margin-bottom: 10px;
margin-bottom: 0.625rem;
}
.caption {
margin: 2.5em auto 2.5em;
padding: 6.25em;
}
<h1>Title</h1>
<div class="caption">
caption
</div>
<div class="big">
I'm a big font.
<p>
Paragraph paragraph paragraph paragraph paragraph paragraph paragraph.
</p>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment