Web design and digital-business ideas grounded in real tasks.

text-box-trim changes the space around a line of type

A web designer comparing two heading layouts with differently trimmed top and bottom spacing on a monitor.
AI-generated photographic illustration; not documentary photography.

Two headings with the same padding can appear to sit at different heights when their fonts change. Font metrics contribute space around the visible letter shapes. CSS text-box-trim offers a way to trim selected edges of the text box instead of compensating with an arbitrary negative margin.

MDN’s property reference explains the available start, end and both-edge options. Its companion property, text-box-edge, determines the metric edges used. The feature concerns the text’s block-edge space; it is not a replacement for all line-height, padding or margin decisions.

Start with a small heading sample

This illustrative rule requests trimming at both edges using the cap-height and alphabetic-baseline edges:

.card-title {
  text-box-trim: trim-both;
  text-box-edge: cap alphabetic;
}

Keep the surrounding layout sensible when those declarations are unsupported. Browsers that do not understand them will ignore them, so the untrimmed version should remain readable and usable.

Change the text, not only the font size

Test capitals, lowercase letters, accented characters and a heading that wraps to a second line. Include the languages your interface actually supports. A compact English sample containing only capitals does not represent every string that may appear in production.

Also test the fallback font before the preferred web font loads. The layout should tolerate that change without hiding content or making neighbouring elements collide.

Keep layout and glyph painting distinct

Trimming box edges is not the same as clipping the visible letters. Avoid adding overflow clipping merely because the box is tighter, and inspect accents and descenders in the finished design. Check focus indicators if the text is inside an interactive component.

Browser support continues to evolve, so verify the compatibility information against the devices you serve. Use the property as a measured typographic improvement rather than assuming that one declaration removes the need for spacing tests.

A useful comparison screenshot shows the same component with and without trimming, using real content. That makes it easier to decide whether the visual improvement survives beyond the neatest possible demo.