CSS text-box-trim Property

The CSS text-box-trim property controls whether extra space is trimmed from the block-start edge, block-end edge, or both edges of a text container.

It works with text-box-edge, which determines the font metrics used as trimming boundaries. Together, these properties provide more precise control over the vertical space around text without changing its font size.

Text box trimming is particularly useful for headings, buttons, labels, and other elements where the extra space included in font metrics can make alignment difficult. Both properties can also be set using the text-box shorthand.

text-box-trim Syntax

The text-box-trim property accepts one of four keywords that determine which outer edges of a text container are trimmed.

Basic Syntax

h2 {
  text-box-trim: trim-both;
}

This declaration trims both the block-start and block-end edges of the text container.

Available Values

h2 {
  text-box-trim: none;
}
h2 {
  text-box-trim: trim-start;
}
h2 {
  text-box-trim: trim-end;
}
h2 {
  text-box-trim: trim-both;
}

CSS-Wide Keywords

h2 {
  text-box-trim: initial;
}
h2 {
  text-box-trim: inherit;
}
h2 {
  text-box-trim: unset;
}
h2 {
  text-box-trim: revert;
}
h2 {
  text-box-trim: revert-layer;
}

The initial value is none, which leaves the normal text box untrimmed.

Combining With text-box-edge

h2 {
  text-box-trim: trim-both;
  text-box-edge: cap alphabetic;
}

The text-box-trim property selects the edges to trim, while text-box-edge defines the font-metric boundaries used for trimming.

Equivalent Shorthand

h2 {
  text-box: trim-both cap alphabetic;
}

The text-box shorthand combines both declarations.

Property Values

The four values determine whether trimming is disabled or applied to one or both outer text-box edges.

Value Description
none Does not trim either edge. This is the initial value.
trim-start Trims the block-start edge of the text container.
trim-end Trims the block-end edge of the text container.
trim-both Trims both the block-start and block-end edges.

Property Characteristics

Characteristic Value
Initial value none
Inherited No
Applies to Block containers and other applicable text-containing boxes defined by CSS Inline Layout
Computed value As specified
Animation type Discrete
Percentages Not accepted
Related properties text-box-edge, text-box, line-height
Specification CSS Inline Layout Module Level 3

Valid Examples

.heading {
  text-box-trim: none;
}
.heading {
  text-box-trim: trim-start;
}
.heading {
  text-box-trim: trim-end;
}
.heading {
  text-box-trim: trim-both;
}

Invalid Examples

.heading {
  text-box-trim: trim-top;
}
.heading {
  text-box-trim: trim-bottom;
}
.heading {
  text-box-trim: trim-all;
}
.heading {
  text-box-trim: 10px;
}
.heading {
  text-box-trim: 50%;
}

The property uses logical trimming keywords rather than physical directions, lengths, or percentages.

How It Works

Fonts include vertical metrics that help determine the height and positioning of text within a line. These measurements can leave space above or below the visible characters.

The text-box-trim property allows the browser to remove selected portions of that space from the outer edges of a text container.

Step 1: Create a Heading

<h2 class="trimmed-heading">CSS Typography</h2>

Step 2: Apply Typography

.trimmed-heading {
  margin: 0;
  font-size: 36px;
  line-height: 1.5;
}

Without trimming, the heading uses its normal text-box dimensions.

Step 3: Select the Font Metrics

.trimmed-heading {
  margin: 0;
  font-size: 36px;
  line-height: 1.5;
  text-box-edge: cap alphabetic;
}

The text-box-edge property defines the cap-height and alphabetic baseline boundaries.

Step 4: Enable Trimming

.trimmed-heading {
  margin: 0;
  font-size: 36px;
  line-height: 1.5;
  text-box-edge: cap alphabetic;
  text-box-trim: trim-both;
}

The browser trims both outer text-box edges using the selected font metrics.

Step 5: Compare the Layout

The heading retains its original font size, but the text container may occupy less vertical space.

Important Consideration

Trimming changes the text container's layout boundaries. It does not resize the glyphs or remove CSS padding and margins.

The none Value

The none value disables text-box trimming. It is the initial value of text-box-trim.

