Text Effect ng CSS

CSS Text Overflow, Word Wrapping, Line Breaking Rules, at Writing Modes

Sa kaniyang kabanata, malalaman mo ang mga sumusunod na attribute:

  • text-overflow
  • word-wrap
  • word-break
  • writing-mode

CSS Text Overflow

CSS text-overflow Ang attribute ay nagtutukoy kung paano ipapakita sa user ang di-nakita na lumang bahagi ng teksto.

Maaaring kutting:

Ito ay isang mahabang teksto na hindi puwedeng ilagay sa kahon. Ito ay isang mahabang teksto na hindi puwedeng ilagay sa kahon.

Maaari itong ipakita bilang tanda ng pagkakabawas (...):

Ito ay isang mahabang teksto na hindi puwedeng ilagay sa kahon. Ito ay isang mahabang teksto na hindi puwedeng ilagay sa kahon.

CSS 代码如下:

实例

p.test1 {
  white-space: nowrap; 
  width: 200px; 
  border: 1px solid #000000;
  overflow: hidden;
  text-overflow: clip; 
}
p.test2 {
  white-space: nowrap; 
  width: 200px; 
  border: 1px solid #000000;
  overflow: hidden;
  text-overflow: ellipsis; 
}

亲自试一试

下面的例子展示了将鼠标悬停在元素上时如何显示溢出的内容:

实例

div.test:hover {
  overflow: visible;
}

亲自试一试

CSS 字换行(Word Wrapping)

CSS word-wrap 属性使长文字能够被折断并换到下一行。

如果一个单词太长而无法容纳在一个区域内,它会向外扩展:

This paragraph contains a very long word: thisisaveryveryveryveryveryverylongword. The long word will break and wrap to the next line.

通过 word-wrap 属性,您可以强制对文本进行换行 - 即使这意味着在词中间将其拆分:

This paragraph contains a very long word: thisisaveryveryveryveryveryverylongword. The long word will break and wrap to the next line.

CSS 代码如下:

实例

允许长单词被打断并换行到下一行:

p {
  word-wrap: break-word;
}

亲自试一试

CSS 换行规则

CSS word-break 属性指定换行规则。

本段包含一些文本。此行将连字符打断:

This paragraph contains some text. This line will-break-at-hyphens.

本段包含一些文本。这些行将在任何字符处中断:

This paragraph contains some text. The lines will break at any character.

CSS 代码如下:

实例

p.test1 {
  word-break: keep-all;
}
p.test2 {
  word-break: break-all;
}

亲自试一试

CSS 书写模式

CSS writing-mode 属性规定文本行是水平放置还是垂直放置。

Some text with a span element with a vertical-rl writing-mode.

下面的例子展示了一些不同的书写模式:

实例

p.test1 {
  writing-mode: horizontal-tb; 
}
span.test2 {
  writing-mode: vertical-rl; 
}
p.test2 {
  writing-mode: vertical-rl; 
}

亲自试一试

CSS 文本效果属性

下表列出了 CSS 文本效果属性:

属性 Description
text-align-last Specify how to align the last line of text.
text-justify Specify how aligned text should be aligned and spaced.
text-overflow Specify how to present the hidden overflow content to the user.
word-break Specify the line break rules for non-CJK scripts.
word-wrap Allow long words to be broken and moved to the next line.
writing-mode Specify whether the specified text line is horizontally or vertically placed.