CSS Reference ยท Typography
The text-box-trim property
Reference for the CSS text-box-trim property: remove half-leading above the first line and below the last line of a text box. Newer feature with partial support.
The text-box-trim property chooses which ends of a text box lose their extra space. Every line of text gets half-leading: the gap that line-height adds above and below the glyphs. At the top of the first line and the bottom of the last line, that gap is just empty space inside the box.
text-box-trim removes it, down to the edge set by text-box-edge. Browser support is partial, so check a compatibility table before relying on it.
.title { text-box-trim: trim-both; text-box-edge: cap alphabetic; }
Try it
With support, the blue box loses its top space for trim-start, its bottom space for trim-end, and both for trim-both. Without support, all four boxes look the same.
Syntax
text-box-trim: none | trim-start | trim-end | trim-both
- Initial value:
none - Applies to: block containers and inline boxes
- Inherited: no
- Animatable: no
- Computed value: as specified
Values
| Value | Description |
|---|---|
none |
No trimming. |
trim-start |
Trims the space before the first formatted line (the top in horizontal text). |
trim-end |
Trims the space after the last formatted line (the bottom in horizontal text). |
trim-both |
Trims both. |
Usage notes
- Edge matters. What counts as the "edge" comes from
text-box-edge. With the defaultautoyou may see little change, so most recipes set the edge tocap alphabetic. - Set it on the block. Put the declaration on the heading, paragraph or button, not on text inside it.
- Only outer sides. Gaps between lines inside a paragraph stay untouched, so multi-line text does not collapse.
- Padding still counts. Trimmed boxes keep their padding, which is exactly what makes uniform padding look balanced.
- Check alignment. Trimming moves the baseline relative to the box, so side-by-side text and icons may need adjustment.
- Progressive enhancement. Treat the trimmed look as a bonus and make sure the layout is still acceptable without it. A
@supports (text-box-trim: trim-both)rule can limit adjustments to browsers that understand the property.
Browser support
Newer and not yet in every browser. Check a compatibility table, and use @supports if your layout depends on it.
Related
text-boxโ the shorthandtext-box-edgeโ which metrics define the edgesline-heightโ source of half-leading- Lesson: Line Height