How can the clamp() CSS function be used with TailwindCSS to make the fontSize linearly scale between a min and a max value?
Interested in particular about integrating with Next.js.
How can the clamp() CSS function be used with TailwindCSS to make the fontSize linearly scale between a min and a max value?
Interested in particular about integrating with Next.js.
Metehan Altuntekin
On
There is no Tailwind utility class to do this easily but you can do this by writing custom CSS with Arbitrary properties
Example:
<h1 class="[font-size:_clamp(2em,5vw,10em)]">Text</h1>
Copyright © 2021 Jogjafile Inc.
At the time of writing, TailwindCSS doesn't seem to support
clamp()natively by default.Search returns a use case on multi-line truncation using the
line-clampplugin, which is different from clamping font sizes.Taking inspiration from this GitHub issue, using
clamp()in TailwindCSS can be accomplished by extending the default theme. Example in a Next.js app:To use it in a
jsxortsxcomponent, one can just passtext-clampas an additionalclassName.Link to working codesandbox.