AIFeed

text-overflow 中的字符串:截断文本的一大胜利

AI summary

text-overflow: <string> 究竟有什么作用?text-overflow: ellipsis 会在行盒末尾添加一个省略号(…),替换掉它所占据的那些字符。

Translation(model translation, not the source's own text)

Chrome 157 实现了 text-overflow: <string>,紧随 2011 年发布的 Firefox 9 之后,呃……2011 年。Safari 27.3 Beta 也已支持该特性,因此我们可以预期它很快就会进入 Baseline。

但 text-overflow: <string> 究竟做什么呢?text-overflow: ellipsis 会在行盒末尾添加一个省略号(…),替换掉它所占据的那些字符。这与 text-overflow: clip(默认值)不同,后者不添加任何内容。


        <!-- Block container -->
<div>This text is overflowing its container.</div>
      

        div {
  /* Size */
  inline-size: 170px;

  /* Prevent wrapping */
  white-space: nowrap;

  /* Hide any overflow */
  overflow-inline: hidden;

  /* Append string */
  text-overflow: "—";
}
      

首先,文本必须在其行内方向上溢出块容器。这就是为什么我在上一个示例中声明了 inline-size: 170px,假设 writing-mode 计算为 horizontal-tb,这决定了宽度。

接下来,white-space: nowrap 阻止文本换行,导致只要行内方向没有足够空间容纳文本就会溢出。是的,这意味着 text-overflow 适用于单行文本(对于多行文本,你需要 line-clamp)。

overflow 必须设置为 visible 以外的任何值。

最后,text-overflow: "—" 会在行盒末尾添加一个 em dash(—),同时截断它所占据的那些字符。

注意,字符串可以为空(text-overflow: "")。这会截断任何溢出的字符而不替换它们,不同于默认的 text-overflow: clip,后者可能会留下不完整的字符。

字符串应该是什么?

关键在于让文本被截断这件事显而易见。我们可以再次使用 em dash,因为它们通常用于对话中的打断和突然中断。

还有 text-overflow: "…",但这与 text-overflow: ellipsis 几乎是一回事。不过,[…] 可用于截断引文,同时表明这是编辑性省略或设计决策,而非引文的结尾。如果我们希望它可点击,类似 [read more](方括号看起来像一个临时按钮)是一种常见格式。然而,text-overflow 字符串不可点击,而且方括号本身已有含义,所以我不推荐这种格式。

其他常见的方括号插入语包括 [truncated]、[sic]、[snip] 和 [excerpt],因此 text-overflow: <string> 的适用场景比你想象的要多。

脱字符(▾)和尖角号(⌄)(没错,它们也是字符串)暗示可展开,但同样,text-overflow 字符串不可点击,所以我们不能用它来取消截断溢出的文本。不过,我们可以让元素本身响应 :hover(或类似事件):


        <div>Hover this container to reveal the full text.</div>
      

        div {
  /* Size */
  inline-size: 170px;

&:not(:hover) {
    /* Prevent wrapping */
    white-space: nowrap;

/* Hide any overflow */
    overflow-inline: hidden;

/* Append string */
    text-overflow: "▾";
  }
}
      

不过它不可动画化。抱歉,连 @starting-style 也不行。

还有双值语法

嗯?用来做什么?

好吧,还记得 overflow 可以设置为 visible 以外的任何值吗?那么,如果容器可滚动(例如 overflow-inline: scroll),双值语法可以在两端都添加省略号/字符串:


        <!-- Block container -->
<div>Hey, scroll this container.</div>
      

        div {
  /* Size */
  inline-size: 170px;

/* Prevent wrapping */
  white-space: nowrap;

/* Hide any overflow, allow scrolling */
  overflow-inline: scroll;

/* <inline-start> <inline-end> */
  text-overflow: "///" ellipsis;
}
      

<inline-end> 是文本之后出现的内容,而 <inline-start> 是文本之前出现的内容。因此,正如我们在下面的演示中所看到的,如果文本还有更多内容可看,省略号会出现在文本末尾;如果文本还有更多内容可看,三个正斜杠(///)会出现在文本开头。如果文本在两端都溢出了容器,我们会同时看到省略号和 ///。

只有 Firefox 实现了这种双值语法:

正在被滚动的元素或可编辑内容的聚焦元素会回退到 text-overflow: clip,以便用户在尝试与之交互时能看到完整文本。不过,这一行为在不同浏览器之间可能有所差异。

接下来是什么?

text-overflow: <string> 让我们能够超越省略号,转向自定义字符串,用以说明文本被截断的原因。此外,单值语法会遵循方向,因此它也是合乎逻辑的。

但这还不是全部;我们还有 text-overflow: fade() 和] fade 即将到来。text-overflow: fade(1em) 会从行盒末尾起 1em 处开始淡化该行,而 text-overflow: fade(10%) 会从末尾起 10% 处开始淡化,并且无论如何到末尾时都会完全透明。text-overflow: fade 做同样的事情,但允许用户代理自行决定该值。

line-clamp 属性本质上与 text-overflow 相同,只不过它针对的是多行文本。然而,它只能截断最后一行(不能截断第一行),而且看起来它也不会支持 fade() 或 fade。

无论如何,一旦所有这些特性都得到网页浏览器的支持,我认为 text-overflow 和 line-clamp 会变得相当有用。我们已经玩 line-clamp 玩了很久了,能有新的技巧很不错。

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by CSS-Tricks
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