大网数据广告活动
Web 标准与前端开发

`text-box-trim` CSS property

· 52CE 我爱测

The **`text-box-trim`** CSS property specifies which of the over and under edges of text content to trim from a text element's block container.

The text-box-trim CSS property specifies which of the over and under edges of text content to trim from a text element's block container.

Syntax

/* Keyword values */
text-box-trim: none;
text-box-trim: trim-both;
text-box-trim: trim-start;
text-box-trim: trim-end;

/* Global values */
text-box-trim: inherit;
text-box-trim: initial;
text-box-trim: revert;
text-box-trim: revert-layer;
text-box-trim: unset;

Value

The text-box-trim property value may be specified as one of the following keywords:

  • none
    • : The default value. No space is trimmed from the text.
  • trim-both
    • : The start (over) and end (under) edges are both trimmed.
  • trim-start
    • : The start (over) edge is trimmed.
  • trim-end
    • : The end (under) edge is trimmed.

Description

The text-box-trim property specifies which of the over and under edges of text content to trim from a text element's block container.

Vertical spacing differs between fonts, making consistent typesetting historically challenging on the web. The text-box-trim property — along with its counterpart property text-box-edge, which specifies how much space to trim — makes consistent vertical spacing of text easier to achieve.

The height of text-only content is relative to the height of the font. In digital font files, the height contains all characters, including capital letters, ascenders, descenders, etc. Different fonts have different base line-heights, meaning that lines of text with the same font-size will produce line boxes of differing heights, affecting the appearance of spacing between lines.

The text-box-trim property allows you to trim the over and under edge of the text's block container, making it easier to control text spacing in the block direction.

The actual amount of space trimmed is specified using the text-box-edge property. For example, you can choose to trim the over edge in line with a font's capital letters or lower-case letters, and the under edge flush with the font's baseline.

The text-box-trim property, along with the text-box-edge property, can also be set by using the text-box shorthand.

Formal definition

Formal syntax

Examples

Basic text-box-trim usage

In the following example we set text-box-edge: cap alphabetic on two paragraphs, which trims the over edge of the text elements' block containers to the top of the capital letters and the under edge flush with the text baseline.

We then set text-box-trim values of trim-end on the first one, and trim-both on the second one. This results in the first paragraph only having its under edge trimmed, whereas the second one has both the over and under edge trimmed.

<p class="one">This is .one</p>

<p class="two">This is .two</p>
html {
  font-family: sans-serif;
  height: 100%;
}

body {
  height: inherit;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 100px;
}

p {
  margin: 0;
  font-size: 6rem;
  font-weight: bold;
}
p {
  text-box-edge: cap alphabetic;
  border-top: 5px solid magenta;
  border-bottom: 5px solid magenta;
}

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

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

Result

The output is as follows. Note how we've included a top and bottom border on each paragraph, so that you can see how the space has been trimmed in each case.

basic_text-box-trim_usage

Interactive text-box-trim and text-box-edge value comparison

In this example we provide a user interface that allows you to choose the text-box-trim and text-box-edge values applied to a paragraph of text.

HTML

In our HTML, we include three main items:

  • Three select elements allowing you to set which edges of the paragraph should be trimmed (the text-box-trim value) and how much space to trim from the block-start and block-end edges of the paragraph (the text-box-edge value).
  • A p element containing text, which the text-box-* values are applied to. This paragraph has contenteditable set on it so you can edit the text.
  • An output element that displays the text-box-* declarations applied to the paragraph. This is updated when a selection is made.

We also import a font from the Google Fonts service to apply to our demo's text.

We have hidden the exact HTML code for brevity.

<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
  href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100..900;1,100..900&display=swap"
  rel="stylesheet" />
<section>
  <div>
    <label for="box-trim">Select edge(s) to trim:</label>
    <select id="box-trim">
      <option>none</option>
      <option>trim-start</option>
      <option>trim-end</option>
      <option selected>trim-both</option>
    </select>
  </div>
  <div>
    <label for="trim-over">Select trim over (start) value:</label>
    <select id="trim-over">
      <option>text</option>
      <option selected>cap</option>
      <option>ex</option>
    </select>
  </div>
</section>
<p class="display" contenteditable>Holly Golightly</p>
<section>
  <div>
    <label for="trim-under">Select trim under (end) value:</label>
    <select id="trim-under">
      <option>text</option>
      <option selected>alphabetic</option>
    </select>
  </div>
</section>
<output></output>

CSS

In our CSS, we apply the imported font to the html element and lay out the UI using flexbox. We have hidden most of the CSS code for brevity, but below we show the rules styling the paragraph the text-box-* effects are applied to and the <output> that shows the text-box-* rules being applied:

html {
  font-family: "Roboto", sans-serif;
  height: 100%;
}

body {
  height: inherit;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 30px;
}

section {
  display: flex;
  justify-content: space-between;
}

section:nth-of-type(2) {
  justify-content: flex-end;
  padding-bottom: 30px;
}

select {
  width: 6rem;
}
p {
  margin: 0;
  font-size: 6rem;
  font-weight: bold;
  border-top: 5px solid magenta;
  border-bottom: 5px solid magenta;
}

output {
  border: 2px solid gray;
  border-radius: 10px;
  padding: 10px;
  margin: 0;
  width: fit-content;
}

Again, note how we've included a top and bottom border on the .display paragraph, so that you can see how the space being trimmed changes when different text-box-* values are selected.

JavaScript

In the JavaScript, we start by grabbing references to the three <select> elements and two <p> elements:

const boxTrimSelect = document.getElementById("box-trim");
const trimOverSelect = document.getElementById("trim-over");
const trimUnderSelect = document.getElementById("trim-under");

const displayElem = document.querySelector("p");
const codeElem = document.querySelector("output");

Next, we define a function called setEdgeTrim(). This applies a text-box value to the paragraph based on the values of the <select> elements, and also prints the applied declarations to the output (both the longhand and shorthand equivalents):

function setEdgeTrim() {
  const textBoxTrimValue = boxTrimSelect.value;
  const textBoxEdgeValue = `${trimOverSelect.value} ${trimUnderSelect.value}`;
  displayElem.style.textBox = `${textBoxTrimValue} ${textBoxEdgeValue}`;

  codeElem.innerHTML = `
    <span><code>text-box-trim: ${textBoxTrimValue}</code></span>
    <br>
    <span><code>text-box-edge: ${textBoxEdgeValue}</code></span>
    <br><br>
    <span>Shorthand equivalent:</span>
    <br><br>
    <span><code>text-box: ${textBoxTrimValue} ${textBoxEdgeValue}</code></span>
  `;
}

In the last part of the JavaScript we run the setEdgeTrim() function once to set an initial state for the UI. We then apply change event listeners to all of the <select> elements (via addEventListener) so that setEdgeTrim() is run whenever one of the <select> values changes to update the UI accordingly:

setEdgeTrim();

boxTrimSelect.addEventListener("change", setEdgeTrim);
trimOverSelect.addEventListener("change", setEdgeTrim);
trimUnderSelect.addEventListener("change", setEdgeTrim);

Result

The output is as follows:

interactive_text-box-trim_and_text-box-edge_value_comparison

text-box-trim is initially set to trim-both, meaning that the over and under edges of the paragraph are trimmed. text-box-edge is initially set to cap alphabetic, meaning that the text is trimmed flush with the top of capital letters at the start edge, and flush with the baseline at the end edge.

Try changing the <select> values to see the effect they have on the display text.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。