Responsive fonts - auto-scaling the root font-size
The root font-size scales up and down proportionately, between defined values, dependent upon the viewport width.
Easy-peasy, stretch & squeezy.
Simplify your calculations:
Fluid-responsive font-size calculator
In this example the font size is set at 1rem (16px) up to 48em (768px) viewport width. It then starts increasing to meet the upper defined value 2rem (32px) at 120em (1920px) wide.
All controlled from a single CSS statement.
Remember to define all font-sizes in em, rem or percent.
/* 1rem @ 48em (768px) increasing to 2rem @ 120em (1920px) */
@media (min-width: 48em) {
:root {
font-size: calc(1rem + ((1vw - .48rem) * 1.389));
/* .48rem = viewportWidthMinimum /100 */
/* 1.389rem = 100 * fontSizeDifference / viewportWidthDifference */
}
}
Where:
fontSizeCalc = 1rem + (1vw - 48em / 100) * 100 * fontSizeDifference / viewportWidthDifference
fontSizeDifference = maxFontSize - minFontSize
= 2em - 1em ≡ 32px - 16px
= 1em ≡ 16px
viewportWidthDifference = viewportMax - viewportMin
= 120em - 48em ≡ 1920px - 768px
= 72em ≡ 1152px
Using pixels:
fontSizeCalc = 1rem + (1vw - 768px / 100) * 100 * 16px / 1152px
= 1rem + (1vw - 7.68px) * 1.389
Using em or rem:
fontSizeCalc = 1rem + (1vw - 48rem / 100) * 100 * 1em / 72em
= 1rem + (1vw - .48rem) * 1.389
Font scaling doesn't stop at the top setting but continues to increment at the same rate. This behaviour may be stopped, or adjusted further, by adding another media query:
/* Stop font scaling above 1920px */
@media (min-width: 120em) {
:root {
font-size: 2rem;
}
}
Tested on Mac and PC: Firefox, Safari, Chrome all behave.
One or two tweaks applied for IE.
Examples:
Demo: Responsive fonts applied to a typical layout .
Live usage in certain modules which appeared in Tesco's Food Love Stories in 2017 (headings, intro copy and 'More Stories' titles).