Text Baseline Fix in CSS

Trim the extra space above and below text with CSS text-box.

There are times when you set equal padding on a button, or line up some text next to an icon, and it still looks off. The text sits a little low, or there's more space above it than below it. Your CSS is fine. The extra space is part of the text itself.

The good news is CSS now has a property built for this: text-box.

TL;DR Use text-box: trim-both cap alphabetic to trim text down to its cap height and baseline, with a negative margin fallback for browsers that don't support it yet.

Where does the extra space come from?

Every line of text sits in a line box that is as tall as its line-height. The glyphs don't fill that box. Fonts reserve room above the capital letters for accents and tall ascenders, and room below the baseline for descenders like "g" and "y". On top of that, any extra line-height gets split in half and added above and below the text (this is called half-leading).

So a button with 16px of padding on all sides really has 16px plus some invisible space above the cap height and below the baseline. How much depends on the font, and it's often not the same on top and bottom.

The fix: text-box

The text-box property trims that space off the first and last line of a block:

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

It's a shorthand for two properties:

.label {
text-box-trim: trim-both;
text-box-edge: cap alphabetic;
}
  • text-box-trim picks which side to trim: trim-start, trim-end or trim-both.
  • text-box-edge picks where to trim to. cap trims the top to the height of capital letters, and alphabetic trims the bottom to the baseline. You can use ex instead of cap to trim to the height of lowercase letters.

With the space trimmed, the element's height matches the visible text, so equal padding looks equal.

text-box just shipped in Safari 18.2, and it's coming to Chrome (and other Chromium browsers) in Chrome 133 early next year. Firefox doesn't support it yet.

Aligning text with an icon

Once the text is trimmed, flexbox centering finally lines up with what you see:

<button class="button">
<svg class="icon" aria-hidden="true" viewBox="0 0 24 24">...</svg>
<span class="label">Download</span>
</button>
.button {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 12px 20px;
font: 600 16px/1.5 Inter, sans-serif;
}
.icon {
width: 16px;
height: 16px;
}
.label {
text-box: trim-both cap alphabetic;
}

align-items: center centers the icon and the label against each other. Without the trim, it centers the icon against the full line box, which is why the text usually looks like it's sitting too low. With the trim, it centers against the capital letters.

The trim goes on the span rather than the button. text-box applies to block containers and inline boxes, and the span becomes a block container as a flex item.

Without text-box
With text-box

A fallback for older browsers

Before text-box, the common fix was to pull the text up and down with negative margins on ::before and ::after. This is the approach the Capsize tool by Seek generates. Wrap it in @supports not so it only applies when text-box isn't available:

@supports not (text-box: trim-both cap alphabetic) {
.label::before,
.label::after {
content: "";
display: table;
}
/* Values for Inter at line-height: 1.5 */
.label::before {
margin-bottom: -0.3864em;
}
.label::after {
margin-top: -0.3864em;
}
}

The display: table pseudo elements give the margins something to attach to, and the negative margins pull the edges of the block in to the cap height and baseline.

The numbers come from the font's metrics, which are stored in the font file. Inter has an ascent of 2728, a descent of 680 and a cap height of 2048, out of 2816 units per em. For the top, that's the space between the ascent and the cap height ((2728 - 2048) / 2816, about 0.2415em) plus the half-leading ((1.5 - 1.2102) / 2, about 0.1449em), where 1.2102 is the font's natural line height ((2728 + 680) / 2816). Inter happens to give the same value for the bottom, but most fonts don't.

That means these numbers only work for this font and this line-height. Change either one and you need new values. Rather than doing the math by hand, plug your font into Capsize and copy the CSS it gives you.

Summary

Use text-box: trim-both cap alphabetic to get rid of the space above and below your text, so padding looks even and icons line up. Keep the negative margin trick from Capsize behind @supports not until Chrome and Firefox catch up.

Have some feedback on this post? .