clamp
to handle font sizing
Mike Foskett has created a very simple and helpful calculator for generating clamp values with several different options. You can find the Fluid-responsive property calculator here.
The
clamp
function in CSS allows us to set a minimum value, maximum value, and a value that the
font-size
should be when the value is in between the minimum and maximum. In our example for the heading, we're setting a
36px
minimum, a
56px
maximum, and a calculated value
calc(2.25rem + ((1vw - 3.6px) * 1.2821))
as our "middle" value.
The "middle" value in our clamp function is the value that will apply when the calculation renders a value that is between our defined minimum and maximum. Otherwise, if the calculation is too low the function defaults to the minimum value and if the calculation is too high the function will default to the maximum.
The
MDN article on
clamp
is a great resource to learn more. If you do not need to support IE,
clamp
has
excellent browser support
.
If you wanted to, you could also use the calculator to handle your
clamp
values in
rem
or even
em
in order to maintain a typescale. It's easiest to demonstrate clamp using pixel values though.
Note: The
calc
base value (
2.25rem
) MUST be stated in
rem
to maintain accessibility. Where:
1.2821 = 100 * font-size_difference / viewport_width_difference