Skip to content

Commit c88f32d

Browse files
committed
Add Foundation units partial for px to rem calculations
1 parent 22a3109 commit c88f32d

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

assets/_sass/base/_units.scss

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Foundation for Sites by ZURB
2+
// foundation.zurb.com
3+
// Licensed under MIT Open Source
4+
5+
////
6+
/// @group functions
7+
////
8+
9+
// scss-lint:disable ZeroUnit
10+
11+
/// Defines the base font size of the page, which is the value `1rem` is equal to.
12+
/// @type Number
13+
/// @group global
14+
$rem-base: 16px !default;
15+
16+
/// Removes the unit (e.g. px, em, rem) from a value, returning the number only.
17+
///
18+
/// @param {Number} $num - Number to strip unit from.
19+
///
20+
/// @returns {Number} The same number, sans unit.
21+
@function strip-unit($num) {
22+
@return $num / ($num * 0 + 1);
23+
}
24+
25+
/// Converts one or more pixel values into matching rem values.
26+
///
27+
/// @param {Number|List} $values - One or more values to convert. Be sure to separate them with spaces and not commas. If you need to convert a comma-separated list, wrap the list in parentheses.
28+
/// @param {Number} $base [$rem-base] - The base value to use when calculating the `rem`. If you're using Foundation out of the box, this is 16px.
29+
///
30+
/// @returns {List} A list of converted values.
31+
@function rem-calc($values, $base: $rem-base) {
32+
$rem-values: ();
33+
$count: length($values);
34+
35+
@if $base == null {
36+
$base: $rem-base;
37+
}
38+
39+
@if $count == 1 {
40+
@return -zf-to-rem($values, $base);
41+
}
42+
43+
@for $i from 1 through $count {
44+
$rem-values: append($rem-values, -zf-to-rem(nth($values, $i), $base));
45+
}
46+
47+
@return $rem-values;
48+
}
49+
50+
// Converts a unitless, pixel, or rem value to em, for use in breakpoints.
51+
@function -zf-bp-to-em($value) {
52+
// Pixel and unitless values are converted to rems
53+
@if unit($value) == 'px' or unitless($value) {
54+
$value: rem-calc($value);
55+
}
56+
57+
// Then the value is converted to ems
58+
@return strip-unit($value) * 1em;
59+
}
60+
61+
/// Converts a pixel value to matching rem value. *Any* value passed, regardless of unit, is assumed to be a pixel value. By default, the base pixel value used to calculate the rem value is taken from the `$rem-base` variable.
62+
/// @access private
63+
///
64+
/// @param {Number} $value - Pixel value to convert.
65+
/// @param {Number} $base [$rem-base] - Base for pixel conversion.
66+
///
67+
/// @returns {Number} A number in rems, calculated based on the given value and the base pixel value. rem values are passed through as is.
68+
@function -zf-to-rem($value, $base: $rem-base) {
69+
// Calculate rem if units for $value is not rem
70+
@if (unit($value) != 'rem') {
71+
$value: strip-unit($value) / strip-unit($base) * 1rem;
72+
}
73+
// Turn 0rem into 0
74+
@if ($value == 0rem) { $value: 0; }
75+
@return $value;
76+
}

assets/css/main.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Imports
77
////////////////////
88

9+
@import "base/units";
910
@import "base/colors";
1011
@import "base/normalize";
1112
@import "base/additional-defaults";

0 commit comments

Comments
 (0)