Syntax

h2 {
  text-box-trim: none;
}

Example

.normal-heading {
  margin: 0;
  font-size: 32px;
  line-height: 1.5;
  text-box-trim: none;
}

The heading retains its normal text-box spacing.

With text-box-edge

.normal-heading {
  text-box-edge: cap alphabetic;
  text-box-trim: none;
}

Although the edge metrics are defined, no trimming occurs because text-box-trim is set to none.

When to Use none

Use none to preserve normal text-box spacing or override a trimming declaration applied by another rule.

The trim-start Value

The trim-start value trims only the block-start edge of the text container.

In horizontal writing modes, the block-start edge is normally the top. In vertical writing modes, its physical position depends on the writing direction.

Syntax

h2 {
  text-box-trim: trim-start;
}

Example

.section-heading {
  margin: 0;
  font-size: 32px;
  line-height: 1.4;
  text-box-edge: cap alphabetic;
  text-box-trim: trim-start;
}

The browser trims the block-start edge using the cap-height metric. The block-end edge remains untrimmed.

Using the Shorthand

.section-heading {
  text-box: trim-start cap alphabetic;
}

When to Use trim-start

Use this value when you want to reduce the extra space before the first line of text while preserving the spacing after the final line.

Important Consideration

Trimming the block-start edge does not remove the element's margin or padding on that side.

The trim-end Value

The trim-end value trims only the block-end edge of the text container.

In horizontal writing modes, the block-end edge is normally the bottom. In vertical writing modes, it follows the logical block direction.

Syntax

h2 {
  text-box-trim: trim-end;
}

Example

.card-heading {
  margin: 0;
  font-size: 28px;
  line-height: 1.4;
  text-box-edge: cap alphabetic;
  text-box-trim: trim-end;
}

The browser trims the block-end edge using the alphabetic baseline boundary.

Using the Shorthand

.card-heading {
  text-box: trim-end cap alphabetic;
}

When to Use trim-end

Use this value when you want to reduce the extra space after the final line of text while preserving the spacing before the first line.

Important Consideration

Characters with descenders may extend below the alphabetic baseline. Test the result when clipping or overflow restrictions are present.

The trim-both Value

The trim-both value trims both the block-start and block-end edges of the text container.

It is useful when the goal is to reduce extra space around the entire text block.

Syntax

h2 {
  text-box-trim: trim-both;
}

Example

.page-title {
  margin: 0;
  font-size: 40px;
  line-height: 1.4;
  text-box-edge: cap alphabetic;
  text-box-trim: trim-both;
}

The browser trims the block-start edge using cap height and the block-end edge using the alphabetic baseline.

Using the Shorthand

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

Comparing All Four Values

Value Block-Start Edge Block-End Edge
none Not trimmed Not trimmed
trim-start Trimmed Not trimmed
trim-end Not trimmed Trimmed
trim-both Trimmed Trimmed

When to Use trim-both

Use trim-both for headings, labels, and other compact text elements where both outer edges should follow selected font metrics more closely.

Relationship With text-box-edge

The text-box-trim property determines which edges are trimmed, while text-box-edge determines where those edges are positioned.

Property Purpose
text-box-trim Controls whether the block-start edge, block-end edge, or both edges are trimmed.
text-box-edge Defines the font metrics used as trimming boundaries.
text-box Combines both properties in one shorthand declaration.

Using Cap Height

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

Uses the cap-height and alphabetic baseline metrics for trimming.

Using X-Height

.heading {
  text-box-edge: ex alphabetic;
  text-box-trim: trim-both;
}

Uses the font's x-height metric for the over edge.

Using Text Metrics

.heading {
  text-box-edge: text;
  text-box-trim: trim-both;
}

Uses the font's text-over and text-under metrics.

Trimming Without Explicit Edge Metrics

.heading {
  text-box-trim: trim-both;
}

When no explicit text-box-edge value is provided, the browser uses the applicable automatic edge metrics.

Important Consideration

Font metrics vary between fonts. The same trimming values can produce different amounts of space reduction.

Using the text-box Shorthand

The text-box shorthand combines text-box-trim and text-box-edge into one declaration.

Longhand Version

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

