How to trim the space above and below text with CSS text-box-trim
With text-box-trim, you can remove the space above and below text. text-box-edge controls where the trimming stops. If you write cap alphabetic, the top of the element aligns with the top of capital letters and the bottom aligns with the baseline. This fixes problems like button text not looking vertically centered even though the top and bottom padding are equal, or a heading having more space above it than expected, without adjusting numbers by hand.
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I used Noto Sans CJK JP as the font, created a test page for each way of writing the CSS, and measured element heights with getBoundingClientRect(). I did not test Firefox or Safari, other fonts, application to inline elements (such as <span>), application to grid or table containers, vertical writing, or multi-column layout.
- 1. The space above and below text comes from line spacing and from the font
- 2. text-box-edge sets where trimming stops, and text-box-trim sets which sides are trimmed
- 3. Writing both in one declaration with text-box
- 4. Vertically centering button text
- 5. Only the top of the first line and the bottom of the last line are trimmed
- 6. Applied to a parent, the first and last paragraphs inside are trimmed
- 7. Japanese characters overflow with cap alphabetic
- 8. Supported browsers
The space above and below text comes from line spacing and from the font
The height of an element that contains only text is the line height set by line-height. That height includes the half-leading, which creates the space between lines, and the space above and below the characters that the font itself provides.
In the test environment, a paragraph with font-size: 40px and line-height: 1.5 containing "Hello" was 60px tall. However, the capital "H" at the same size reaches only 30px above the baseline (measured with measureText() on a canvas). The remaining 30px is the space above and below the characters.
text-box-edge sets where trimming stops, and text-box-trim sets which sides are trimmed
You use the two properties together.
p {
text-box-trim: trim-both;
text-box-edge: cap alphabetic;
}
text-box-trim decides which sides to trim, top or bottom (MDN page for text-box-trim).
| Value | Sides trimmed |
|---|---|
none |
Nothing (initial value) |
trim-start |
Top only |
trim-end |
Bottom only |
trim-both |
Top and bottom |
text-box-edge decides where trimming stops. The first value is the top position, and the second value is the bottom position (MDN page for text-box-edge). For the top you can write text (the font's ascent position), cap (the top of capital letters), or ex (the top of the lowercase x). For the bottom you can write text (the font's descent position) or alphabetic (the baseline).
The initial value of text-box-edge is auto, which behaves the same as text. If you don't specify text-box-trim or set it to none, writing text-box-edge removes nothing.
In the test environment, I added text-box-trim: trim-both to the 60px paragraph from earlier, changed text-box-edge, and measured the height. The results are below.
text-box-edge |
Element height |
|---|---|
(text-box-trim: none) |
60px |
text |
58px |
text alphabetic |
46px |
cap alphabetic |
29.31px |
ex alphabetic |
21.72px |
With text, only 1px each was removed from the top and bottom of the line spacing. With cap alphabetic, the height was almost the same as the height of "H" (30px). With ex alphabetic, it was almost the same as the height of "x" (22px). When I kept text-box-edge at cap alphabetic and set text-box-trim to trim-start, the height was 42.31px. With trim-end, it was 47px, so only one side was trimmed in each case.

Trimming doesn't change the size of the text. Only the element's box gets smaller. As a result, characters taller than capital letters are drawn outside the element. In the test environment, the top of "Hello" (the top of the l) reached 32px above the baseline. That is slightly beyond the 29.31px that cap alphabetic trimmed to.
Writing both in one declaration with text-box
The shorthand text-box lets you write both in a single declaration.
p {
text-box: trim-both cap alphabetic;
}
In the test environment, I used getComputedStyle() to check the values that the two properties get from each text-box value.
text-box value |
text-box-trim |
text-box-edge |
|---|---|---|
trim-both cap alphabetic |
trim-both |
cap alphabetic |
cap alphabetic |
trim-both |
cap alphabetic |
trim-start |
trim-start |
auto |
normal |
none |
auto |
If you omit the text-box-trim value, it becomes trim-both, not the initial value none. The MDN page for text-box also describes this. So text-box: cap alphabetic alone trims both the top and the bottom.
The top and bottom values of text-box-edge can only be written in this order. In Chromium 153, CSS.supports() accepted cap alphabetic as valid, but rejected alphabetic cap and a single cap value.
Vertically centering button text
text-box-trim is useful for components like buttons, where padding surrounds the text. Equal top and bottom padding does not make the space above and below the text equal. In the test environment, I measured a button with font-size: 20px and padding: 12px 24px without text-box. The distance from the top of the element to the top of capital letters was about 20.3px, and the distance from the baseline to the bottom of the element was about 19px.
.button {
display: inline-block;
font-size: 20px;
padding: 12px 24px;
line-height: 1.5;
text-box: trim-both cap alphabetic;
}

