The difference between each of the values is basically how to treat spaces.
In HTML, when there are 2 or more spaces in the source code, the browser renders as only 1 space, and breaks the line of text in the source code is also treated as 1 space. For example:
<div>
texto1 texto2
</div>
See above that between "texto1" and "texto2" has 4 spaces, but the browser only renders 1 space.
And here when one text comes below the other, this "line break in the source code" between "text1" and "text2" is treated with 1 space:
<div>
texto1
texto2
</div>
This is the default HTML. However, if you apply white-space: pre
in the div above, all spaces are rendered as they are in the source code, including indentation spaces and line breaks, see:
div{
white-space: pre;
}
<div>
texto1
texto2
</div>
It’s a kind of WYSIWYG (what you see is what you have).
In the case of pre-line
is different. It deletes more than 1 space in only 1 space (which is normal in HTML) but keeps line breaks in the text as they are in the source code:
div{
width: 100px;
border: 1px solid #000;
white-space: pre-line;
}
<div>
Lorem ipsum dolor
sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
See above that between the words "Lorem" and "ipsum" has 5 spaces, but is rendered only 1 space. It keeps the line breaking of the source code before the text (between the <div>
and the beginning of the text). Note that at the beginning of the text there is a blank line that is the space generated by the indentation of the text in the div.
In the case of pre-wrap
, spaces are preserved as they are in the source code, including the space at the beginning of the container, generating a new line before the text, because of the space between <div>
and the beginning of the text:
div{
width: 100px;
border: 1px solid #000;
white-space: pre-wrap;
}
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</div>
In short:
pre
Keeps spaces and line breaks as they are in the source code.
pre-line
Keeps line breaks from source code and deletes more than 1 space in just 1.
pre-wrap
Same thing from pre-line
, but keeps the spaces as they are in the source code, without deletion.