Typography Beginner

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.

Modern
4 lines
1.btn {
2  padding: 10px 20px;
3  text-box: trim-both cap alphabetic;
4}
Old 7 lines
1.btn {
2  display: inline-flex;
3  align-items: center;
4  padding: 10px 20px;
5  /* still looks off-center vertically */
6  padding-top: 8px; /* manual tweak */
7}
Limited availability 79% global usage

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

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
Kinsta

Your first month is free

Managed WordPress hosting for faster sites.

Learn more
⚑

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.

New CSS drops.

Join 600+ readers who've survived clearfix hacks.

ESC