Tailwind clamp 🗜️

Leverage the the CSS clamp function in your Tailwind CSS project.

Features

Requirements

The current version of the plugin is designed to be used with Tailwind version 4. To use it with version 3 use version 3.x.

Installation

Install the plugin from npm:

npm i tailwind-clamp

Add the plugin in your main CSS file:

@import "tailwindcss";
@plugin "tailwind-clamp";

Configuration

The plugin allows two configuration options:

NameTypeDescriptionDefault value
minSize{string}Viewport size where the clamp starts.23.4375rem
maxSize{string}Viewport size where the clamp end.90rem

Value should be a css length (px, rem, em). The unit for both options need to match.

@import "tailwindcss";
@plugin "tailwind-clamp" {
  minSize: 25rem,
  maxSize: 80rem
};

Usage

The plugin relies on the arbitrary values syntax clamp-[...]. You need to pass at least three arguments separated by commas without whitespace, optionally you can also pass the minSize and the maxSize:

clamp-[<property>,<start>,<end>,[minSize,maxSize]]

Arguments

Examples

<div class="clamp-[px,20,40] clamp-[py,10,18]">
  Add some fluid padding here.
</div>

<div class="@container">
  <div class="clamp-[text,lg,3xl,@sm,@5xl] clamp-[py,2,4,@29.5rem,@82rem]">
    Add some fluid typography and padding to the content of the container.
  </div>
</div>

Note on spacing and sizing properties

All spacing and sizing properties (p, m, w, etc.) accept unitless numbers and .5 floating numbers that will get multiplied by the --spacing variable, analog to how Tailwind CSS handles spacing values.

<div class="clamp-[p,1,2]">
  This will generate a `padding` value of `1rem` at `minSize` and `2rem` at `maxSize`.
</div>

Supported properties

Credits & mentions

The plugin is based on the formula presented in this article.

See also fluid.tw by Max Barvian.