CSS text-box Property

The CSS text-box property is a shorthand that controls whether extra space is trimmed from the block-start and block-end edges of a text container.

It combines the text-box-trim and text-box-edge properties. Together, they allow text to align more closely with its font-defined character boundaries instead of relying entirely on the space included in the font's line box.

This property is useful for headings, buttons, labels, and other interface elements where precise vertical spacing is important. It does not remove padding or margins and does not change the text's font size.

text-box Syntax

The text-box shorthand accepts a trimming value, an edge definition, or both. The order of the two components is flexible.

Basic Syntax

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

This declaration trims the block-start and block-end edges of the heading's text box. It uses the font's cap-height metric for the over edge and its alphabetic baseline for the under edge.

Common Values

h2 {
  text-box: normal;
}
h2 {
  text-box: trim-both;
}
h2 {
  text-box: trim-start;
}
h2 {
  text-box: trim-end;
}
h2 {
  text-box: trim-both cap alphabetic;
}
h2 {
  text-box: trim-both ex alphabetic;
}
h2 {
  text-box: trim-both text;
}

CSS-Wide Keywords

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

The initial shorthand behavior is equivalent to text-box-trim: none and text-box-edge: auto.

Shorthand Expansion

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

Equivalent longhand declarations:

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

As with other CSS shorthands, omitted components are reset to their initial values rather than retaining earlier longhand declarations.

Property Values

The shorthand combines trimming keywords with text-edge metrics.

Value Description
normal Uses the normal text box without trimming. Equivalent to the initial longhand values.
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.
none A text-box-trim value that disables trimming.
auto A text-box-edge value that uses the automatic edge metrics.
text Uses font text-over and text-under metrics for the corresponding edges.
cap Uses the font's capital-letter height metric for the over edge.
ex Uses the font's x-height metric for the over edge.
alphabetic Uses the alphabetic baseline for the under edge.
ideographic Uses the ideographic under-edge metric.
text-box-trim + text-box-edge Combines a trimming mode with one or two edge metrics.

Property Characteristics

Characteristic Value
Property type Shorthand
Initial value normal
Longhands text-box-trim, text-box-edge
Inherited No, as a shorthand; its longhands have their own inheritance rules
Applies to As defined by the individual longhand properties
Computed value Determined by the longhand properties
Animation type Determined by the longhand properties
Percentages Not accepted
Specification CSS Inline Layout Module Level 3

Valid Examples

.heading {
  text-box: normal;
}
.heading {
  text-box: trim-both;
}
.heading {
  text-box: trim-both cap alphabetic;
}
.heading {
  text-box: trim-start cap;
}
.heading {
  text-box: trim-end text;
}
.heading {
  text-box: trim-both ex alphabetic;
}

Invalid Examples

.heading {
  text-box: center;
}
.heading {
  text-box: 10px;
}
.heading {
  text-box: 50%;
}
.heading {
  text-box: trim-all;
}
.heading {
  text-box: trim-both middle;
}

The shorthand does not accept alignment keywords, arbitrary lengths, percentages, or unsupported trimming values.

How It Works

Fonts include vertical metrics that influence the space allocated to lines of text. These metrics can leave visible space above capital letters or below the baseline even when an element has no padding or margin.

The text-box shorthand allows the browser to trim selected block-axis edges of the text container using specified font metrics.

Step 1: Create a Heading

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

Step 2: Apply Basic Styling

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

The heading uses its normal text box, which includes space determined by the font metrics and line-height calculation.

Step 3: Trim Both Edges

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

The browser trims the heading's block-start edge using the cap-height metric and its block-end edge using the alphabetic baseline.

Step 4: Compare the Result

The text itself retains its font size, but the trimmed text container can occupy less vertical space. The exact amount depends on the font and its metrics.

Important Consideration

Text box trimming does not change the glyph outlines. It changes the layout space around the text, and some glyphs may extend beyond the selected trimming edges.

The text-box-trim Component

The text-box-trim longhand determines which block-axis edges are trimmed.

No Trimming

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

Preserves the normal text box without trimming either edge.

Trim the Start Edge

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

Trims the block-start edge, which is the top edge in horizontal writing modes.

Trim the End Edge

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

Trims the block-end edge, which is the bottom edge in horizontal writing modes.

Trim Both Edges

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

Trims both the block-start and block-end edges using the applicable text-edge metrics.

Comparison

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

The text-box shorthand can set these trimming values together with the edge metrics.

The text-box-edge Component

The text-box-edge longhand determines which font-defined metrics are used as trimming boundaries.

Automatic Edges

.heading {
  text-box-edge: auto;
}

Uses the automatic edge metrics defined by the specification.

Text Edges

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

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

Cap Height and Alphabetic Baseline

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

Uses the capital-letter height for the over edge and the alphabetic baseline for the under edge.

X-Height and Alphabetic Baseline

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

Uses the font's x-height for the over edge and the alphabetic baseline for the under edge.

Using the Shorthand

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

