_tools.font-size.scss 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ///* ========================================================================
  2. // #FONT-SIZE
  3. // ======================================================================== */
  4. // Generates a rem font-size (with pixel fallback) and a baseline-compatible
  5. // unitless line-height from a pixel font-size value. Basic usage is simply:
  6. //
  7. // @include inuit-font-size(18px);
  8. //
  9. // You can force a specific line-height by passing it as the second argument:
  10. //
  11. // @include inuit-font-size(16px, 1);
  12. //
  13. // You can also modify the line-height by increments, while staying in the
  14. // baseline grid, by setting the `$modifier` parameter. It takes a positive
  15. // or negative integer, and it will add or remove "lines" to the generated
  16. // line-height. This is the recomended way to do it, unless you really need
  17. // an absolute value. i.e.:
  18. //
  19. // // add 2 lines:
  20. // @include inuit-font-size(24px, $modifier: +2);
  21. //
  22. // // subtract 1 line:
  23. // @include inuit-font-size(24px, $modifier: -1);
  24. @mixin inuit-font-size($font-size, $line-height: auto, $modifier: 0, $important: false) {
  25. @if (type-of($font-size) == number) {
  26. @if (unit($font-size) != "px") {
  27. @error "`#{$font-size}` needs to be a pixel value.";
  28. }
  29. } @else {
  30. @error "`#{$font-size}` needs to be a number.";
  31. }
  32. @if ($important == true) {
  33. $important: !important;
  34. } @else if ($important == false) {
  35. $important: null;
  36. } @else {
  37. @error "`#{$important}` needs to be `true` or `false`.";
  38. }
  39. // We provide a `px` fallback for old IEs not supporting `rem` values.
  40. font-size: $font-size $important;
  41. font-size: ($font-size / $inuit-global-font-size) * 1rem $important;
  42. @if ($line-height == "auto") {
  43. // Define how many grid lines each text line should span.
  44. // By default, we set it to the minimum number of lines necessary
  45. // in order to contain the defined font-size, +1 for some breathing room.
  46. // This can be modified with the `$modifier` parameter.
  47. $lines: ceil($font-size / $inuit-global-baseline) + $modifier + 1;
  48. $line-height: $lines * $inuit-global-baseline;
  49. line-height: ($line-height / $font-size) $important;
  50. }
  51. @else {
  52. @if (type-of($line-height) == number or $line-height == "inherit" or $line-height == "normal") {
  53. line-height: $line-height $important;
  54. }
  55. @else if ($line-height != 'none' and $line-height != false) {
  56. @error "D’oh! `#{$line-height}` is not a valid value for `$line-height`."
  57. }
  58. }
  59. }