Equivalent Shorthand

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

Both versions produce the same trimming configuration.

Trim the Start Edge

.heading {
  text-box: trim-start cap alphabetic;
}

Trim the End Edge

.heading {
  text-box: trim-end cap alphabetic;
}

Trim Both Edges

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

Restore Normal Spacing

.heading {
  text-box: normal;
}

The normal value restores the initial shorthand behavior.

Shorthand Reset Behavior

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

The shorthand resets the omitted text-box-edge component to its initial value. Include the desired edge metrics in the shorthand when both properties must be set together.

Effects on Layout

Text-box trimming changes the block-axis space associated with the outer edges of a text container. This can affect element dimensions and the position of surrounding content.

Reducing Heading Height

.page-heading {
  margin: 0;
  font-size: 40px;
  line-height: 1.5;
  text-box-edge: cap alphabetic;
  text-box-trim: trim-both;
}

The heading can occupy less vertical space without changing the size of its characters.

Preserving Padding

.button {
  padding: 12px 20px;
  text-box-edge: cap alphabetic;
  text-box-trim: trim-both;
}

Padding remains part of the element's box model. Trimming does not remove it.

Working With Line Height

.heading {
  font-size: 32px;
  line-height: 1.5;
  text-box-edge: cap alphabetic;
  text-box-trim: trim-both;
}

The line-height property continues to influence the spacing and positioning of text. Trimming affects the selected outer text-box edges.

Multiline Text

For multiline text, trimming affects the outer block-start and block-end edges. It does not independently remove spacing between every internal line.

Writing Modes

The property uses logical block-axis directions. In vertical writing modes, block-start and block-end may correspond to the left and right sides rather than the top and bottom.

Responsive Layouts

Font size, line wrapping, and font fallback can influence the final dimensions of trimmed text. Test layouts at different viewport sizes.

Practical Examples

Trim a Page Heading

.page-title {
  margin: 0;
  font-size: 40px;
  line-height: 1.3;
  text-box-edge: cap alphabetic;
  text-box-trim: trim-both;
}

Reduces extra space at both outer edges of a large heading.

Trim Only the Start Edge

.section-title {
  text-box-edge: cap alphabetic;
  text-box-trim: trim-start;
}

Trims the block-start edge while leaving the block-end edge unchanged.

Trim Only the End Edge

.section-title {
  text-box-edge: cap alphabetic;
  text-box-trim: trim-end;
}

Trims the block-end edge while leaving the block-start edge unchanged.

Button Label

.action-button {
  padding: 12px 20px;
  font-size: 16px;
  line-height: 1.4;
  text-box-edge: cap alphabetic;
  text-box-trim: trim-both;
}

Helps control the space around the text without removing the button's padding.

Compact Label

.compact-label {
  font-size: 14px;
  line-height: 1.4;
  text-box-edge: ex alphabetic;
  text-box-trim: trim-both;
}

Uses x-height and alphabetic baseline metrics for a compact label.

Card Heading

.card-heading {
  margin: 0 0 16px;
  text-box-edge: cap alphabetic;
  text-box-trim: trim-both;
}

Trims the text box while preserving the heading's bottom margin.

Responsive Heading

.responsive-heading {
  font-size: 28px;
  text-box-edge: cap alphabetic;
  text-box-trim: trim-both;
}
@media (min-width: 768px) {
  .responsive-heading {
    font-size: 40px;
  }
}

Maintains trimming as the heading's font size changes.

Restore Normal Text Spacing

.normal-heading {
  text-box-trim: none;
}

Disables trimming without necessarily changing the selected edge metrics.

Progressive Enhancement

.enhanced-heading {
  margin: 0;
  line-height: 1.2;
}
@supports (text-box-trim: trim-both) and (text-box-edge: cap alphabetic) {
  .enhanced-heading {
    text-box-trim: trim-both;
    text-box-edge: cap alphabetic;
  }
}

Applies trimming only when the browser recognizes both declarations.

text-box-trim Example

This example compares all four trimming values. Each sample uses the same font, font size, line height, and edge metrics so the differences can be observed.

HTML Example