This declaration sets both the trimming mode and the selected text-edge metrics.

Important Consideration

Different fonts have different metrics. The same text-box-edge value can therefore produce different amounts of trimming across font families.

Understanding Font Metrics

Font metrics describe measurements used to position text within a line. The text-box property relies on these measurements to determine where trimming occurs.

Cap Height

Cap height is the font-defined height of capital letters above the baseline. The cap value uses this metric for the over edge.

X-Height

X-height describes the approximate height of lowercase letters such as x. The ex value uses the font's x-height metric.

Alphabetic Baseline

The alphabetic baseline is the reference line on which many Latin letters rest. The alphabetic value uses this baseline for the under edge.

Text-Over and Text-Under Metrics

The text value uses the font's text-over and text-under measurements, which account for the font's intended text extents.

Font Metrics vs. Visible Glyphs

Font metrics do not necessarily match the exact outline of every character. Accents, descenders, punctuation, and other glyph features can extend beyond a selected metric.

Why Font Choice Matters

.heading-one {
  font-family: Arial, sans-serif;
  text-box: trim-both cap alphabetic;
}
.heading-two {
  font-family: Georgia, serif;
  text-box: trim-both cap alphabetic;
}

Both headings use the same trimming values, but their results may differ because the fonts have different metrics.

Important Consideration

Text box trimming uses font metrics rather than measuring the visible pixels of each rendered character.

One-Value and Two-Value Syntax

The shorthand can specify only a trimming mode, only an edge definition, or both components together.

Trimming Mode Only

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

Sets text-box-trim to trim-both and resets text-box-edge to auto.

Edge Definition Only

.heading {
  text-box: cap alphabetic;
}

Sets the edge metrics while leaving trimming disabled through the initial text-box-trim value.

Trimming and Edge Definition

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

Sets both the trimming mode and the edge metrics in one declaration.

Reversed Component Order

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

The shorthand allows the trimming and edge components in either order.

Resetting the Shorthand

.heading {
  text-box: normal;
}

Restores the normal text box behavior.

Important Consideration

Setting an edge metric alone does not activate trimming. A trimming value such as trim-both must also apply for the text box to be trimmed.

Effects on Layout

The text-box property affects the block-axis space associated with text at the selected edges. This can change the overall dimensions and positioning of elements in a layout.

Reducing Extra Space Around Headings

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

The heading can occupy less vertical space because the selected text-box edges are trimmed.

Combining With Padding

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

Text box trimming does not remove padding. The padding remains part of the element's box model.

Combining With Line Height

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

The line-height property still participates in text layout. Trimming adjusts the selected text-box boundaries rather than replacing line-height behavior.

Multiline Text

For multiline text containers, trimming applies at the selected outer text-box edges. It does not remove the normal spacing between every pair of internal lines.

Vertical Writing Modes

The block-start and block-end edges depend on the writing mode. In horizontal writing, they usually correspond to the top and bottom edges. In vertical writing, they follow the block axis.

Responsive Layouts

Changes in font size, font family, line wrapping, and available width can affect the final layout. Test trimmed text at different viewport sizes.

Practical Examples

Trim a Page Heading

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

Trims the outer text-box edges of a large heading.

Trim Only the Top Edge

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

Trims the block-start edge using the cap-height metric.

Trim Only the Bottom Edge

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

Trims the block-end edge using the text-under metric.

Button Label

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

Can help align the button's text more closely with its intended vertical spacing.

Card Heading

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

Trims the heading's text box while retaining its bottom margin.

Compact Label

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

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

Responsive Heading

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

Retains the trimming behavior as the heading's font size changes.

Restore Normal Text Spacing

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

Disables text box trimming and restores the initial edge definition.

Progressive Enhancement

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

Applies trimming only when the browser recognizes the declaration.

text-box Example

This example compares a heading with normal text-box spacing against one using text-box: trim-both cap alphabetic. The colored background helps reveal the difference in the text container's vertical dimensions.

HTML Example

<div class="text-box-demo">
  <div class="text-box-panel">
    <h4>Normal Text Box</h4>
    <div class="text-box-sample normal">
      <span>CSS Heading</span>
    </div>
  </div>
  <div class="text-box-panel">
    <h4>Trimmed Text Box</h4>
    <div class="text-box-sample trimmed">
      <span>CSS Heading</span>
    </div>
  </div>
</div>

CSS Example

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

Normal Text Box

CSS Heading

Trimmed Text Box

CSS Heading

In browsers that support text-box, the trimmed sample can occupy less vertical space because its selected outer text-box edges are adjusted using font metrics. The visible text remains the same size.

Comparing the Results

Declaration Behavior
text-box: normal Preserves normal text-box spacing.
text-box: trim-both cap alphabetic Trims the outer edges using cap-height and alphabetic baseline metrics.

Changing the Edge Metrics

.text-box-demo .trimmed {
  text-box: trim-both ex alphabetic;
}

The trimmed sample now uses the x-height metric for its over edge.

Trimming Only One Edge

