Mixins for LESS and Sass (.scss) to make all page text —font sizes and line heights— responsive
Set your font-sizes and line-heights with these mixins, set a mobile scale factor, and all text on your page will scale down automagically.
Want some text to scale differently? Use a media query to give that text a different full-size value for mobile.
Want some text to always stay the same size? Want some text to keep its font size while scaling its line height? Want some text to keep its line height while scaling its font size? Just don't use the mixin for the things you want to keep constant, or override the mixin with an explicit style.
By default, these mixins scale mobile line height inversely to the mobile font size, to make reading small text easier. To have line height scale proportionally to font size, simply change the mobile-line-height to lh / fz * mobile-text-factor.
-
Sass (.scss): https://codepen.io/henry/pen/qXLwZa?editors=1100
-
Set variables
name argument default what is it? fz{number}, unitless14page base font-size (e.g. if designed at 14px, 14)lh{number}, unitless18page base line-height (e.g. if designed at 18px, use 18)mobile-font-size-factor{number}, unitless.74the factor by which to sclae font size on mobile mobile-line-height-factor{number}, unitlessmobile-font-size-factor * 5/4the factor by which to scale line height on mobile -
Set font-size and line-height with the mixins
mixin argument default what does it do? fz(){number}
pixel font size / 1pxfzsets font size lh(){number}
ratio of line height to font sizelhsets line height fzlh(){number,number}
arg1: unitless font size
arg2: unitless line heightfz, lhshorthand to set both fz()andlh()Note that on a consistently typeset site,
-
Setting font size to 20px and line height to 24px:
// LESS my-target { .fzlh(20,24) }
// Sass.scss my-target { @include fzlh(20,24) }
-
Setting font size to 20px:
// LESS my-target { .fz(20) }
// Sass.scss my-target { @include fz(20) }
-
Setting line spacing to double-spaced:
// LESS my-target { .lh(2)// }
// Sass.scss my-target { @include lh(2) }
-