The height of this button was 54px without any setting, and 38.66px with text-box: trim-both cap alphabetic added. 38.66px is the sum of 12px of padding at the top, 12px at the bottom, and the height of capital letters in this font at 20px, which is 14.66px. The distance from the top of capital letters to the top of the element and from the baseline to the bottom of the element both became the 12px padding.
The CSS Inline Layout Module Level 3 specification says that trimming is based on font metrics rather than fixed lengths, so the intended alignment is kept even if the font changes.
Only the top of the first line and the bottom of the last line are trimmed
When text spans multiple lines, the space between lines is not trimmed.
In the test environment, I added text-box: trim-both cap alphabetic to a two-line paragraph with font-size: 20px and line-height: 2. The height went from 80px to 54.66px. The 25.34px reduction is 13.34px trimmed from above the first line plus 12px trimmed from below the second line. That amount is the same as trimming a one-line paragraph top and bottom, so the line spacing is kept.
The CSS Inline Layout Module Level 3 specification also says that trimming applies only to the top of the block's first line and the bottom of its last line.
Applied to a parent, the first and last paragraphs inside are trimmed
text-box-trim also works when applied to a parent element that contains paragraphs, not only to the paragraph itself.
.card-body {
text-box: trim-both cap alphabetic;
}
<div class="card-body">
<p>First</p>
<p>Second</p>
</div>
In the test environment, only the top of the first paragraph and the bottom of the second paragraph were trimmed. The bottom of the first paragraph and the top of the second paragraph were not trimmed. In components where paragraphs are stacked, such as the body of a card, you can tighten the top and bottom spacing all at once.
However, the parent's setting does not always reach the paragraphs.
One case is when there is padding or a border between the parent and the paragraph. In the test environment, adding padding-top: 1px to the first paragraph stopped the top of that paragraph from being trimmed. The specification also says that a non-zero padding or border in between prevents the effect.
Another case is when the parent is a flex container. In the test environment, applying the setting to a parent with display: flex did not trim the paragraphs inside. A note in the specification says that it does not apply to flex, grid, or table layouts, and that the effect does not reach the content inside them. When I applied it directly to a paragraph that was a flex item, it was trimmed.
The specification's definition says text-box-trim is not inherited. In the test environment, when I set it only on the parent, the computed value of text-box-trim on the child paragraph was none. text-box-edge, however, had the computed value cap alphabetic on the child, the same as the parent.
Japanese characters overflow with cap alphabetic
cap and alphabetic refer to the top of capital Latin letters and the baseline. In the test environment's font, kanji and kana extend above and below those positions.

In the test environment, I measured 40px "Nihongo no moji" (Japanese for "Japanese characters") with measureText() on a canvas. The glyphs were drawn up to 34px above the baseline and 4px below it. The element box trimmed with cap alphabetic extends up to 29.31px above the baseline and 0px below it. The characters overflow by about 5px at the top and 4px at the bottom.
text prevents the characters from overflowing, but it only removes the line spacing. In the 40px paragraph earlier, the height went from 60px to just 58px. The more line spacing there is, the more text can trim. In the test environment, I added text-box: trim-both text to a two-line Japanese paragraph with font-size: 20px and line-height: 2. The height went from 80px to 69px.
The CSS Inline Layout Module Level 3 specification also defines ideographic, which aligns with the em box of ideographs, and ideographic-ink, which aligns with the ink of the glyphs. However, in Chromium 153, CSS.supports() rejected both values. In the MDN browser compatibility data, only Firefox 154 supports these two values, and Chrome and Safari do not.
Supported browsers
According to the MDN browser compatibility data, text-box-trim, text-box-edge, and text-box are supported in Chrome 133, Safari 18.2, and Firefox 154. For application to inline elements, Chrome does not support it, Firefox supports it from 154, and Safari supports it from 26.5 with partial support (it does not take effect in vertical writing).





