Fluid type & spacing
clamp() scales that grow smoothly between screen sizes.
What it is
Instead of picking a size per breakpoint, give a fluid value a smallest size, a largest size and the widths between which it grows: clamp(min, preferred, max). The preferred part mixes rem, so zooming and the browser's text-size setting still work, with cqi, 1% of the nearest container's width, or of the viewport when there's no container. Put the values in @theme as --text-fluid-* and --spacing-fluid-*, and Tailwind makes text-fluid-lg, p-fluid-lg, gap-fluid-sm and friends. The heading at the top of this page uses text-fluid-2xl.
When to use it
- Headings and hero text, which need the biggest change between phone and desktop
- Section padding and gaps, so space scales with the type
- Keep body text nearly fixed: a small range, never below 16px
1. A page in fluid tokens
Grows with its box
No breakpoints, no jumps: every size follows the width in a straight line.
Body text grows a little, headings a lot. That keeps the hierarchy clear on a phone without the headline swallowing the screen, and lets it breathe on a wide column.
A caption, in text-fluid-sm.
| Token | Now | Range |
|---|---|---|
| text-fluid-4xl | … | 28 → 52px |
| text-fluid-lg | … | 18 → 24px |
| text-fluid-base | … | 16 → 18px |
| text-fluid-sm | … | 14 → 16px |
| spacing-fluid-lg | … | 20 → 64px |
| spacing-fluid-sm | … | 12 → 24px |
The readings come from the browser (getComputedStyle) each time the box resizes, and stay inside each token's range however far you drag.
2. Stepped vs fluid
text-2xl @md:text-4xl @2xl:text-5xl
Stepped heading
text-fluid-4xl
Fluid heading
Breakpoints hold a size, then jump at @md and @2xl. The fluid heading passes through every size in between.
3. Make your own
@theme {
--text-fluid-custom: clamp(1.125rem, 0.4375rem + 3.4375cqi, 2.5rem);
}Sized by your clamp()
…
The same function generated this site's tokens, and a unit test checks the stylesheet still matches it.
Show code · src/lib/fluid.ts
// Fluid sizes: a value that grows in a straight line from `min` at one width
// to `max` at another, and holds steady outside that range. In CSS that's
// clamp(min, intercept + slope × width, max).
export type Fluid = { min: number; max: number; from: number; to: number }; // all in px
const ROOT = 16; // px per rem
const num = (n: number) => String(Number(n.toFixed(4)));
const slopeOf = ({ min, max, from, to }: Fluid) => (max - min) / (to - from);
/**
* The clamp() for a fluid size. The middle term keeps a rem part, so browser
* zoom and text-size settings still scale it (WCAG 1.4.4). The width part is
* in cqi: 1% of the nearest container's inline size, or of the viewport when
* there's no container, so one token works for pages and components alike.
*/
export function fluidClamp(f: Fluid) {
const slope = slopeOf(f);
const intercept = f.min - slope * f.from;
return `clamp(${num(f.min / ROOT)}rem, ${num(intercept / ROOT)}rem + ${num(slope * 100)}cqi, ${num(f.max / ROOT)}rem)`;
}
/** The size in px at a given container width: what the browser computes. */
export function fluidAt(f: Fluid, width: number) {
return Math.min(f.max, Math.max(f.min, f.min + slopeOf(f) * (width - f.from)));
}
// This site's fluid tokens, as defined in @djmiskey/lab-tokens/tailwind.css (a test keeps the two in
// step). Text grows 320 → 960px wide; that's a phone up to the content column.
const range = { from: 320, to: 960 };
export const fluidTokens: Record<string, Fluid> = {
"text-fluid-sm": { min: 14, max: 16, ...range },
"text-fluid-base": { min: 16, max: 18, ...range },
"text-fluid-lg": { min: 18, max: 24, ...range },
"text-fluid-2xl": { min: 22, max: 32, ...range },
"text-fluid-4xl": { min: 28, max: 52, ...range },
"spacing-fluid-sm": { min: 12, max: 24, ...range },
"spacing-fluid-lg": { min: 20, max: 64, ...range },
};
Show code · src/app/tailwind/fluid/FluidPreview.tsx
"use client";
import { useRef, useState } from "react";
import { Resizer } from "@/components/Resizer";
import { fluidTokens } from "@/lib/fluid";
// A short article set entirely in fluid tokens. Each time the box changes
// width, read back what the browser computed for each token.
const samples = [
{ token: "text-fluid-4xl", read: (s: CSSStyleDeclaration) => s.fontSize },
{ token: "text-fluid-lg", read: (s: CSSStyleDeclaration) => s.fontSize },
{ token: "text-fluid-base", read: (s: CSSStyleDeclaration) => s.fontSize },
{ token: "text-fluid-sm", read: (s: CSSStyleDeclaration) => s.fontSize },
{ token: "spacing-fluid-lg", read: (s: CSSStyleDeclaration) => s.paddingTop },
{ token: "spacing-fluid-sm", read: (s: CSSStyleDeclaration) => s.rowGap },
];
export function FluidPreview() {
const root = useRef<HTMLDivElement>(null);
const [now, setNow] = useState<Record<string, string>>({});
const measure = () => {
const next: Record<string, string> = {};
for (const { token, read } of samples) {
const el = root.current?.querySelector(`[data-fluid="${token}"]`);
if (el) next[token] = `${Number.parseFloat(read(getComputedStyle(el))).toFixed(1)}px`;
}
setNow(next);
};
return (
<div className="grid grid-cols-[minmax(0,1fr)] gap-4">
<Resizer onResize={measure} min={16} max={60} testId="fluid-box">
{/* A wrapper, because querySelector only searches below the element it's called on. */}
<div ref={root}>
<div data-fluid="spacing-fluid-lg" className="rounded-xl border border-line bg-bg p-fluid-lg">
<div data-fluid="spacing-fluid-sm" className="grid gap-fluid-sm">
<h3 data-fluid="text-fluid-4xl" className="m-0 text-fluid-4xl font-bold">Grows with its box</h3>
<p data-fluid="text-fluid-lg" className="m-0 text-fluid-lg text-muted">No breakpoints, no jumps: every size follows the width in a straight line.</p>
<p data-fluid="text-fluid-base" className="m-0 text-fluid-base">
Body text grows a little, headings a lot. That keeps the hierarchy clear on a phone without the headline
swallowing the screen, and lets it breathe on a wide column.
</p>
<p data-fluid="text-fluid-sm" className="m-0 text-fluid-sm text-muted">A caption, in text-fluid-sm.</p>
</div>
</div>
</div>
</Resizer>
<div className="overflow-x-auto">
<table className="w-full border-collapse text-sm">
<thead>
<tr className="text-left text-xs text-muted">
<th className="py-1 pr-3 font-medium">Token</th>
<th className="py-1 pr-3 text-right font-medium">Now</th>
<th className="py-1 text-right font-medium">Range</th>
</tr>
</thead>
<tbody>
{samples.map(({ token }) => (
<tr key={token} className="border-t border-line" data-testid={`fluid-${token}`}>
<td className="py-1 pr-3 font-mono text-xs">{token}</td>
<td className="py-1 pr-3 text-right font-mono text-xs tabular-nums">{now[token] ?? "…"}</td>
<td className="py-1 text-right font-mono text-xs whitespace-nowrap text-muted">
{fluidTokens[token].min} → {fluidTokens[token].max}px
</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
}
Show code · node_modules/@djmiskey/lab-tokens/tailwind.css
/* The shared tokens for a Tailwind v4 lab. Import it after Tailwind:
@import "tailwindcss";
@import "@djmiskey/lab-tokens/tailwind.css";
then set the lab's accent (see tokens.css). */
@import "./tokens.css";
/* The theme switch's classes live in this package; tell Tailwind to scan it. */
@source "./ThemeSwitch.tsx";
/* dark: follows the theme switch, with the same no-JavaScript fallback.
@slot is where the utility's styles go. */
@custom-variant dark {
&:where([data-theme="dark"], [data-theme="dark"] *) {
@slot;
}
@media (prefers-color-scheme: dark) {
&:where(:root:not([data-theme]), :root:not([data-theme]) *) {
@slot;
}
}
}
/* Components use the tokens through these colours (bg-surface, text-muted,
border-line…), so dark mode needs no dark: variants. */
@theme inline {
--color-bg: var(--bg);
--color-surface: var(--surface);
--color-line: var(--line);
--color-fg: var(--fg);
--color-muted: var(--muted);
--color-accent: var(--accent);
--color-accent-ink: var(--accent-ink);
--color-code: var(--code);
--color-danger: var(--danger);
--font-sans: system-ui, -apple-system, "Segoe UI", sans-serif;
--font-mono: ui-monospace, SFMono-Regular, Menlo, monospace;
}
/* Fluid type and spacing: each grows in a straight line from 320 to 960px
wide, then holds. cqi measures the nearest container, or the viewport when
there isn't one. (Generated by fluidClamp() in the labs hub, djmiskey.com/tailwind/fluid.) */
@theme {
--text-fluid-sm: clamp(0.875rem, 0.8125rem + 0.3125cqi, 1rem);
--text-fluid-base: clamp(1rem, 0.9375rem + 0.3125cqi, 1.125rem);
--text-fluid-lg: clamp(1.125rem, 0.9375rem + 0.9375cqi, 1.5rem);
--text-fluid-lg--line-height: 1.4;
--text-fluid-2xl: clamp(1.375rem, 1.0625rem + 1.5625cqi, 2rem);
--text-fluid-2xl--line-height: 1.25;
--text-fluid-4xl: clamp(1.75rem, 1rem + 3.75cqi, 3.25rem);
--text-fluid-4xl--line-height: 1.1;
--spacing-fluid-sm: clamp(0.75rem, 0.375rem + 1.875cqi, 1.5rem);
--spacing-fluid-lg: clamp(1.25rem, -0.125rem + 6.875cqi, 4rem);
}
/* Element defaults for the explanatory text on every lab page. */
@layer base {
body { @apply bg-bg font-sans text-[15px] leading-relaxed text-fg; }
a { @apply text-accent-ink; }
/* Links inside running text need more than colour to stand out (WCAG 1.4.1). */
article :is(p, li) a { @apply underline underline-offset-2; }
h1 { @apply mb-1.5 text-fluid-2xl font-bold; }
h2 { @apply mb-2.5 text-[1.05rem] font-bold; }
h3 { @apply mt-5 mb-2 text-[0.95rem] font-bold; }
p { @apply my-3 first:mt-0; }
ul, ol { @apply my-0 pl-5; }
ol { @apply list-decimal; }
ul { @apply list-disc; }
code { @apply rounded bg-code px-1.5 py-0.5 font-mono text-[0.9em]; }
input:not([type="checkbox"]):not([type="range"]):not([type="color"]), select {
@apply min-w-0 rounded-md border border-line bg-bg px-2 py-1.5 text-fg focus:outline-2 focus:outline-accent;
}
input[type="range"], input[type="checkbox"] { @apply accent-accent; }
}