CSS text-box-trim and text-box-edge for cap-height centering
Centering button text without line-height tweaks
Every line box has space above the cap line and below the baseline: the font's ascent and descent plus half-leading from line-height. That is why button text often looks a pixel off. text-box-trim removes it so text aligns to its actual letterforms.
The old hack vs the modern fix
View demo/* per-font line-height tuning to fake a centered look */
.btn {
line-height: 1.15;
padding: 13px 20px 11px;
}.btn {
text-box-trim: trim-both;
text-box-edge: cap alphabetic;
padding: 12px 20px;
}
/* the button measures from the cap line, not the metric box */<button class="px-5 py-3 rounded-lg [text-box-trim:trim-both] [text-box-edge:cap_alphabetic]">Subscribe</button>Text box browser support
Chrome 133, Edge 133, Safari 18.2 and Firefox 154 ship it. Keep normal padding so older browsers still show the untrimmed text and the button looks fine.
Live demo
Why the modern way wins
Optical centering, finally
Padding now measures from the cap line to the baseline. The button's vertical space matches what your eye actually sees, not the font's metric box.
Pairs with anything
Works on buttons, chips, tags, headings, anywhere a line of text sits inside a fixed shape. Combine with display: grid; place-items: center and the alignment lands first try.
Edges you can choose
text-box-edge: cap alphabetic trims the typical Latin shape. Use ex, ideographic, or text for other scripts and writing modes.
- Key Change
- Trim the extra space
- Cap line replaces metric box
- Old Approach
- line-height + padding
- Per-font tuning
- Modern Approach
- text-box-trim + text-box-edge
- One declaration
How it works
Every font ships with metrics that include space above the cap line and below the baseline, and line-height adds half-leading on top. That space is what makes a 32px-tall button with 16px text look like the text sits a little high or low. Tuning line-height per font, or chasing the right padding values, was the workaround.
text-box-trim: trim-both tells the browser to size the inline box from the trimmed edges instead of the full metric box. text-box-edge: cap alphabetic picks the cap line as the top edge and the alphabetic baseline as the bottom. Padding now applies to those visual edges. The button text centers cleanly without any per-font tuning.
Use trim-start or trim-end when you only want one side trimmed, useful for headings that need to sit flush with a top edge. The text-box shorthand combines both properties when you want them in one line.