在本章中,您将了解以下属性:
text-overflow
word-wrap
word-break
writing-mode
CSS text-overflow
属性指定如何溢出内容 显示的内容应向用户发出信号。
可以对其进行剪辑:
This is some long text that will not fit in the box
或者它可以呈现为省略号 (...):
This is some long text that will not fit in the box
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;
}
自己尝试一下 →
<!DOCTYPE html>
<html>
<head>
<style>
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;
}
</style>
</head>
<body>
<h1>The text-overflow Property</h1>
<p>The following two paragraphs contains a long text that will not fit in the box.</p>
<h2>text-overflow: clip:</h2>
<p class="test1">This is some long text that will not fit in the box</p>
<h2>text-overflow: ellipsis:</h2>
<p class="test2">This is some long text that will not fit in the box</p>
</body>
</html>
以下示例展示了如何将鼠标悬停在元素上时显示溢出的内容:
div.test:hover {
overflow: visible;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<head>
<style>
div.test {
white-space: nowrap;
width: 200px;
overflow: hidden;
border: 1px solid #000000;
}
div.test:hover {
overflow: visible;
}
</style>
</head>
<body>
<p>Hover over the two divs below, to see the entire text.</p>
<div class="test" style="text-overflow:ellipsis;">This is some long text that will not fit in the box</div>
<br>
<div class="test" style="text-overflow:clip;">This is some long text that will not fit in the box</div>
</body>
</html>
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;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<head>
<style>
p.test {
width: 11em;
border: 1px solid #000000;
word-wrap: break-word;
}
</style>
</head>
<body>
<h1>The word-wrap Property</h1>
<p class="test">This paragraph contains a very long word: thisisaveryveryveryveryveryverylongword. The long word will break and wrap to the next line.</p>
</body>
</html>
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;
}
自己尝试一下 →
<!DOCTYPE html>
<html>
<head>
<style>
p.test1 {
width: 140px;
border: 1px solid #000000;
word-break: keep-all;
}
p.test2 {
width: 140px;
border: 1px solid #000000;
word-break: break-all;
}
</style>
</head>
<body>
<h1>The word-break Property</h1>
<p class="test1">This paragraph contains some text. This line will-break-at-hyphens.</p>
<p class="test2">This paragraph contains some text. The lines will break at any character.</p>
</body>
</html>
CSS writing-mode
属性指定 文本行是水平还是垂直排列。
一些带有 span 元素的文本,具有 Vertical-rl 书写模式。
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;
}
自己尝试一下 →
<!DOCTYPE html>
<html>
<head>
<style>
p.test1 {
writing-mode: horizontal-tb;
}
span.test2 {
writing-mode: vertical-rl;
}
p.test2 {
writing-mode: vertical-rl;
}
</style>
</head>
<body>
<h1>The writing-mode Property</h1>
<p class="test1">Some text with default writing-mode.</p>
<p>Some text with a span element with a <span class="test2">vertical-rl</span> writing-mode.</p>
<p class="test2">Some text with writing-mode: vertical-rl.</p>
</body>
</html>
下表列出了 CSS 文本效果属性:
指定对齐文本的对齐方式和间距
指定如何向用户发出未显示的溢出内容的信号
指定非 CJK 脚本的换行规则
允许长单词被分解并换行到下一行
指定文本行是水平还是垂直布局