Button Text Never Looks Centered? CSS text-box-trim Fixes Font Metrics

October 12, 2026 · 7 min read · CSS

By Syed · Frontend Engineer & Creator of UITweak

You know the feeling. The button has equal padding top and bottom, flexbox centering is on, devtools shows a perfectly symmetrical box, and yet the text sits slightly low. Or the headline has a mysterious gap above it that no margin explains. You nudge it with a pixel of padding, then two, then a negative margin, and it looks right until someone changes the font. The problem was never your alignment. It was the font's invisible metrics, and now CSS has a property that trims them.

30-second version: text-box-trim cuts the dead space fonts reserve above capitals and below baselines. Pair it with text-box-edge to choose what to trim to: .btn { text-box: trim-both cap alphabetic; } makes text sit optically centered with zero nudge pixels. Supported in Chrome 133+, Safari 18.2+ and Firefox 154+.

Why line-height: 1 never fixed it

A font file reserves vertical space for everything it might ever draw: capitals, ascenders, descenders, accents. That reservation lives inside the line box even when the actual glyphs are small. Setting line-height: 1 only makes the box exactly 1em tall. It does nothing about the gap between the top of a capital letter and the top of that 1em box, which is pure font-metric air.

This is why two different fonts at the same font-size and line-height produce visibly different spacing. The metrics differ per typeface, so any pixel nudge you add is married to one font. Switch from Inter to a brand typeface and your "fix" silently becomes wrong.

The recipe: trim to what you mean

Two properties work together. text-box-trim picks which edges get trimmed, text-box-edge picks what they get trimmed to:

.btn {
  /* trim top of first line and bottom of last line */
  text-box-trim: trim-both;
  /* top: cut down to cap-height, bottom: cut to the baseline */
  text-box-edge: cap alphabetic;
}

That combination is the money setting for UI text. The top edge hugs the capitals, the bottom edge sits on the baseline, and the text finally looks centered inside equal padding. No nudge pixels, no font-specific hacks.

The trim keywords are trim-start (top of first line), trim-end (bottom of last line) and trim-both. The edge keywords include cap (capitals), ex (x-height, good for lowercase-heavy text), alphabetic (baseline) and text (the default text edge).

Or use the one-line shorthand

The text-box shorthand sets both in a single declaration:

.btn {
  text-box: trim-both cap alphabetic;
}

.headline {
  text-box: trim-both cap alphabetic;
}

Three places it pays off immediately

1. Buttons and badges

The classic symptom: a pill badge or button where the label floats a pixel or two low. With text-box: trim-both cap alphabetic, equal padding becomes truly equal visually. This is the fix for every "my button text looks off-center" thread.

2. Headline gaps (the negative-margin killer)

Big display headlines carry a pocket of air above the capitals that designers never drew in Figma. The old fix was a negative top margin tuned by eye, and it broke every time the typeface or size changed. Trimming to cap-height gives you the tight box the designer actually meant, computed from the font's own metrics:

.hero-title {
  font-size: 4rem;
  line-height: 1.05;
  text-box: trim-both cap alphabetic; /* no more magic negative margin */
}

3. Icon plus text rows

A 16px icon next to a label often looks misaligned even in a centered flex row, because the text's line box is taller than its glyphs. Trim the text and the icon and the visible letters line up for real instead of approximately.

The fallback: degrade to normal spacing

Unsupported browsers simply ignore the properties and render normal line-box spacing, which is exactly your current behavior. So the safe pattern is to write your normal CSS first and layer the trim on top:

.btn {
  padding: 0.75rem 1.5rem; /* looks fine everywhere */
}

@supports (text-box: trim-both cap alphabetic) {
  .btn {
    text-box: trim-both cap alphabetic; /* optically tight where supported */
  }
}

Design the button first, then trim it

Trimming fixes alignment, but you still need a good button underneath it: padding scale, radius, hover states, focus rings. uitweak's Tailwind button studio generates the full button (variants, sizes, states) visually. Copy the output, then add the one text-box line so the label sits perfectly centered in whatever padding you chose.

Browser support

text-box-trim, text-box-edge and the text-box shorthand are now in all three engines: Chrome 133+, Safari 18.2+, and Firefox 154 (shipped August 18, 2026), which completed the set.

  • Chrome / Edge: 133+
  • Safari: 18.2+
  • Firefox: 154+

Because unsupported browsers fall back to ordinary line-box spacing, this is a safe progressive enhancement: use the @supports pattern above and nobody gets a broken layout.

Inline Elements Ignore It: Put It on the Block

text-box-trim applies to block containers. Put it on an inline span and nothing happens: no error, no effect, just the same spacing as before. This is the first thing to check when the trim "does not work." Buttons and headings are fine; their content forms a block container inside. The practical rule: apply the trim to the element that establishes the block (the button, the heading, the badge), never to a span nested inside it. If your label sits in a span inside the button, the trim goes on the button. DevTools will not warn you about this one. The property is valid, the selector matches, and the used value simply has no box to act on.

CJK Text Needs Different Edges

cap and alphabetic are Latin-metric concepts. For CJK text, trimming to cap-height gives wrong results, because ideographic glyphs fill the em box on different metrics than Latin capitals do. The edge keywords include ideographic options designed for exactly this. If your UI mixes scripts, do not ship one trim value globally. Test with real CJK strings and pick the edge per script, the same way you would pick a font stack per script. Optical alignment is per typeface and per script. The property just makes it cheap to express once you know which edge you mean.

Rollout in a Design System: Nudges First

The trim only pays off after the old hacks are gone. Before rolling out a shared .trim utility, grep for the nudge pixels: negative margins on headings, asymmetric padding on buttons, translateY(1px) on labels. Those were compensating for the same font metrics the trim now handles, and keeping them double-corrects the alignment. Convert one component family at a time: buttons, then badges, then headings. Screenshot each, delete the nudges as you go. The utility itself is one line. The audit is the actual work, and skipping it is how you ship text that looks worse than before.

/* one utility, applied after the nudges are deleted */
.trim {
  text-box: trim-both cap alphabetic;
}

Verify With Guides, Not Eyeballing

"Looks centered" is not a test. Screenshot the button, draw horizontal guides at the cap top and the baseline in any image editor, and measure guide-to-padding on both sides. Do it for three typefaces: your body font, your display font, and the system fallback stack. Metrics differ per font, and the fallback is what a large share of users see while webfonts load. When all three measure equal, the trim is doing its job. When they do not, the edge keyword is wrong for that typeface, not the technique. I keep these screenshots in the PR. They end arguments faster than any explanation.

FAQ

Does it trim space between lines of a paragraph?

No. It trims the over edge of the first line and the under edge of the last line of the block container. Spacing between lines inside a paragraph is still controlled by line-height.

Will it break my existing pixel nudges?

Possibly, yes. If you already added manual padding offsets to compensate for font metrics, remove them when you add the trim, or you will double-correct. Trim first, then judge the alignment with fresh eyes.

cap or ex for the top edge?

Use cap for headings and buttons with capitalized text. Use ex for lowercase-heavy display text, where trimming to cap-height would leave extra air above the x-height letters.

Share this guide