Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clamp font size using TailwindCSS?

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.

like image 255
swimmer Avatar asked Jun 04 '26 09:06

swimmer


2 Answers

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-clamp plugin, 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:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./app/**/*.{js,ts,jsx,tsx,mdx}",
    "./pages/**/*.{js,ts,jsx,tsx,mdx}",
    "./components/**/*.{js,ts,jsx,tsx,mdx}",

    // Or if using `src` directory:
    "./src/**/*.{js,ts,jsx,tsx,mdx}",
  ],
  theme: {
    extend: {
      fontSize: {
        clamp: "clamp(1rem, 5vw, 3rem)",
      },
    },
  },
  plugins: [],
};

To use it in a jsx or tsx component, one can just pass text-clamp as an additional className.

enter image description here

Link to working codesandbox.

like image 122
swimmer Avatar answered Jun 05 '26 22:06

swimmer


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>

like image 27
Metehan Altuntekin Avatar answered Jun 06 '26 00:06

Metehan Altuntekin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!