CSS Portal

CSS text-box-trim Property

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The text-box-trim property controls whether and how user agents reduce or ignore the empty, transparent, or inkless portions of glyph bounding boxes when constructing inline boxes and calculating inline-level geometry. By trimming that extra space around glyph outlines, the property influences the visual tightness of text runs - for example it can reduce apparent gaps between adjacent glyphs or between lines when fonts include built-in side bearings, accent overhangs, or extra internal padding. This is a cosmetic/layout control that changes the geometry used for painting and layout without altering the underlying character content.

Trimming affects many aspects of inline layout: how the browser computes line boxes, where underlines and strike-throughs are positioned, and the intersection of adjacent inline boxes. Because those geometry calculations feed into spacing and alignment, the property interacts with vertical rhythm and line spacing; see line-height for the related concept of how line boxes are sized. Trimming also has consequences for how overflow and truncation appear at the inline edges - it changes what the UA considers the visible text box, which can affect clipping and elision behavior similar in purpose to text-overflow. Finally, the way words wrap and white-space collapse are presented can change when the trimmed glyph metrics shift the breaking opportunities; compare this when tuning wrapping behavior with white-space.

In painting and hit-testing, trimming moves where backgrounds, borders, and decorations are applied because those paints often follow the inline box extents. That means selection highlights, caret placement, and pointer hit areas can visually shift when trimming is applied, so it’s important to verify interactive elements behave as expected. Trimming also interacts with spacing you might intentionally add around text via layout properties - for example, internal visual tightening produced by trimming should be considered alongside any extra space introduced with padding on inline wrappers or containers.

When deciding whether to use trimming, consider typography goals and cross-font consistency: it’s most useful when you want optical tightness (e.g., for compact UI labels or tight headline typography) or to correct large built-in font bearings that create unwanted visual gaps. Because trimming depends on font metrics and rendering details, test with the specific fonts, sizes, and user-agent environments you target - fonts with heavy diacritics, variable fonts, or fallbacks can change how much trimming matters. Finally, treat trimming as a visual refinement tool rather than a substitute for proper font choice or robust layout practices; it’s best used for controlled situations where you need the extra optical adjustment.

Definition

Initial value
none
Applies to
Block containers and inline boxes
Inherited
no
Computed value
the specified keyword
Animatable
yes
JavaScript syntax
object.style.textBoxtrim

Syntax

text-box-trim: auto | none | both | start | end;

Values

  • auto The default behavior. The browser automatically decides how to trim the text box based on the font metrics and layout context.
  • none No trimming occurs. The box dimensions include the full text glyphs, including overhanging parts such as diacritics or accents.
  • both The text box is trimmed on both the start and end sides, ensuring that the box strictly bounds the visible glyphs.
  • start Only the starting side of the text box is trimmed.
  • endOnly the ending side of the text box is trimmed.

Example

<div class="container">
<div class="box standard">
<h2>Standard Heading</h2>
<p>This box has standard leading. Notice the gap between the top of the letters and the border.</p>
</div>

<div class="box trimmed">
<h2>Trimmed Heading</h2>
<p>This box uses text-box-trim. The space above the capital letters is removed for tighter alignment.</p>
</div>
</div>
/* Container styling for side-by-side comparison */
.container {
  display: flex;
  gap: 20px;
  font-family: sans-serif;
}

.box {
  border: 2px solid #3498db;
  padding: 0; /* Padding is 0 to highlight the trim effect */
  width: 300px;
}

/* The Standard Version */
.standard h2 {
  margin: 0;
  background-color: #f0f0f0;
}

/* The Trimmed Version */
.trimmed h2 {
  margin: 0;
  background-color: #f0f0f0;
  
  /* Trims the whitespace from the top and bottom */
  text-box-trim: both;
  
  /* Specifies that we want to trim to the top of capital letters 
     and the bottom of the alphabetic baseline */
  text-box-edge: cap alphabetic;
}

p {
  margin: 10px 0;
  line-height: 1.5;
}

Browser Support

The following information will show you the current browser support for the CSS text-box-trim property. Hover over a browser icon to see the version that first introduced support for this CSS property.

This property is supported in some modern browsers, but not all.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!