Free Developer Tool
Enter your text and a box size to find the largest font size that fits — both width and height. Pretext.js binary-searches the answer with zero DOM reflow.
Pretext binary-searches the largest font size whose wrapped height and widest line both fit the box — each candidate measured with prepare() + layout(), no DOM thrashing.
Headlines, hero banners, data-viz labels, name badges, and certificates all share a problem: the text is dynamic but the box is fixed. You want the largest size that still fits, and you want it without the flicker of resize-measure-resize loops. This tool computes that size directly.
It wraps the text to the container width at each candidate size, checks the resulting height against the container height, and binary-searches to the largest size that fits — then previews the text at that exact size inside a box of the dimensions you gave.
// Binary-search the largest size that fits
let lo = 6, hi = 200;
while (hi - lo > 1) {
const mid = (lo + hi) >> 1;
const prepared = prepare(text, mid + 'px Arial');
const { height } = layout(prepared, boxWidth, mid * 1.2);
if (height <= boxHeight) lo = mid; else hi = mid;
}
// lo → largest fitting font sizeEach candidate costs one prepare() + layout(), and the whole search is over in well under a millisecond — no element is ever inserted or measured in the DOM.
To fit text to container you need the largest font size at which the text still fits both the width and the height of the box. The difficulty is that you cannot fit text in one calculation: font size affects wrapping, wrapping affects height, and height determines whether the text fits. So to fit text to container properly you have to test candidate sizes and measure the result of each — which is exactly what this fit-text-to-container tool does.
Many sites try to fit text with CSS clamp() or viewport units, but those scale the font with the viewport, not with the content, so long strings still overflow and short ones leave gaps. To truly fit text to container you must measure the specific text against the specific box, and that requires real glyph measurement rather than a CSS approximation.
The reliable way to fit text to container is a binary search over font size. Pick a minimum and maximum size, test the middle, and keep the half where the text still fits. Each test wraps the text to the container width and checks the resulting height against the container height. In a handful of steps the search converges on the largest size that fits — and because every measurement is pure arithmetic, you can fit the text in well under a millisecond.
Older “fit text” plugins resized the element, read the DOM, and resized again — repeated forced reflows that make it expensive to fit text across many elements. By measuring with Pretext.js instead, this tool can fit text to container without ever inserting an element or triggering layout, so fitting text stays cheap even on a page full of boxes.
The fit-text-to-container tool above also previews the result: it renders your text at the computed size inside a box of the exact dimensions you entered, so you can confirm the fit visually before copying the size into your own code.
Fit text to a banner so a dynamic headline always fills the space without overflowing.
Fit names of any length into a fixed printable area.
Fit text inside chart segments, treemap tiles, and map regions.
Fit text to uniform tiles so a grid stays visually balanced.
In every one of these cases the goal is the same: fit text to a container so it is as large as possible while still fitting. Use the tool above whenever you need to fit text by hand, then apply the resulting font size in CSS or wire the same Pretext.js measurement into your app to fit text automatically at runtime.
Search for the largest font size at which the text, wrapped to the container width, has a total height that fits the container height — and whose widest line still fits the width. A binary search between a min and max size converges in a handful of measurements. This tool does that with Pretext.js and previews the result.
clamp() and vw units scale font size with the viewport, not with the actual text content, so long strings still overflow and short ones leave gaps. Fitting requires measuring the specific text against the specific box, which is what this calculator does.
Classic fit-text plugins resize, read the DOM, and resize again — repeated forced reflows that cause jank, especially with many elements. Pretext measures glyph widths once and tests candidate sizes with pure arithmetic, so finding the fit never triggers layout thrashing.
Yes. The text wraps to the container width at each candidate size, and the fit check compares the wrapped height to the container height. Adjust the line-height ratio to control spacing.
Yes — free, no sign-in, and fully client-side. Your text stays in your browser.
To fit text to container you need real measurement, not a CSS approximation. This tool does it in one step: enter the text and the box size, and it finds the largest size that fits — the same method for headlines, badges, or chart labels.
Because it measures with Pretext.js, you can fit text to container without any DOM reflow, so it stays fast even across a whole page of boxes. Bookmark it to size text by hand, and use the same engine to do it automatically at runtime. Measurement is the only reliable way to fit text to container.
Pixel width of any string at any font.
Line count & height at a given width.
Copy-paste truncation CSS + live preview.
All four tools are powered by Pretext.js, the open-source JavaScript text-measurement engine. Try the interactive playground to explore the full API.