How Clamp function Works

The clamp() CSS function clamps a middle value within a range of values between a defined minimum bound and a maximum bound. The function takes three parameters: a minimum value, apreferred value, and a maximum value.

clamp(minimum, preferred, maximum)

  • The minimum is the smallest value that the function will return.
  • The preferred is the value that the function will return if it falls within the specified range.
  • The maximum is the largest value that the function will return.

Advantage of using clamp calculator

Using a calculator, you can determine the optimal preferred value that will adjust linearly across a specified viewport width. The value will increase gradually as the viewport width expands. Let’s explore this further with an example.

Imagine you want a font size of 1.5rem on mobile devices and 3rem on desktop devices. Most mobile devices have a viewport width of less than 450px, while most desktop devices have a viewport width greater than 1024px.

If you enter these values into the calculator with the default rem base, you'll get the following result:

font-size: clamp(1.5rem, 0.324rem + 4.181vw, 3rem);

This is how the example works

See Demo on Codepen

Disadvantage of using clamp

In my opinion, the main disadvantage is that you can't determine the viewport range just by looking at the clamp function. To address this, I decided to add a comment to the code that provides an explanation. You can generate the comment using the calculator.