Skip to content
WANCRAFT

C.102Typography & Text

Fit Text

A headline that sizes itself to exactly fill its container's width — per line, with any font, without a first-paint jump.

NewReactCSSTailwindintermediate

Overview

Fit Text makes a headline span its container edge to edge. The server renders a CSS clamp() estimate in container query units, so the first paint is already close; after mount each line is measured once (text width scales linearly with font size) and the exact size is written to a CSS variable. A ResizeObserver keeps it exact as the container changes, document.fonts.ready refits once web fonts arrive, and in multi-line mode every line is fitted on its own for the stacked editorial look. Sizes are clamped between min and max; a line that cannot fit even at the minimum wraps instead of overflowing. Resizes can ease with a short transition, and optional blueprint guides print each line's fitted size.

01Installation

Method 01 · Registry

CLI verified

Uses the shadcn CLI in a project initialised with shadcn init. Installs the file to components/ui/fit-text.tsx and adds its dependencies.

npx shadcn@latest add https://wancraft.my/r/fit-text.json

Method 02 · Copy and paste

  1. 1. No extra dependencies — React and Tailwind CSS only.
  2. 2. Copy the source from the Code tab into components/ui/fit-text.tsx.
  3. 3. Import it and adjust props.
Dependencies
None
Peer
react ≥ 18, tailwindcss ≥ 4
Added
13 Oct 2026
Updated
—

02Usage

The default configuration. Change properties above to generate a tailored example in the Usage tab.

example.tsx
import { FitText } from "@/components/ui/fit-text";

export function FitTextExample() {
  return (
    <FitText className="font-semibold uppercase tracking-[-0.03em]">
      {"Make it\nfit the\nwhole width"}
    </FitText>
  );
}

03Props

PropTypeDefaultDescription
childrenstring—Required. The headline; in multiline mode each "\n" starts a line.
as"h1" | "h2" | "h3" | "p" | "div""h2"Element to render.
minnumber16Smallest font size in px; lines that still overflow wrap.
maxnumber160Largest font size in px.
multilinebooleantrueSplit on "\n" and fit every line independently.
animatebooleantrueEase font-size changes on resize (never on first paint).
durationnumber300Resize transition length in ms.
lineHeightnumber1Unitless line height.
guidesbooleanfalseHairline under each line with its fitted size.
accentstring"#FF7448"Guide colour.
...propsHTMLAttributes<HTMLElement>—Forwarded to the root; set the font with className.

04Features

  • 01Measured-ratio fitting: one measurement per line gives the exact size
  • 02Server render uses a container-query clamp() estimate, so there is no layout jump
  • 03ResizeObserver refits on width changes only; document.fonts.ready refits after web fonts load
  • 04Multi-line mode fits each line independently
  • 05min / max clamping, wrapping instead of overflowing below the minimum
  • 06Optional eased transition on resize and blueprint guides with live sizes
motion · tweeninput · viewport

05Accessibility

  • Renders a real heading (or the element you choose) with plain text, so it is read and copied normally.
  • Lines are separated by spaces in the DOM, so screen readers do not run words together.
  • Guides are aria-hidden decoration.
  • With prefers-reduced-motion the resize transition is disabled.

06Limitations

  • Fitting is horizontal only; very tall containers are not filled vertically.