Vertically Center Text in CSS with text-box-trim

Vertical text centering without padding hacks

Text always looked optically off-center because font metrics include extra space for ascenders and descenders. The text-box property trims that invisible space for true visual centering.

Old way 7 lines
.btn   {  display: inline-flex;  align-items: center;  padding: 10px 20px;  padding-top: 8px;}
Modern
4 lines
.btn   {  padding: 10px 20px;  text-box: trim-both cap alphabetic;}
Limited availability 79% global usage

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Not ready for production without a fallback.

132+
17.4+
132+
compare default vs trimmed text boxes
DEFAULT
Heading
Extra space above & below
TRIMMED
Heading
Text box trimmed to cap height
text-box: trim-both cap alphabetic

True centering

Trims invisible font metric space above cap height and below alphabetic baseline. Math and optics finally agree.

Works everywhere

Buttons, badges, tags, headings, pills. Anywhere text looked subtly off-center now looks perfect.

Font-agnostic

The browser reads the font's actual metrics. Switching fonts doesn't break the centering.

Lines Saved
7 → 4
No manual tweaks
Old Approach
Padding hacks
Uneven top/bottom padding
Modern Approach
text-box trim
trim-both cap alphabetic

How it works

Every font has internal metrics: ascent (space above for accents) and descent (space below for descenders like g and y). When you center text in a button or container, the browser centers the full content box including these invisible metrics. The visual center of the actual letters ends up slightly too low.

The text-box shorthand combines text-box-trim and text-box-edge. Using trim-both trims above and below, while cap alphabetic sets the trim edges to the cap height (top of capital letters) and alphabetic baseline. The result: the visible text is truly centered within its container, regardless of font or size.

ESC