Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Typography

The text-box property

Reference for the CSS text-box shorthand: trim the extra space above and below text with text-box-trim and text-box-edge. Newer feature with partial browser support.

The text-box property is a shorthand for text-box-trim and text-box-edge. Together they remove the extra space that fonts and line-height add above the first line and below the last line of a block, so the box hugs the letters themselves.

This is a newer feature with partial browser support. Treat it as an enhancement and check a compatibility table before depending on it.

CSS
h1 {
  text-box: trim-both cap alphabetic;
}

Try it

Trimmed and untrimmed boxes

The orange box on the right is as tall as the capital letters when your browser supports text-box. The box on the left keeps the extra space created by the large line height. In a browser without support, both boxes look the same.

Syntax

CSS
text-box: normal | <'text-box-trim'> || <'text-box-edge'>
  • Initial value: normal, which means text-box-trim: none and text-box-edge: auto
  • Applies to: block containers and inline boxes
  • Inherited: no
  • Animatable: no
  • Computed value: as the longhands

Values

Value Description
normal Resets both longhands to their initial values. No trimming.
trim-start, trim-end, trim-both Which sides to trim, as in text-box-trim.
text edge keywords Which font metrics define the edges, as in text-box-edge, for example cap alphabetic.

A common recipe is text-box: trim-both cap alphabetic, which trims to the top of capital letters and the baseline.

Usage notes

  • Why it helps. Lines get "half-leading" from line-height. Trimming it removes the invisible gap, so padding, borders and alignment match what you see.
  • Buttons and badges. Text inside a fixed-padding button can look off-center because of font metrics. Trimming makes optical centering much easier.
  • Only outer edges. Trimming affects the start of the first line and the end of the last line of the box; space between lines is unchanged.
  • Shorthand resets. Writing text-box: trim-both resets the edge to auto. Declare both parts together or use the longhands.
  • Fallbacks. In browsers without support the content simply keeps its extra space, so your design should still work, just with slightly different spacing.
  • Fonts vary. The cap-height and baseline come from the font file, so results differ between fonts.

Browser support

A recent addition. Some current browsers support it and others do not yet, so check a compatibility table and treat it as progressive enhancement.