CSS Clamp LogoCSS Clamp

CSS Clamp Guide

clamp()is a CSS function that allows you to clamp a value between a minimum and maximum value. It is a great tool for spacing and typography and very underused in my opinion. If you're interested in the technical details, check out the deepdive, MDN docs and the W3C spec.

A short overview of the syntax:

clamp(minimum, preferred, maximum)

Lets take the following clamp as an example: clamp(1rem, 0.4rem + 3vw, 4rem). When resizing the viewport the clamped value will be at least 1 rem and at most 4 rem. Between viewport widths of 20 rem and 120 rem the value will be clamped (fluid) between 1 rem and 4 rem linearly.

To clamp or not to clamp?

There are a few things to consider when using clamp(), first lets take a look at the drawbacks:

And the benefits:

Real world example

Using max-width of 1200px. When scaling down the viewport there is no difference in margin from 1200px and down.
Using clamp of 1200px. When scaling down the viewport there is a nice, fluid margin all the way down to the minimum viewport width.