white-space
CSS property sets how
white space
inside an element is handled.
注意:
To make words break
within themselves
,使用
overflow-wrap
,
word-break
,或
hyphens
代替。
/* Keyword values */ white-space: normal; white-space: nowrap; white-space: pre; white-space: pre-wrap; white-space: pre-line; white-space: break-spaces; /* Global values */ white-space: inherit; white-space: initial; white-space: unset;
white-space
property is specified as a single keyword chosen from the list of values below.
normal
Sequences of white space are collapsed. Newline characters in the source are handled the same as other white space. Lines are broken as necessary to fill line boxes.
nowrap
normal
, but suppresses line breaks (text wrapping) within the source.
pre
<br>
elements.
pre-wrap
<br>
, and as necessary to fill line boxes.
pre-line
<br>
, and as necessary to fill line boxes.
break-spaces
pre-wrap
, except that:
The following table summarizes the behavior of the various
white-space
values:
| New lines | Spaces and tabs | Text wrapping | End-of-line spaces | |
|---|---|---|---|---|
normal
|
Collapse | Collapse | Wrap | Remove |
nowrap
|
Collapse | Collapse | No wrap | Remove |
pre
|
Preserve | Preserve | No wrap | Preserve |
pre-wrap
|
Preserve | Preserve | Wrap | Hang |
pre-line
|
Preserve | Collapse | Wrap | Remove |
break-spaces
|
Preserve | Preserve | Wrap | Wrap |
| 初始值 |
normal
|
|---|---|
| 适用于 | 所有元素 |
| 继承 | yes |
| 计算值 | 如指定 |
| 动画类型 | discrete |
normal | pre | nowrap | pre-wrap | pre-line | break-spaces
code {
white-space: pre;
}
pre {
word-wrap: break-word; /* IE 5.5-7 */
white-space: pre-wrap; /* Modern browsers */
}
<div id="css-code" class="box">
p { white-space:
<select>
<option>normal</option>
<option>nowrap</option>
<option>pre</option>
<option>pre-wrap</option>
<option>pre-line</option>
<option>break-spaces</option>
</select> }
</div>
<div id="results" class="box">
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
.box {
width: 300px;
padding: 16px;
border-radius: 10px;
}
#css-code {
background-color: rgb(220, 220, 220);
font-size: 16px;
font-family: monospace;
}
#css-code select {
font-family: inherit;
}
#results {
background-color: rgb(230, 230, 230);
overflow-x: scroll;
height: 400px;
white-space: normal;
font-size: 14px;
}
var select = document.querySelector("#css-code select");
var results = document.querySelector("#results p");
select.addEventListener("change", function(e) {
results.setAttribute("style", "white-space: "+e.target.value);
})
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Text Module Level 3
The definition of 'white-space' in that specification. |
工作草案 | Precisely defines the breaking algorithms. |
|
CSS Level 2 (Revision 1)
The definition of 'white-space' in that specification. |
推荐 | 初始定义。 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
white-space
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 5.5 | Opera 完整支持 4 | Safari 完整支持 1 | WebView Android 完整支持 1 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
break-spaces
|
Chrome 完整支持 76 | Edge 完整支持 79 | Firefox 完整支持 69 | IE 不支持 No | Opera 完整支持 62 | Safari 完整支持 13.1 | WebView Android 完整支持 76 | Chrome Android 完整支持 76 | Firefox Android 不支持 No | Opera Android 完整支持 54 | Safari iOS 完整支持 13.4 | Samsung Internet Android 完整支持 12.0 |
nowrap
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 5.5 | Opera 完整支持 4 | Safari 完整支持 1 | WebView Android 完整支持 1 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
pre
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 6 | Opera 完整支持 4 | Safari 完整支持 1 | WebView Android 完整支持 37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
pre-line
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 3.5 | IE 完整支持 8 | Opera 完整支持 9.5 | Safari 完整支持 3 | WebView Android 完整支持 37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
pre-wrap
|
Chrome 完整支持 1 | Edge 完整支持 12 |
Firefox
完整支持
3
|
IE
完整支持
8
注意事项
|
Opera 完整支持 8 | Safari 完整支持 3 | WebView Android 完整支持 37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
| Support in SVG | Chrome 不支持 No | Edge 不支持 12 — 79 | Firefox 完整支持 36 | IE 完整支持 10 | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 36 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
Support on
<textarea>
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 36 | IE 完整支持 5.5 | Opera 完整支持 4 | Safari 完整支持 1 | WebView Android 完整支持 37 | Chrome Android 完整支持 18 | Firefox Android 完整支持 36 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
完整支持
不支持
见实现注意事项。
要求使用供应商前缀或不同名称。
overflow-wrap
,
word-break
,
hyphens