.text-box-demo .trimmed {
  text-box: trim-start cap;
}

Only the block-start edge is trimmed.

Testing Browser Support

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

The feature query limits the trimming declaration to browsers that recognize the property and value.

Accessibility Considerations

Text box trimming can improve visual alignment, but it should not reduce readability or interfere with the presentation of important characters.

Avoid Clipping Glyphs

Some glyphs, accents, and descenders can extend beyond selected font metrics. Test trimmed text carefully, especially when an ancestor uses overflow: hidden.

Preserve Readable Line Spacing

Do not use trimming as a substitute for appropriate line-height. Multiline text still needs sufficient spacing between lines.

Test Different Languages

Fonts and writing systems use different metrics. A trimming configuration suitable for Latin text may not be appropriate for every language.

Support Text Resizing

Test the layout when users enlarge text or change browser zoom levels. Trimmed elements should remain readable and should not overlap surrounding content.

Maintain Adequate Touch Targets

For buttons and other interactive controls, preserve sufficient padding and minimum dimensions. Text trimming should not make controls difficult to activate.

Use Progressive Enhancement

Because browser support may vary, ensure the interface remains usable without text box trimming.

Check Font Fallbacks

Fallback fonts may have different metrics, which can change the amount of trimming. Test the fonts actually used by the browser.

Common Mistakes

Confusing text-box With the CSS Box Model

The property does not replace margin, padding, border, or box-sizing. It specifically controls text-box edge trimming.

Using Unsupported Alignment Values

h2 {
  text-box: center;
}

The center keyword is not a valid text-box value.

Using Lengths or Percentages

h2 {
  text-box: 10px;
}

The shorthand uses trimming and font-metric keywords, not arbitrary numeric measurements.

Expecting Edge Values Alone to Trim Text

h2 {
  text-box: cap alphabetic;
}

This declaration sets the edge metrics but leaves trimming disabled. Include a trimming value to activate it.

Expecting Identical Results Across Fonts

Different fonts use different metrics, so identical declarations may produce different amounts of trimming.

Ignoring Multiline Text

Text box trimming adjusts the selected outer edges. It does not remove all spacing between internal lines.

Removing Padding Unnecessarily

Padding remains independent of text box trimming. Removing padding from interactive elements may reduce their usable area.

Ignoring Browser Support

Unsupported browsers ignore the declaration. Provide a layout that remains acceptable without trimming.

Assuming Trimming Changes Font Size

The property does not resize glyphs. It changes the space associated with selected text-box edges.

Using Trimming to Fix Every Spacing Problem

Text box trimming is useful for font-related edge spacing, but ordinary layout spacing should still be controlled with appropriate CSS properties.

Overlooking Shorthand Resets

A text-box declaration resets omitted longhand components to their initial values. It does not preserve previously declared values automatically.

Important Considerations

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

  • It is a shorthand for text-box-trim and text-box-edge.
  • It controls trimming at the block-start and block-end edges of text containers.
  • The initial shorthand behavior is normal.
  • The normal value disables trimming and restores automatic edge metrics.
  • 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 text-box-edge component defines the metrics used for trimming.
  • The cap value refers to the font's cap-height metric.
  • The ex value refers to the font's x-height metric.
  • The alphabetic value refers to the alphabetic baseline.
  • The text value uses font text-edge metrics.
  • Trimming depends on font metrics rather than individual glyph outlines.
  • The property does not change font size.
  • The property does not remove margins or padding.
  • It can affect an element's block-axis dimensions.
  • It does not replace normal line-height calculations.
  • For multiline text, trimming applies at the selected outer text-box edges.
  • Vertical writing modes change the physical location of block-start and block-end.
  • The same declaration may produce different results with different fonts.
  • Fallback fonts can change the visual result.
  • Accents and other glyph features may extend beyond selected trimming metrics.
  • Text can be clipped when overflow restrictions are applied.
  • The shorthand resets omitted longhand values.
  • Edge definitions alone do not activate trimming.
  • Lengths and percentages are not valid shorthand values.
  • Browser support should be checked before relying on the property for critical layout behavior.
  • Feature queries can help apply trimming as a progressive enhancement.
  • Test responsive layouts and text resizing.
  • Preserve sufficient spacing and dimensions for interactive controls.

Browser Support

The CSS text-box shorthand is a newer CSS feature with limited availability compared with established typography properties. Support varies by browser and version.

Browsers that do not recognize the shorthand ignore the declaration and retain normal text-box behavior. Because the feature affects layout dimensions, test it in the browsers your website supports.

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

Feature Detection

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

The feature query checks whether the browser recognizes the shorthand declaration.

Fallback Behavior

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

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

Testing Recommendations

Test the property with different fonts, multiline headings, narrow containers, text resizing, and writing modes. Pay particular attention to clipping and unexpected changes in element height.

Specifications

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

CSS Inline Layout Module Level 3 - text-box Property

The specification defines the shorthand syntax, its relationship to the longhand properties, and the text-edge trimming model.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: text-box