brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: text-box: trim-both cap alphabetic in CSS removes the empty space above and below text

Css
~/tips/tip-css-text-box-trim-cap-baseline

text-box: trim-both cap alphabetic;

Buttons and headings get optically even padding because the font's built-in spacing is cut off.

Every font carries some empty space above the capitals and below the baseline. That is why a button with equal padding never looks centered, and text-box is, in my view, the long missing fix.

.button,
h1 {
  text-box: trim-both cap alphabetic;
}

It is a shorthand for two properties. text-box-trim: trim-both says to trim at the top of the first line and the bottom of the last line. text-box-edge: cap alphabetic says where to cut: at the cap height of the font at the top and at the alphabetic baseline at the bottom. After that, padding: 0.75rem 1.25rem really means 0.75rem of space between the edge and the letters.

For headings it also helps with spacing to the element above, because the gap you set is the gap you see. If your text has many descenders like g and p, alphabetic lets them hang into the padding, so try text as the bottom edge if that looks cramped.

Support is fairly new: Chrome 133, Safari 18.2 and Firefox 154. Older browsers ignore the line and keep the normal line box, so nothing breaks. It is a nice progressive enhancement, but check the result in one older browser too, since the spacing there will be a bit larger.

Further reading: MDN: text-box, MDN: text-box-edge

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link