text-overflow - CSS: Cascading Style Sheets 编辑

The text-overflow CSS property sets how hidden overflow content is signaled to users. It can be clipped, display an ellipsis (''), or display a custom string.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

The text-overflow property doesn't force an overflow to occur. To make text overflow its container you have to set other CSS properties: overflow and white-space. For example:

overflow: hidden;
white-space: nowrap;

The text-overflow property only affects content that is overflowing a block container element in its inline progression direction (not text overflowing at the bottom of a box, for example).

Syntax

The text-overflow property may be specified using one or two values. If one value is given, it specifies overflow behavior for the end of the line (the right end for left-to-right text, the left end for right-to-left text). If two values are given, the first specifies overflow behavior for the left end of the line, and the second specifies it for the right end of the line.

Each value is specified as one of:

Values

clip
The default for this property. This keyword value will truncate the text at the limit of the content area, therefore the truncation can happen in the middle of a character. To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: '';.
ellipsis
This keyword value will display an ellipsis ('…', U+2026 Horizontal Ellipsis) to represent clipped text. The ellipsis is displayed inside the content area, decreasing the amount of text displayed. If there is not enough space to display the ellipsis, it is clipped.
<string> This is an experimental API that should not be used in production code.
The <string> to be used to represent clipped text. The string is displayed inside the content area, shortening the size of the displayed text. If there is not enough space to display the string itself, it is clipped.
fade This is an experimental API that should not be used in production code.
This keyword clips the overflowing inline content and applies a fade-out effect near the edge of the line box with complete transparency at the edge.
fade( <length> | <percentage> ) This is an experimental API that should not be used in production code.
This function clips the overflowing inline content and applies a fade-out effect near the edge of the line box with complete transparency at the edge.
The argument determines the distance over which the fade effect is applied. The <percentage> is resolved against the width of the line box. Values lower than 0 are clipped to 0. Values greater than the width of the line box are clipped to the width of the line box.

Formal definition

Initial valueclip
Applies toblock container elements
Inheritedno
Computed valueas specified
Animation typediscrete

Formal syntax

[ clip | ellipsis | <string> ]{1,2}

Examples

CSS

p {
  width: 200px;
  border: 1px solid;
  padding: 2px 5px;

  /* BOTH of the following are required for text-overflow */
  white-space: nowrap;
  overflow: hidden;
}

.overflow-visible {
  white-space: initial;
}

.overflow-clip {
  text-overflow: clip;
}

.overflow-ellipsis {
  text-overflow: ellipsis;
}

.overflow-string {
  /* Not supported in most browsers,
     see the 'Browser compatibility' section below */
  text-overflow: " [..]";
}

HTML

<p class="overflow-visible">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p class="overflow-clip">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p class="overflow-ellipsis">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p class="overflow-string">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>

Result

Note: Live results in the following table may be shown incorrectly due to a limitation of the MDN Editor which removes the all contents of style attributes which have text-overflow properties with string value.

CSS valuedirection: ltrdirection: rtl
Expected ResultLive resultExpected ResultLive result
visible overflow1234567890123456789009876543211234567890
text-overflow: clipt-o_clip.png1234567890t-o_clip_rtl.png1234567890
text-overflow: ''123451234567890543211234567890
text-overflow: ellipsis1234…1234567890…43211234567890
text-overflow: '.'1234.1234567890.43211234567890
text-overflow: clip clip12345612345678906543211234567890
text-overflow: clip ellipsis1234…12345678906543…1234567890
text-overflow: clip '.'1234.12345678906543.1234567890
text-overflow: ellipsis clip…34561234567890…43211234567890
text-overflow: ellipsis ellipsis…34…1234567890…43…1234567890
text-overflow: ellipsis '.'…34.1234567890…43.1234567890
text-overflow: ',' clip,34561234567890,43211234567890
text-overflow: ',' ellipsis,34…1234567890,43…1234567890
text-overflow: ',' '.',34.1234567890,43.1234567890

Specifications

SpecificationStatusComment
CSS Overflow Module Level 4Added the values <string> and fade and the fade() function
CSS Overflow Module Level 3
The definition of 'text-overflow' in that specification.
Working DraftInitial definition

A previous version of this interface reached the Candidate Recommendation status. As some not-listed-at-risk features needed to be removed, the spec was demoted to the Working Draft level, explaining why browsers implemented this property unprefixed, though not at the CR state.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:70 次

字数:16747

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文