<div class="text-trim-demo">
  <div class="text-trim-panel">
    <h4>none</h4>
    <div class="text-trim-sample trim-none">CSS Heading</div>
  </div>
  <div class="text-trim-panel">
    <h4>trim-start</h4>
    <div class="text-trim-sample trim-start">CSS Heading</div>
  </div>
  <div class="text-trim-panel">
    <h4>trim-end</h4>
    <div class="text-trim-sample trim-end">CSS Heading</div>
  </div>
  <div class="text-trim-panel">
    <h4>trim-both</h4>
    <div class="text-trim-sample trim-both">CSS Heading</div>
  </div>
</div>

CSS Example

.text-trim-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
}
.text-trim-demo .text-trim-panel {
  padding: 16px;
  border: 1px solid #cbd5e1;
  background: #f8fafc;
}
.text-trim-demo h4 {
  margin: 0 0 16px;
  font-size: 14px;
}
.text-trim-demo .text-trim-sample {
  display: flow-root;
  background: #dbeafe;
  font-family: Arial, sans-serif;
  font-size: 32px;
  font-weight: bold;
  line-height: 1.5;
  text-box-edge: cap alphabetic;
}
.text-trim-demo .trim-none {
  text-box-trim: none;
}
.text-trim-demo .trim-start {
  text-box-trim: trim-start;
}
.text-trim-demo .trim-end {
  text-box-trim: trim-end;
}
.text-trim-demo .trim-both {
  text-box-trim: trim-both;
}
Browser Result

none

CSS Heading

trim-start

CSS Heading

trim-end

CSS Heading

trim-both

CSS Heading

In supporting browsers, the samples demonstrate how each trimming value affects the outer text-box edges. The amount of space removed depends on the font's metrics.

Comparing the Results

Value Expected Behavior
none Preserves normal text-box spacing.
trim-start Trims only the block-start edge.
trim-end Trims only the block-end edge.
trim-both Trims both outer edges.

Changing the Edge Metrics

.text-trim-demo .text-trim-sample {
  text-box-edge: ex alphabetic;
}

The samples now use the x-height metric for their over edges.

Disabling Trimming

.text-trim-demo .text-trim-sample {
  text-box-trim: none;
}

All samples return to normal text-box spacing.

Testing Browser Support

@supports (text-box-trim: trim-both) and (text-box-edge: cap alphabetic) {
  .text-trim-demo .trim-both {
    text-box-trim: trim-both;
    text-box-edge: cap alphabetic;
  }
}

The feature query checks whether the browser recognizes the trimming and edge declarations.

Accessibility Considerations

Text-box trimming can improve alignment, but it should not interfere with the readability or visibility of text.

Avoid Clipping Characters

Accented characters, descenders, and other glyph features may extend beyond selected font metrics. Test trimmed text when an element or ancestor uses overflow: hidden.

Preserve Readable Line Spacing

Use appropriate line-height values, particularly for multiline text. Trimming should not cause lines to overlap.

Support Text Resizing

Test layouts when users enlarge text or increase browser zoom. Ensure trimmed text does not overlap adjacent content.

Test Different Languages

Writing systems use different font metrics. A trimming configuration suitable for Latin text may not be appropriate for every language.

Maintain Interactive Target Sizes

For buttons and other interactive controls, preserve sufficient padding and minimum dimensions. Text trimming should not reduce usability.

Check Font Fallbacks

Fallback fonts can change the amount of trimming because their metrics may differ from the preferred font.

Use Progressive Enhancement

Ensure the interface remains usable in browsers that do not support text-box trimming.

Common Mistakes

Using Physical Direction Keywords

h2 {
  text-box-trim: trim-top;
}

The property uses logical block-axis keywords such as trim-start and trim-end.

Using Unsupported Values

h2 {
  text-box-trim: trim-all;
}

Use trim-both to trim both outer edges.

Using Lengths or Percentages

h2 {
  text-box-trim: 10px;
}

The property accepts trimming keywords, not numeric distances.

Confusing text-box-trim With text-box-edge

The first property determines which edges are trimmed. The second determines the font metrics used as boundaries.

Expecting Trimming to Remove Padding

Text-box trimming does not remove an element's padding or margins.

