Effetti di testo CSS

CSS Testo Overflow, Sbordatura Intera Parola, Regole di Sbordatura e Modo di Scrittura

In questa sezione, imparerai le seguenti proprietà:

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

CSS Testo Overflow

CSS text-overflow L'attributo determina come l'overflow non visualizzato deve essere presentato all'utente.

Può essere tagliato:

Questo è un testo lungo che non può essere contenuto in una casella. Questo è un testo lungo che non può essere contenuto in una casella

Può anche essere rappresentato con un punto ellissi (…):

Questo è un testo lungo che non può essere contenuto in una casella. Questo è un testo lungo che non può essere contenuto in una casella

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 文本效果属性:

属性 Descrizione
text-align-last Determinare come allineare l'ultima riga di testo.
text-justify Determinare come allineare e spaziare il testo allineato.
text-overflow Determinare come visualizzare il contenuto sovrapposto non visualizzato.
word-break Determinare le regole di换行 per i caratteri non CJK.
word-wrap Permette che le parole lunghe vengano interrotte e passino alla riga successiva.
writing-mode Determinare se il testo specificato è posizionato orizzontalmente o verticalmente.