Fluid typography and CSS math functions
July 09, 2021
The CSS math functions min(), max() and clamp() can be used for responsive font sizes. In this blog post I want to reveal to you the logic behind fluid typography and offer an interactive code generator, so you can easily experiment with the settings.
Specifying the font size in vw units allows us to scale make the font size resond to the width of the viewport. vw (viewport width) is defined as 1/100 of the viewport width. To calculate the correct font size in vw units we need to know the preferred font size at a specific viewport width. For example, if we want a font size of 24 pixels at a 1200 pixel wide screen that would translate to 24 / (1200 / 100) = 2vw. Use the code generator below to get the right vw value for any given viewport width and font size.
This font size is
/* edit the numbers above and resize the window */font-size: 3.75vw
This font size is
/* edit the numbers above and resize the window */font-size: min(6.25vw, 48px)
This font size is
/* edit the numbers above and resize the window */font-size: clamp(24px, 6.25vw, 48px)