Fluid interfaces. From mobile to desktop.
Tailwind CSS plugin to build responsive interfaces that scale smoothly using CSS clamp() without breakpoint micromanagement.
- Clamp values between a min and max viewport width, making it grow / shrink with the viewport.
- Possibility to use small to large, large to small, negative to positive, positive to negative and negative to negative values. (Negative values only work on properties that allow them, e.g.
margin) - Supports
px,remandemunits. - Supports
textvalues with multiple properties (fontSize,lineHeight,letterSpacing). IflineHeightis definded as a unitless number or acalc()function, the resulting value is calculated and converted to thefontSizeunit. - Supports using Tailwind CSS theme values, arbitrary values or a combination.
- Supports container queries.
- Supports CSS custom properties (
--*) as the target property, to storeclamp()values in variables for reuse. - Supports defining clamped theme variables via
@theme { --clamp-*: start, end; }.
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.
Install the plugin from npm:
npm i tailwind-clampAdd the plugin in your main CSS file:
@import "tailwindcss";
@plugin "tailwind-clamp";The plugin allows two configuration options:
| Name | Type | Description | Default 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;
}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]]
propertyProperty that the value should be applied to. See a list of all supported properties below.startValue atminSizeviewport size. It can be a key from your Tailwind CSS config file or a a css length (px,rem,em), the unit will need to matchend.endValue atmaxSizeviewport size. It can be a key from your Tailwind CSS config file or a css length (px,rem,em), the unit will need to matchstart.[minSize=23.4375rem]Viewport or container size, where the clamp starts, defaults to23.4375rem(375px). It can be a breakpoint or container size name from your theme or a css length (px,rem,em). Container size names and values are prefixed with@. The unit will need to matchmaxSizeand be smaller thanmaxSize.[maxSize=90rem]Viewport or container size, where the clamp stops, defaults to90rem(1440px). It can be a breakpoint or container size name from your theme or a css length (px,rem,em). Container size names and values are prefixed with@. The unit will need to matchminSizeand be be larger thanminSize.
<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>If you only want to override maxSize while keeping the default minSize, you can leave the fourth argument empty:
<div class="clamp-[p,2,4,,md]">
Padding scales from the default minSize (375px) up to the `md` breakpoint.
</div>This is equivalent to writing clamp-[p,2,4,23.4375rem,md] but saves you from having to remember the default value.
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>You can use CSS custom properties (--*) as the target property to store a clamp() value in a CSS variable for reuse in calc() expressions or other properties.
clamp-[--variable-name,start,end]
Note
Only explicit CSS lengths (px, rem, em) are accepted as values — theme tokens and unitless numbers are not supported for custom properties.
<body class="clamp-[--blockspace,2rem,6rem]">
<header class="[--header-height:80px] fixed top-0">...</header>
<section class="mt-[calc(var(--header-height)+var(--blockspace))]">
Content with fluid spacing that accounts for the fixed header.
</section>
</body>You can define clamped values directly in your @theme block using the --clamp-* namespace. The plugin will compute the clamp() formula and inject the result as a CSS custom property.
@theme {
--clamp-blockspace: 2rem, 6rem;
--clamp-display: 2rem, 4rem;
}You can also pass custom viewport breakpoints as the third and fourth values:
@theme {
--clamp-narrow-space: 1rem, 3rem, 20rem, 80rem;
}Theme clamp variables can be referenced by other theme variables. This lets you use computed clamp values for built-in Tailwind theme tokens like font sizes, spacing, and more:
@theme {
--clamp-blockspace: 2rem, 6rem;
--clamp-display: 2rem, 4rem;
--font-size-display: var(--clamp-display);
--spacing-section: var(--clamp-blockspace);
}With the above theme, you can use the standard Tailwind utilities directly:
<h1 class="text-display">Fluid heading</h1>
<section class="p-section">Fluid padding from the theme</section>You can also reference the clamp variables directly in utilities using the () shorthand syntax:
<section class="mt-(--clamp-blockspace)">
Content with fluid spacing defined in the theme.
</section>Or in custom CSS:
.hero {
padding: var(--clamp-blockspace);
font-size: var(--clamp-display);
}Note
Only explicit CSS lengths (px, rem, em) are accepted — unitless numbers and theme tokens are not supported in theme variable definitions.
pincludingpt,pb,pl,pr,px,py,ps,pe,pbs,pbe.mincludingmt,mb,ml,mr,mx,my,ms,me,mbs,mbe.space-xandspace-yinsetincludinginset-x,inset-y,inset-bs,inset-be.topleftandstart.rightandend.bottomtextincludingfont-size,line-heightandletter-spacingif defined.gapincludinggap-x,gap-y.wandinlinehandblocksizemin-w,min-h,min-inline,min-blockmax-w,max-h,max-inline,max-blockbasisindentroundedincludingrounded-s,rounded-ss,rounded-se,rounded-e,rounded-ee,rounded-es,rounded-t,rounded-r,rounded-b,rounded-l,rounded-tl,rounded-tr,rounded-bl,rounded-br.translate-xandtranslate-ytext-strokestrokeleadingtrackingborderincludingborder-t,border-b,border-l,border-r,border-x,border-y,border-s,border-e,border-bs,border-be.outlineandoutline-offsetscroll-mincludingscroll-mx,scroll-my,scroll-ms,scroll-me,scroll-mt,scroll-mb,scroll-ml,scroll-mr,scroll-mbs,scroll-mbescroll-pincludingscroll-px,scroll-py,scroll-ps,scroll-pe,scroll-pt,scroll-pb,scroll-pl,scroll-pr,scroll-pbs,scroll-pbedecorationunderline-offset--*(CSS custom properties)
If you use tailwind-merge to resolve conflicting Tailwind classes, install the tailwind-clamp-merge plugin so that clamp utilities are correctly merged with their static counterparts:
npm i tailwind-clamp-mergeconst twMerge = extendTailwindMerge(withTailwindClamp);This teaches tailwind-merge that clamp-[p,1,3] belongs to the same class group as p-4, so conflicts are resolved correctly:
twMerge('p-4 clamp-[p,1,3]')
// => 'clamp-[p,1,3]'
twMerge('clamp-[p,1,3] p-4')
// => 'p-4'
twMerge('text-lg clamp-[text,lg,3xl]')
// => 'clamp-[text,lg,3xl]'
// Hierarchical conflicts work too
twMerge('px-4 py-2 clamp-[p,1,3]')
// => 'clamp-[p,1,3]'
twMerge('w-4 h-8 clamp-[size,10,20]')
// => 'clamp-[size,10,20]'All supported properties and their conflict hierarchies (e.g. p vs px/py, size vs w/h, rounded vs rounded-tl, border vs border-t) are handled automatically.
The plugin is based on the formula presented in this article.
See also fluid.tw by Max Barvian.
MIT