Baseline Widely available *
The white-space
CSS property sets how white space inside an element is handled.
white-space: preserve nowrap;
<section class="default-example" id="default-example">
<div id="example-element">
<p>
But ere she from the church-door stepped She smiled and told us why: 'It
was a wicked woman's curse,' Quoth she, 'and what care I?' She smiled, and
smiled, and passed it off Ere from the door she steptâ
</p>
</div>
</section>
#example-element {
width: 16rem;
}
#example-element p {
border: 1px solid #c5c5c5;
padding: 0.75rem;
text-align: left;
}
The property specifies two things:
Note: To make words break within themselves, use overflow-wrap
, word-break
, or hyphens
instead.
/* Single keyword values */
white-space: normal;
white-space: pre;
white-space: pre-wrap;
white-space: pre-line;
/* white-space-collapse and text-wrap-mode shorthand values */
white-space: wrap;
white-space: collapse;
white-space: preserve nowrap;
/* Global values */
white-space: inherit;
white-space: initial;
white-space: revert;
white-space: revert-layer;
white-space: unset;
Values
The white-space
property values can be specified as a single keyword chosen from the list of values below, or two values representing shorthand for the white-space-collapse
and text-wrap-mode
properties.
normal
Sequences of white space are collapsed. Newline characters in the source are handled the same as other white spaces. Lines are broken as necessary to fill line boxes.
pre
Sequences of white space are preserved. Lines are only broken at newline characters in the source and at <br>
elements.
pre-wrap
Sequences of white space are preserved. Lines are broken at newline characters, at <br>
, and as necessary to fill line boxes.
pre-line
Sequences of white space are collapsed. Lines are broken at newline characters, at <br>
, and as necessary to fill line boxes.
The following table summarizes the behavior of the various white-space
keyword values:
normal
Collapse Collapse Wrap Remove Hang pre
Preserve Preserve No wrap Preserve No wrap pre-wrap
Preserve Preserve Wrap Hang Hang pre-line
Preserve Collapse Wrap Remove Hang
A tab defaults to 8 spaces and can be configured using the tab-size
property. In the case of normal
, nowrap
, and pre-line
values, every tab is converted to a space (U+0020) character.
Note: There is a distinction made between spaces and other space separators. These are defined as follows:
Spaces (U+0020), tabs (U+0009), and segment breaks (such as newlines).
All other space separators defined in Unicode, other than those already defined as spaces.
Where white space is said to hang, this can affect the size of the box when measured for intrinsic sizing.
Collapsing of white spaceThe white-space-collapse
property page explains the browser algorithm for collapsing white space.
white-space =Examples Basic example
normal |
pre |
nowrap |
pre-wrap |
break-spaces |
pre-line
code {
white-space: pre;
}
Line breaks inside <pre> elements
pre {
white-space: pre-wrap;
}
In action
<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: 350px;
padding: 16px;
}
#css-code {
background-color: rgb(220 220 220);
font-size: 16px;
font-family: monospace;
}
#css-code select {
font-family: inherit;
width: 100px;
}
#results {
background-color: rgb(230 230 230);
overflow-x: scroll;
white-space: normal;
font-size: 14px;
}
const select = document.querySelector("#css-code select");
const results = document.querySelector("#results p");
select.addEventListener("change", (e) => {
results.style.setProperty("white-space", e.target.value);
});
Controlling line wrapping in tables HTML
<table>
<tr>
<td></td>
<td>Very long content that splits</td>
<td class="nw">Very long content that don't split</td>
</tr>
<tr>
<td class="nw">white-space:</td>
<td>normal</td>
<td>nowrap</td>
</tr>
</table>
CSS
table {
border-collapse: collapse;
border: solid black 1px;
width: 250px;
height: 150px;
}
td {
border: solid 1px black;
text-align: center;
}
.nw {
white-space: nowrap;
}
Result Multiple lines in SVG text element
The white-space
CSS property can be used to create multiple lines in a <text>
element, which does not wrap by default.
The text inside the <text>
element needs to be split into multiple lines for the new lines to be detected. After the first line the rest need to have their whitespace removed.
<svg viewBox="0 0 320 150">
<text y="20" x="10">Here is an English paragraph
that is broken into multiple lines
in the source code so that it can
be more easily read and edited
in a text editor.
</text>
</svg>
CSS
text {
white-space: break-spaces;
}
Result Specifications Browser compatibility See also
overflow-wrap
, word-break
, hyphens
tab-size
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3