Text Effect ng CSS

CSS Text Overflow, Word Break, Wrapping Rules, at Writing Modes

Sa kanyang kabanata, matututunan 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 naipakita na lumubog na nilalaman.

Maaaring bawasan:

Ito ay ilang mahabang teksto na hindi maaring ilagay sa kwadrado. Ito ay ilang mahabang teksto na hindi maaring ilagay sa kwadrado.

Maaari ring ipakita bilang tuldok (...):

Ito ay ilang mahabang teksto na hindi maaring ilagay sa kwadrado. Ito ay ilang mahabang teksto na hindi maaring ilagay sa kwadrado.

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 overflow content that is not displayed 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.