About CSS Clamp Generator
Give the size you want on a narrow screen, the size you want on a wide one, and the two viewport widths where each should apply. This tool works out the straight line between them and writes it as a clamp(): the smaller size as the floor, a rem-plus-vw preferred value, and the larger size as the ceiling.
It works for any length, not only type: padding, gaps, margins and widths. A table shows the exact size at common device widths, and a preview draws it at the two widths you chose and halfway between.
How it works
- The slope is the change in size divided by the change in viewport width. One hundredth of the viewport width is 1vw, so the slope times 100 is the vw part of the preferred value.
- The constant part is the size the line would give at a zero-width viewport. It is written in rem (or px), and as a subtraction when it is negative.
- The smaller of the two sizes becomes the first argument of clamp() and the larger the last, so the value also works when it shrinks as the screen widens.
- Numbers are rounded to four decimal places, which is far below one pixel at any realistic width.
Questions
Why is the preferred value a rem plus a vw?
A vw value alone would be fixed to the screen and ignore a reader’s font size setting. Adding a rem part keeps some of the size tied to the root font size, and it is what makes the line pass through both of your sizes.
Does a fluid font size cause accessibility problems?
It can. Browser zoom enlarges rem but not vw, so text made mostly of vw grows less when someone zooms. Keeping the large size within about 2.5 times the small one lets text still reach 200%. The tool warns you when a font size goes past that.
Which browsers support clamp()?
All current versions of Chrome, Edge, Firefox and Safari, and every browser released since 2020. Very old browsers ignore the whole declaration, so put a plain fallback value before it if you still support them.
What root font size should I use?
Use the size your stylesheet sets on the html element. If you do not set one, browsers use 16px, which is the default here.
Everything you enter here is processed in this browser tab. Nothing is uploaded, logged or shared. How privacy works here.