Expecting Identical Results Across Fonts

Different fonts use different metrics, so the amount of trimming can vary.

Ignoring Multiline Text

Trimming applies at the selected outer edges of a text container. It does not remove spacing between every internal line.

Ignoring Writing Modes

Block-start and block-end are logical directions. They do not always correspond to the physical top and bottom.

Overlooking Glyph Extents

Some characters may extend beyond the selected trimming boundaries. Avoid clipping important text.

Assuming the Property Is Inherited

The text-box-trim property is not inherited. Descendant elements do not automatically receive the parent's trimming value.

Overwriting Longhand Values With the Shorthand

A later text-box declaration can reset an earlier text-box-trim value.

Ignoring Browser Support

Unsupported browsers ignore the declaration. Provide a usable fallback layout.

Expecting Smooth Animation

The property uses discrete animation behavior. Changes between trimming keywords do not interpolate smoothly.

Important Considerations

Keep the following points in mind when using text-box-trim:

  • It controls trimming at the outer block-start and block-end edges of text containers.
  • It works with text-box-edge.
  • It is a longhand component of the text-box shorthand.
  • The initial value is none.
  • The property is not inherited.
  • The none value disables trimming.
  • The trim-start value trims the block-start edge.
  • The trim-end value trims the block-end edge.
  • The trim-both value trims both outer edges.
  • The property uses logical directions rather than physical top and bottom keywords.
  • It does not accept lengths or percentages.
  • It does not change font size.
  • It does not remove margins or padding.
  • It can change the block-axis dimensions of text containers.
  • It does not replace normal line-height calculations.
  • Multiline text retains normal spacing between internal lines.
  • The selected trimming boundaries depend on text-box-edge.
  • Automatic edge metrics are used when no explicit edge definition is provided.
  • The amount of trimming depends on the font's metrics.
  • Different font families can produce different results.
  • Fallback fonts may change the final layout.
  • Accents and descenders may extend beyond selected font-metric boundaries.
  • Overflow restrictions can clip glyphs.
  • Vertical writing modes change the physical location of block-start and block-end.
  • The text-box shorthand can set trimming and edge metrics together.
  • Shorthand declarations reset omitted longhand values.
  • The property uses discrete animation behavior.
  • Browser support varies by browser and version.
  • Feature queries can help apply trimming as a progressive enhancement.
  • Test responsive layouts, font fallbacks, text resizing, and different writing systems.
  • Preserve appropriate spacing and target sizes for interactive controls.

Browser Support

The CSS text-box-trim property is a newer CSS typography feature with limited availability compared with established properties such as font-size and line-height. Support varies by browser and version.

Browsers that do not recognize the property ignore the declaration and retain normal text-box spacing. For predictable results, check support for both text-box-trim and text-box-edge.

For current compatibility information, visit Can I Use? and search for text-box-trim.

Feature Detection

@supports (text-box-trim: trim-both) {
  .heading {
    text-box-trim: trim-both;
  }
}

This checks whether the browser recognizes the trimming declaration.

Checking Both Properties

@supports (text-box-trim: trim-both) and (text-box-edge: cap alphabetic) {
  .heading {
    text-box-trim: trim-both;
    text-box-edge: cap alphabetic;
  }
}

Checking both properties is useful when the intended result requires specific font-metric boundaries.

Fallback Behavior

.heading {
  margin: 0;
  font-size: 32px;
  line-height: 1.2;
}
@supports (text-box-trim: trim-both) and (text-box-edge: cap alphabetic) {
  .heading {
    text-box-trim: trim-both;
    text-box-edge: cap alphabetic;
  }
}

The heading remains readable without trimming, while supporting browsers can apply the additional adjustment.

Testing Recommendations

Test the property with different fonts, multiline headings, narrow containers, text resizing, and writing modes. Check for clipping and unexpected changes in element dimensions.

Specifications

The text-box-trim property is defined in the CSS Inline Layout Module Level 3 specification.

CSS Inline Layout Module Level 3 - text-box-trim Property

The specification defines the supported trimming keywords, their effects on text-container edges, and their relationship with font-metric boundaries.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: text-box-trim